delayed_job_recurring 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +42 -1
  3. data/lib/delayed/recurring_job.rb +20 -10
  4. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56a419fef66a6f253c1a663f68c8b2014e168b6c
4
- data.tar.gz: b48e38fab7cd43a9e9a3d172350dce9825f9f13c
3
+ metadata.gz: fff505d7e12a6ba0aad2e7f8dd6ead4986a30980
4
+ data.tar.gz: 12910c837cc398f16dae86c1acaa8387a8b36e18
5
5
  SHA512:
6
- metadata.gz: 407dee43ba1a8e56e68889b6286471886e11105169b08057657f309e28419c7b35c5cdca8004e0c8fbdd3d81c5552cc0d9536b4f04098c8a5c1fad29a8c8fb62
7
- data.tar.gz: 561c484ace963a6e0aa9216a54c425980d6309f09ac7fecaa64e6c60b9d8c5d97e3e23a56b2bd208460f355ca5b6b1d6dbd7dc912542676e0b75f7f2f441e0f1
6
+ metadata.gz: bde7769c64e1e7202481cb79f455a652ddf6ad952fc0fe59167e39c070f67ebf7748f487fb124874312ede5ea2125dac6bee441b5a0a8a05eb3af26344701675
7
+ data.tar.gz: 2d7759c1d9860038b5a3ea7dbd53702ab44ad7243b0c9537f30cdfc2f32ad8e362c5f55da777a93aca4fad8dec98a0fdb3a596bb55d4872df285e962d7d79722
data/README.md CHANGED
@@ -28,12 +28,41 @@ class MyTask
28
28
  end
29
29
  ```
30
30
 
31
- And schedule it. In a rails app, you might put this in an initializer:
31
+ Finally, schedule it:
32
32
 
33
33
  ```ruby
34
34
  MyTask.schedule! # run every day at 11am Pacific time (accounting for daylight savings)
35
35
  ```
36
36
 
37
+ The best practice is to add your `MyTask.schedule!` lines to a rake file, e.g.
38
+
39
+ ```ruby
40
+ # lib/tasks/recurring_jobs.rake
41
+
42
+ namespace :recurring
43
+ task init: :environment do
44
+ MyTask.schedule!
45
+ MyOtherTask.schedule!
46
+
47
+ if Rails.env.production?
48
+ MyProductionOnlyTask.schedule!
49
+ end
50
+ end
51
+ end
52
+ ```
53
+
54
+ and invoke this job by running `rake recurring:init` on each deployment.
55
+
56
+ Alternatively, if your app only has one instance running, you can put your
57
+ `schedule!` calls into an initializer, and then your jobs will be automatically
58
+ scheduled when your app starts. However, if you have more than one instance of
59
+ your app running in production, this will lead to a race condition and you will
60
+ end up with duplicate recurring jobs in the queue.
61
+
62
+ ## ActiveJob
63
+
64
+ Note: your task class should **not** inherit from ActiveJob::Base.
65
+
37
66
  ## Advanced usage
38
67
 
39
68
  ### Passing options to schedule
@@ -54,6 +83,18 @@ MyTask.schedule(run_every: 1.day, run_at: ['11:00', '6:00pm']
54
83
  MyTask.schedule(run_every: 1.week, run_at: ['sunday 8:00am', 'wednesday 8:00am'])
55
84
  ```
56
85
 
86
+ ### Scheduling multiple jobs of same class
87
+
88
+ By default, before scheduling a new job, the old jobs scheduled with the same class will be unscheduled.
89
+
90
+ To schedule multiple jobs with same class, pass an unique matching param `job_matching_param` and value for that matching param in each job as below:
91
+
92
+ ```ruby
93
+ MyTask.schedule(run_at: '12:00', job_matching_param: 'schedule_id', schedule_id: 2)
94
+ ```
95
+
96
+ This allows you to schedule multiple jobs with same class if value of the unique matching param(which is `schedule_id` in above example) is different in each job.
97
+
57
98
  ## Thanks!
58
99
 
59
100
  Many thanks to @ginjo and @kares for their work! This code was derived from https://gist.github.com/ginjo/3688965.
@@ -41,8 +41,7 @@ module Delayed
41
41
  enqueue_opts[:queue] = @schedule_options[:queue] if @schedule_options[:queue]
42
42
 
43
43
  Delayed::Job.transaction do
44
- self.class.jobs.destroy_all
45
-
44
+ self.class.jobs(@schedule_options).destroy_all
46
45
  if Gem.loaded_specs['delayed_job'].version.to_s.first.to_i < 3
47
46
  Delayed::Job.enqueue self, enqueue_opts[:priority], enqueue_opts[:run_at]
48
47
  else
@@ -161,29 +160,40 @@ module Delayed
161
160
  end
162
161
 
163
162
  # Show all jobs for this schedule
164
- def jobs
165
- ::Delayed::Job.where("(handler LIKE ?) OR (handler LIKE ?)", "--- !ruby/object:#{name} %", "--- !ruby/object:#{name}\n%")
163
+ def jobs(options = {})
164
+ options = options.with_indifferent_access
165
+
166
+ # Construct dynamic query with 'job_matching_param' if present
167
+ query = ["((handler LIKE ?) OR (handler LIKE ?))", "--- !ruby/object:#{name} %", "--- !ruby/object:#{name}\n%"]
168
+ if options[:job_matching_param].present?
169
+ matching_key = options[:job_matching_param]
170
+ matching_value = options[matching_key]
171
+ query[0] = "#{query[0]} AND handler LIKE ?"
172
+ query << "%#{matching_key}: #{matching_value}%"
173
+ end
174
+
175
+ ::Delayed::Job.where(query)
166
176
  end
167
177
 
168
178
  # Remove all jobs for this schedule (Stop the schedule)
169
- def unschedule
170
- jobs.each{|j| j.destroy}
179
+ def unschedule(options = {})
180
+ jobs(options).each{|j| j.destroy}
171
181
  end
172
182
 
173
183
  # Main interface to start this schedule (adds it to the jobs table).
174
184
  # Pass in a time to run the first job (nil runs the first job at run_interval from now).
175
185
  def schedule(options = {})
176
- schedule!(options) unless scheduled?
186
+ schedule!(options) unless scheduled?(options)
177
187
  end
178
188
 
179
189
  def schedule!(options = {})
180
190
  return unless Delayed::Worker.delay_jobs
181
- unschedule
191
+ unschedule(options)
182
192
  new.schedule!(options)
183
193
  end
184
194
 
185
- def scheduled?
186
- jobs.count > 0
195
+ def scheduled?(options = {})
196
+ jobs(options).count > 0
187
197
  end
188
198
 
189
199
  def inherited(subclass)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delayed_job_recurring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Novak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-31 00:00:00.000000000 Z
11
+ date: 2017-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -44,28 +44,28 @@ dependencies:
44
44
  requirements:
45
45
  - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.0
47
+ version: 3.6.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: 3.0.0
54
+ version: 3.6.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec-rails
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 3.0.1
61
+ version: 3.6.1
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: 3.0.1
68
+ version: 3.6.1
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
71
  requirement: !ruby/object:Gem::Requirement