delayed_job_recurring_mongoid 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +61 -0
- data/lib/delayed/recurring_job.rb +234 -0
- data/lib/delayed_job_recurring.rb +3 -0
- metadata +173 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f7176aac736c3f7f78876bfb96468e9f30d5f6da
|
4
|
+
data.tar.gz: 228a93dd86e040b8114640c839a5d9fa6b446102
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8f77007632494f39cc1c5ac79886d752bdff47245cb653329924fb1a496a427bbd52c5c7d851d028a04057c783742720b46a6cfe132c4b05614c8b2adfef208
|
7
|
+
data.tar.gz: d2e215b8d3eff2d56be8755f4ab38ec2e3d05a4893f347ee10fc551b8a62e178bde3bd66e66a117cdb72fce1ddfe6144b4f763bde04c688c71d17c6ae3ef73cb
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Amitree
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# delayed\_job\_recurring\_mongoid
|
2
|
+
[![Build Status](https://travis-ci.org/artdevil/delayed_job_recurring_mongoid.svg?branch=master)](https://travis-ci.org/artdevil/delayed_job_recurring_mongoid)
|
3
|
+
|
4
|
+
Extends delayed\_job to support recurring jobs.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
This is copy project from https://github.com/amitree/delayed_job_recurring and make it compatible with mongoid.
|
9
|
+
|
10
|
+
Add it to your Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'delayed_job_recurring_mongoid'
|
14
|
+
```
|
15
|
+
|
16
|
+
Then define a task class. We like the concept of
|
17
|
+
[interactors](http://eng.joingrouper.com/blog/2014/03/03/rails-the-missing-parts-interactors),
|
18
|
+
so we put our task classes in `app/interactors`. You could also put them in `lib` or even `app/models`.
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
class MyTask
|
22
|
+
include Delayed::RecurringJob
|
23
|
+
run_every 1.day
|
24
|
+
run_at '11:00am'
|
25
|
+
timezone 'US/Pacific'
|
26
|
+
queue 'slow-jobs'
|
27
|
+
def perform
|
28
|
+
# Do some work here!
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
And schedule it. In a rails app, you might put this in an initializer:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
MyTask.schedule! # run every day at 11am Pacific time (accounting for daylight savings)
|
37
|
+
```
|
38
|
+
|
39
|
+
## Advanced usage
|
40
|
+
|
41
|
+
### Passing options to schedule
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
MyTask.schedule(run_at: '12:00')
|
45
|
+
```
|
46
|
+
|
47
|
+
### Running at multiples times each day
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
MyTask.schedule(run_every: 1.day, run_at: ['11:00', '6:00pm']
|
51
|
+
```
|
52
|
+
|
53
|
+
### Running on specific days of the week
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
MyTask.schedule(run_every: 1.week, run_at: ['sunday 8:00am', 'wednesday 8:00am'])
|
57
|
+
```
|
58
|
+
|
59
|
+
## Thanks!
|
60
|
+
|
61
|
+
Many thanks to @ginjo and @kares for their work! This code was derived from https://gist.github.com/ginjo/3688965.
|
@@ -0,0 +1,234 @@
|
|
1
|
+
#
|
2
|
+
# Delayed::RecurringJob
|
3
|
+
#
|
4
|
+
# Inspired by https://gist.github.com/ginjo/3688965
|
5
|
+
#
|
6
|
+
module Delayed
|
7
|
+
module RecurringJob
|
8
|
+
def self.included(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
base.class_eval do
|
11
|
+
@@logger = Delayed::Worker.logger
|
12
|
+
cattr_reader :logger
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def failure
|
17
|
+
schedule!
|
18
|
+
end
|
19
|
+
|
20
|
+
def success
|
21
|
+
schedule!
|
22
|
+
end
|
23
|
+
|
24
|
+
# Schedule this "repeating" job
|
25
|
+
def schedule! options = {}
|
26
|
+
options = options.dup
|
27
|
+
|
28
|
+
if run_every = options.delete(:run_every)
|
29
|
+
options[:run_interval] = serialize_duration(run_every)
|
30
|
+
end
|
31
|
+
|
32
|
+
@schedule_options = options.reverse_merge(@schedule_options || {}).reverse_merge(
|
33
|
+
run_at: self.class.run_at,
|
34
|
+
timezone: self.class.timezone,
|
35
|
+
run_interval: serialize_duration(self.class.run_every),
|
36
|
+
priority: self.class.priority,
|
37
|
+
queue: self.class.queue
|
38
|
+
)
|
39
|
+
|
40
|
+
enqueue_opts = { priority: @schedule_options[:priority], run_at: next_run_time }
|
41
|
+
enqueue_opts[:queue] = @schedule_options[:queue] if @schedule_options[:queue]
|
42
|
+
|
43
|
+
if Gem.loaded_specs['delayed_job'].version.to_s.first.to_i < 3
|
44
|
+
Delayed::Job.enqueue self, enqueue_opts[:priority], enqueue_opts[:run_at]
|
45
|
+
else
|
46
|
+
Delayed::Job.enqueue self, enqueue_opts
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def next_run_time
|
51
|
+
times = @schedule_options[:run_at]
|
52
|
+
times = [times] unless times.is_a? Array
|
53
|
+
times = times.map{|time| parse_time(time, @schedule_options[:timezone])}
|
54
|
+
times = times.map{|time| time.in_time_zone @schedule_options[:timezone]} if @schedule_options[:timezone]
|
55
|
+
|
56
|
+
interval = deserialize_duration(@schedule_options[:run_interval])
|
57
|
+
|
58
|
+
until next_time = next_future_time(times)
|
59
|
+
times.map!{ |time| time + interval }
|
60
|
+
end
|
61
|
+
|
62
|
+
# Update @schedule_options to avoid growing number of calculations each time
|
63
|
+
@schedule_options[:run_at] = times
|
64
|
+
|
65
|
+
next_time
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
# We don't want the run_interval to be serialized as a number of seconds.
|
70
|
+
# 1.day is not the same as 86400 (not all days are 86400 seconds long!)
|
71
|
+
def serialize_duration(duration)
|
72
|
+
case duration
|
73
|
+
when ActiveSupport::Duration
|
74
|
+
{value: duration.value, parts: duration.parts}
|
75
|
+
else
|
76
|
+
duration
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def deserialize_duration(serialized)
|
81
|
+
case serialized
|
82
|
+
when Hash
|
83
|
+
ActiveSupport::Duration.new(serialized[:value], serialized[:parts])
|
84
|
+
else
|
85
|
+
serialized
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def parse_time(time, timezone)
|
90
|
+
case time
|
91
|
+
when String
|
92
|
+
time_with_zone = get_timezone(timezone).parse(time)
|
93
|
+
parts = Date._parse(time, false)
|
94
|
+
wday = parts.fetch(:wday, time_with_zone.wday)
|
95
|
+
time_with_zone + (wday - time_with_zone.wday).days
|
96
|
+
else
|
97
|
+
time
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def get_timezone(zone)
|
102
|
+
if zone
|
103
|
+
ActiveSupport::TimeZone.new(zone)
|
104
|
+
else
|
105
|
+
Time.zone
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def next_future_time(times)
|
110
|
+
times.select{|time| time > Time.now}.min
|
111
|
+
end
|
112
|
+
|
113
|
+
module ClassMethods
|
114
|
+
def run_at(*times)
|
115
|
+
if times.length == 0
|
116
|
+
@run_at || run_every.from_now
|
117
|
+
else
|
118
|
+
if @run_at_inherited
|
119
|
+
@run_at = []
|
120
|
+
@run_at_inherited = nil
|
121
|
+
end
|
122
|
+
@run_at ||= []
|
123
|
+
@run_at.concat times
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def run_every(interval = nil)
|
128
|
+
if interval.nil?
|
129
|
+
@run_interval || 1.hour
|
130
|
+
else
|
131
|
+
@run_interval = interval
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def timezone(zone = nil)
|
136
|
+
if zone.nil?
|
137
|
+
@tz
|
138
|
+
else
|
139
|
+
@tz = zone
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def priority(priority = nil)
|
144
|
+
if priority.nil?
|
145
|
+
@priority
|
146
|
+
else
|
147
|
+
@priority = priority
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def queue(*args)
|
152
|
+
if args.length == 0
|
153
|
+
@queue
|
154
|
+
else
|
155
|
+
@queue = args.first
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Show all jobs for this schedule
|
160
|
+
def jobs
|
161
|
+
::Delayed::Job.any_of({:handler => Regexp.new(".*--- !ruby/object:#{name}.*")})
|
162
|
+
# ::Delayed::Job.where("(handler LIKE ?) OR (handler LIKE ?)", "--- !ruby/object:#{name} %", "--- !ruby/object:#{name}\n%")
|
163
|
+
end
|
164
|
+
|
165
|
+
# Remove all jobs for this schedule (Stop the schedule)
|
166
|
+
def unschedule
|
167
|
+
jobs.each{|j| j.destroy}
|
168
|
+
end
|
169
|
+
|
170
|
+
# Main interface to start this schedule (adds it to the jobs table).
|
171
|
+
# Pass in a time to run the first job (nil runs the first job at run_interval from now).
|
172
|
+
def schedule(options = {})
|
173
|
+
schedule!(options) unless scheduled?
|
174
|
+
end
|
175
|
+
|
176
|
+
def schedule!(options = {})
|
177
|
+
return unless Delayed::Worker.delay_jobs
|
178
|
+
unschedule
|
179
|
+
new.schedule!(options)
|
180
|
+
end
|
181
|
+
|
182
|
+
def scheduled?
|
183
|
+
jobs.count > 0
|
184
|
+
end
|
185
|
+
|
186
|
+
def inherited(subclass)
|
187
|
+
[:@run_at, :@run_interval, :@tz, :@priority].each do |var|
|
188
|
+
next unless instance_variable_defined? var
|
189
|
+
subclass.instance_variable_set var, self.instance_variable_get(var)
|
190
|
+
subclass.instance_variable_set "#{var}_inherited", true
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
end # ClassMethods
|
195
|
+
end # RecurringJob
|
196
|
+
|
197
|
+
module Task
|
198
|
+
# Creates a new class wrapper around a block of code to be scheduled.
|
199
|
+
def self.new(name, options, &block)
|
200
|
+
task_class = Class.new
|
201
|
+
task_class.class_eval do
|
202
|
+
include Delayed::RecurringJob
|
203
|
+
|
204
|
+
def display_name
|
205
|
+
self.class.name
|
206
|
+
end
|
207
|
+
|
208
|
+
def perform
|
209
|
+
block.call
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
Object.const_set(name, task_class) if name
|
214
|
+
task_class.schedule(options)
|
215
|
+
return task_class
|
216
|
+
end
|
217
|
+
|
218
|
+
# Schedule a block of code on-the-fly.
|
219
|
+
# This is a friendly wrapper for using Task.new without an explicit constant assignment.
|
220
|
+
# Delayed::Task.schedule('MyNewTask', run_every: 10.minutes, run_at: 1.minute.from_now){do_some_stuff_here}
|
221
|
+
# or
|
222
|
+
# Delayed::Task.schedule(run_every: 10.minutes, run_at: 1.minute.from_now){do_some_stuff_here}
|
223
|
+
def self.schedule(name_or_options={}, options={}, &block)
|
224
|
+
case name_or_options
|
225
|
+
when Hash
|
226
|
+
name, options = nil, name_or_options
|
227
|
+
else
|
228
|
+
name = name_or_options
|
229
|
+
end
|
230
|
+
|
231
|
+
self.new name, options, &block
|
232
|
+
end
|
233
|
+
end # Task
|
234
|
+
end # Delayed
|
metadata
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delayed_job_recurring_mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adhiguna Utama Sabril
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mongoid-rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: timecop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mongoid
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: database_cleaner
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.3'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.3'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: delayed_job
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: delayed_job_mongoid
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: Extends delayed_job to support recurring jobs, including timezone support
|
140
|
+
email: adhiguna.sabril@gmail.com
|
141
|
+
executables: []
|
142
|
+
extensions: []
|
143
|
+
extra_rdoc_files: []
|
144
|
+
files:
|
145
|
+
- LICENSE
|
146
|
+
- README.md
|
147
|
+
- lib/delayed/recurring_job.rb
|
148
|
+
- lib/delayed_job_recurring.rb
|
149
|
+
homepage: https://github.com/artdevil/delayed_job_recurring_mongoid
|
150
|
+
licenses:
|
151
|
+
- MIT
|
152
|
+
metadata: {}
|
153
|
+
post_install_message:
|
154
|
+
rdoc_options: []
|
155
|
+
require_paths:
|
156
|
+
- lib
|
157
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - ">"
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '1.9'
|
162
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
requirements: []
|
168
|
+
rubyforge_project:
|
169
|
+
rubygems_version: 2.4.6
|
170
|
+
signing_key:
|
171
|
+
specification_version: 4
|
172
|
+
summary: Recurring jobs for delayed_job with mongoid
|
173
|
+
test_files: []
|