|
1 | 1 | module SolidQueue |
2 | | - module Job::Executable |
3 | | - extend ActiveSupport::Concern |
| 2 | + class Job |
| 3 | + module Executable |
| 4 | + extend ActiveSupport::Concern |
4 | 5 |
|
5 | | - included do |
6 | | - has_one :ready_execution, dependent: :destroy |
7 | | - has_one :claimed_execution, dependent: :destroy |
8 | | - has_one :failed_execution, dependent: :destroy |
| 6 | + included do |
| 7 | + include ConcurrencyControls |
9 | 8 |
|
10 | | - has_one :scheduled_execution, dependent: :destroy |
| 9 | + has_one :ready_execution, dependent: :destroy |
| 10 | + has_one :claimed_execution, dependent: :destroy |
| 11 | + has_one :failed_execution, dependent: :destroy |
11 | 12 |
|
12 | | - after_create :prepare_for_execution |
| 13 | + has_one :scheduled_execution, dependent: :destroy |
13 | 14 |
|
14 | | - scope :finished, -> { where.not(finished_at: nil) } |
15 | | - end |
16 | | - |
17 | | - STATUSES = %w[ ready claimed failed scheduled ] |
| 15 | + after_create :prepare_for_execution |
18 | 16 |
|
19 | | - STATUSES.each do |status| |
20 | | - define_method("#{status}?") { public_send("#{status}_execution").present? } |
21 | | - end |
| 17 | + scope :finished, -> { where.not(finished_at: nil) } |
| 18 | + end |
22 | 19 |
|
23 | | - def prepare_for_execution |
24 | | - if due? |
25 | | - ReadyExecution.create_or_find_by!(job_id: id) |
26 | | - else |
27 | | - ScheduledExecution.create_or_find_by!(job_id: id) |
| 20 | + %w[ ready claimed failed scheduled ].each do |status| |
| 21 | + define_method("#{status}?") { public_send("#{status}_execution").present? } |
28 | 22 | end |
29 | | - end |
30 | 23 |
|
31 | | - def finished! |
32 | | - if delete_finished_jobs? |
33 | | - destroy! |
34 | | - else |
35 | | - touch(:finished_at) |
| 24 | + def prepare_for_execution |
| 25 | + if due? then dispatch |
| 26 | + else |
| 27 | + schedule |
| 28 | + end |
36 | 29 | end |
37 | | - end |
38 | 30 |
|
39 | | - def finished? |
40 | | - finished_at.present? |
41 | | - end |
| 31 | + def finished! |
| 32 | + if delete_finished_jobs? |
| 33 | + destroy! |
| 34 | + else |
| 35 | + touch(:finished_at) |
| 36 | + end |
| 37 | + end |
42 | 38 |
|
43 | | - def failed_with(exception) |
44 | | - FailedExecution.create_or_find_by!(job_id: id, exception: exception) |
45 | | - end |
| 39 | + def finished? |
| 40 | + finished_at.present? |
| 41 | + end |
46 | 42 |
|
47 | | - def discard |
48 | | - destroy unless claimed? |
49 | | - end |
| 43 | + def failed_with(exception) |
| 44 | + FailedExecution.create_or_find_by!(job_id: id, exception: exception) |
| 45 | + end |
50 | 46 |
|
51 | | - def retry |
52 | | - failed_execution&.retry |
53 | | - end |
| 47 | + def discard |
| 48 | + destroy unless claimed? |
| 49 | + end |
54 | 50 |
|
55 | | - private |
56 | | - def due? |
57 | | - scheduled_at.nil? || scheduled_at <= Time.current |
| 51 | + def retry |
| 52 | + failed_execution&.retry |
58 | 53 | end |
59 | 54 |
|
60 | | - def delete_finished_jobs? |
61 | | - SolidQueue.delete_finished_jobs |
| 55 | + def failed_with(exception) |
| 56 | + FailedExecution.create_or_find_by!(job_id: id, exception: exception) |
62 | 57 | end |
| 58 | + |
| 59 | + private |
| 60 | + def due? |
| 61 | + scheduled_at.nil? || scheduled_at <= Time.current |
| 62 | + end |
| 63 | + |
| 64 | + def dispatch |
| 65 | + if acquire_concurrency_lock then ready |
| 66 | + else |
| 67 | + block |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def schedule |
| 72 | + ScheduledExecution.create_or_find_by!(job_id: id) |
| 73 | + end |
| 74 | + |
| 75 | + def ready |
| 76 | + ReadyExecution.create_or_find_by!(job_id: id) |
| 77 | + end |
| 78 | + |
| 79 | + |
| 80 | + def delete_finished_jobs? |
| 81 | + SolidQueue.delete_finished_jobs |
| 82 | + end |
| 83 | + end |
63 | 84 | end |
64 | 85 | end |
0 commit comments