good_job 3.17.4 → 3.18.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7b833bb3f1b28dca921562c3110e4147d955a98982f2b9481f9f7f2b41aa648
4
- data.tar.gz: 4f230602f1f5a2faf5198035f207e6f206bc614078a8bce8f38f71613250a8bd
3
+ metadata.gz: cb40c03312f09c2cb91e77bce2ab3aacf87c62a18125d62b50bdc92b312d34f5
4
+ data.tar.gz: c1c73c9264b8bd6454aad7685d326d082c00d5c3f14667ab89e1f77cce9059ea
5
5
  SHA512:
6
- metadata.gz: '0990adc9282acc9ad3b387f494b20e716e9f1f6ba37c1ec4da200dd89b53f8dd8d6cac60dc652323127086c837bf08bb02d3c28f1a75d589a1c435a897524b0d'
7
- data.tar.gz: 961e5e7e3d522f07572f9031096b940408f3bc7568a46b475d45daeb37ae755e237f497cdbb3221e4419bde4d73a560d743f132359b7ee03ffc8062d24f99b23
6
+ metadata.gz: 9825b6d3c5a496c7fdee3aec3f0b5653a6c68d40b15487bb43d21b48817ae077052b3c3520b6ac0dd54354cc68b1157800270e95119240260ef68ab55046b293
7
+ data.tar.gz: a903a5cac7a8b753e457d7e98018ca575825dcb1be515702cd30bd92494f3fc22740e09e48c4b3a8312fae2824021e86d6f6d9e431d0b10f39c05ea97457baca
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [v3.18.0](https://github.com/bensheldon/good_job/tree/v3.18.0) (2023-08-30)
4
+
5
+ [Full Changelog](https://github.com/bensheldon/good_job/compare/v3.17.4...v3.18.0)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - Support configuring cron schedule dynamically with a block [\#1051](https://github.com/bensheldon/good_job/pull/1051) ([DanielHeath](https://github.com/DanielHeath))
10
+
3
11
  ## [v3.17.4](https://github.com/bensheldon/good_job/tree/v3.17.4) (2023-08-24)
4
12
 
5
13
  [Full Changelog](https://github.com/bensheldon/good_job/compare/v3.17.3...v3.17.4)
data/README.md CHANGED
@@ -504,6 +504,10 @@ config.good_job.cron = {
504
504
  cron: "0 0,12 * * *",
505
505
  class: "AnotherJob",
506
506
  },
507
+ complex_schedule: {
508
+ class: "ComplexScheduleJob",
509
+ cron: -> (last_ran) { (last_ran.blank? ? Time.now : last_ran + 14.hours).at_beginning_of_minute }
510
+ }
507
511
  # etc.
508
512
  }
509
513
  ```
@@ -27,6 +27,7 @@ module GoodJob # :nodoc:
27
27
  def initialize(params = {})
28
28
  @params = params
29
29
 
30
+ return if cron_proc?
30
31
  raise ArgumentError, "Invalid cron format: '#{cron}'" unless fugit.instance_of?(Fugit::Cron)
31
32
  end
32
33
 
@@ -41,10 +42,6 @@ module GoodJob # :nodoc:
41
42
  params.fetch(:class)
42
43
  end
43
44
 
44
- def cron
45
- params.fetch(:cron)
46
- end
47
-
48
45
  def set
49
46
  params[:set]
50
47
  end
@@ -61,16 +58,21 @@ module GoodJob # :nodoc:
61
58
  params[:description]
62
59
  end
63
60
 
64
- def next_at
61
+ def next_at(previously_at: nil)
62
+ if cron_proc?
63
+ result = Rails.application.executor.wrap { cron.call(previously_at || last_at) }
64
+ return Fugit.parse(result).next_time.to_t if result.is_a?(String)
65
+
66
+ return result
67
+
68
+ end
65
69
  fugit.next_time.to_t
66
70
  end
67
71
 
68
72
  def schedule
69
- fugit.original
70
- end
73
+ return "Custom schedule" if cron_proc?
71
74
 
72
- def fugit
73
- @_fugit ||= Fugit.parse(cron)
75
+ fugit.original
74
76
  end
75
77
 
76
78
  def jobs
@@ -80,11 +82,7 @@ module GoodJob # :nodoc:
80
82
  def last_at
81
83
  return if last_job.blank?
82
84
 
83
- if GoodJob::Job.column_names.include?('cron_at')
84
- (last_job.cron_at || last_job.created_at).localtime
85
- else
86
- last_job.created_at
87
- end
85
+ (last_job.cron_at || last_job.created_at).localtime
88
86
  end
89
87
 
90
88
  def enabled?
@@ -116,11 +114,7 @@ module GoodJob # :nodoc:
116
114
  end
117
115
 
118
116
  def last_job
119
- if GoodJob::Job.column_names.include?('cron_at')
120
- jobs.order("cron_at DESC NULLS LAST").first
121
- else
122
- jobs.order(created_at: :asc).last
123
- end
117
+ jobs.order("cron_at DESC NULLS LAST").first
124
118
  end
125
119
 
126
120
  def display_properties
@@ -138,6 +132,18 @@ module GoodJob # :nodoc:
138
132
 
139
133
  private
140
134
 
135
+ def cron
136
+ params.fetch(:cron)
137
+ end
138
+
139
+ def cron_proc?
140
+ cron.respond_to?(:call)
141
+ end
142
+
143
+ def fugit
144
+ @_fugit ||= Fugit.parse(cron)
145
+ end
146
+
141
147
  def set_value
142
148
  value = set || {}
143
149
  value.respond_to?(:call) ? value.call : value
@@ -82,12 +82,13 @@ module GoodJob # :nodoc:
82
82
 
83
83
  # Enqueues a scheduled task
84
84
  # @param cron_entry [CronEntry] the CronEntry object to schedule
85
- def create_task(cron_entry)
86
- cron_at = cron_entry.next_at
85
+ # @param previously_at [Date, Time, ActiveSupport::TimeWithZone, nil] the last, +in-memory+, scheduled time the cron task was intended to run
86
+ def create_task(cron_entry, previously_at: nil)
87
+ cron_at = cron_entry.next_at(previously_at: previously_at)
87
88
  delay = [(cron_at - Time.current).to_f, 0].max
88
89
  future = Concurrent::ScheduledTask.new(delay, args: [self, cron_entry, cron_at], executor: @executor) do |thr_scheduler, thr_cron_entry, thr_cron_at|
89
90
  # Re-schedule the next cron task before executing the current task
90
- thr_scheduler.create_task(thr_cron_entry)
91
+ thr_scheduler.create_task(thr_cron_entry, previously_at: thr_cron_at)
91
92
 
92
93
  Rails.application.executor.wrap do
93
94
  cron_entry.enqueue(thr_cron_at) if thr_cron_entry.enabled?
@@ -2,7 +2,7 @@
2
2
 
3
3
  module GoodJob
4
4
  # GoodJob gem version.
5
- VERSION = '3.17.4'
5
+ VERSION = '3.18.0'
6
6
 
7
7
  # GoodJob version as Gem::Version object
8
8
  GEM_VERSION = Gem::Version.new(VERSION)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: good_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.17.4
4
+ version: 3.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Sheldon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-24 00:00:00.000000000 Z
11
+ date: 2023-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob