sidekiq-cron 0.3.1 → 0.4.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
  SHA1:
3
- metadata.gz: e36b7b669446980725e708550d8a4c468a155f42
4
- data.tar.gz: b95a3e582638be3924da88fcd45d2c8e5f6e1826
3
+ metadata.gz: bcc4ae725097b1fc4a42a3c8f0b211f10dd1a046
4
+ data.tar.gz: 50589e96e308bd347d29e917ee8d7c31b00d3782
5
5
  SHA512:
6
- metadata.gz: 333bab73a66a5fc2dcc80646a7cd1a64a34ba9e3c92f46bc31560b7b0b774caaf949864b11d2a63d2571a51c280ce9147b2a64ddc626e7c150b5d01dfcaf2cd6
7
- data.tar.gz: 4f14cf44af1f6c610a7e411577f7a6555b15fb25d19f3909db96461db03c42d22098d50af826b3f54f6d37acbcdfed613f048a81db4bca7fbdf7952cad1ec11a
6
+ metadata.gz: 46b8ae7aa71d31dd693f0a3cf5035ed5f06bbf174da2a742de270c3c9d9d2fc6428b4bc26ca4d71a0028254e4a0b0f1228483906d544f5c92a9f40b0cc3a3eef
7
+ data.tar.gz: 1819f9a1eec1a60c0f1348df9a28747eedab16e4673e10dbfccd3ae62a5b82911f11a6f90dbc1962d664021579f4b723b8d6b97c028bbc9d471af495ead82a6f
data/Changes.md CHANGED
@@ -2,7 +2,7 @@ v 0.3.1
2
2
  -------
3
3
 
4
4
  - add CSRF tags to forms so it will work with sidekiq >= 3.4.2
5
- - remove titl dependency
5
+ - remove tilt dependency
6
6
 
7
7
  v 0.3.0
8
8
  -------
data/Gemfile CHANGED
@@ -1,7 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'sidekiq', '>= 2.17.3'
3
+ gem 'sidekiq', '>= 4.0.0'
4
4
  gem 'rufus-scheduler', '>= 2.0.24'
5
+ gem 'redis-namespace', '>= 1.5.2'
5
6
 
6
7
  group :development do
7
8
  gem 'bundler'
data/README.md CHANGED
@@ -18,7 +18,7 @@ Requirements
18
18
  -----------------
19
19
 
20
20
  - Redis 2.4 or greater is required.
21
- - Sidekiq 2.17.3 or grater is required.
21
+ - Sidekiq 4 or grater is required (for Sidekiq <4 use version sidekiq-cron 0.3.1)
22
22
 
23
23
  Change Log
24
24
  ----------
@@ -47,16 +47,18 @@ _Job properties_:
47
47
  {
48
48
  'name' => 'name_of_job', #must be uniq!
49
49
  'cron' => '1 * * * *',
50
- 'klass' => 'MyClass',
50
+ 'class' => 'MyClass',
51
51
  #OPTIONAL
52
52
  'queue' => 'name of queue',
53
- 'args' => '[Array or Hash] of arguments which will be passed to perform method'
53
+ 'args' => '[Array or Hash] of arguments which will be passed to perform method',
54
+ 'active_job' => true, # enqueue job through rails 4.2+ active job interface
55
+ 'queue_name_prefix' => 'prefix' # rails 4.2+ active job queue with prefix
54
56
  }
55
57
  ```
56
58
 
57
59
  ### Time, cron and sidekiq-cron
58
60
 
59
- Cron line is always evaluated against UTC time. So if you are in Prague (timezone +02:00) and you want a job to be enqueued at 8:30 AM, you will need to adjust cronline to `30 6 * * *`.
61
+ sidekiq-cron uses [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler) to parse the cronline. By default, the timezone this is evaluated against will be the system time of the machine you are running on. If you want to have your jobs enqueued based on a different time zone you can specify a timezone in the cronline, like this `'0 22 * * 1-5 America/Chicago'`. See [rufus-scheduler documentation](https://github.com/jmettraux/rufus-scheduler#a-note-about-timezones) for more information.
60
62
 
61
63
  ### What objects/classes can be scheduled
62
64
  #### Sidekiq Worker
@@ -92,14 +94,14 @@ class HardWorker
92
94
  end
93
95
  end
94
96
 
95
- Sidekiq::Cron::Job.create(name: 'Hard worker - every 5min', cron: '*/5 * * * *', klass: 'HardWorker')
97
+ Sidekiq::Cron::Job.create(name: 'Hard worker - every 5min', cron: '*/5 * * * *', class: 'HardWorker')
96
98
  # => true
97
99
  ```
98
100
 
99
101
  `create` method will return only true/false if job was saved or not.
100
102
 
101
103
  ```ruby
102
- job = Sidekiq::Cron::Job.new(name: 'Hard worker - every 5min', cron: '*/5 * * * *', klass: 'HardWorker')
104
+ job = Sidekiq::Cron::Job.new(name: 'Hard worker - every 5min', cron: '*/5 * * * *', class: 'HardWorker')
103
105
 
104
106
  if job.valid?
105
107
  job.save
@@ -180,7 +182,7 @@ second_job:
180
182
  #initializers/sidekiq.rb
181
183
  schedule_file = "config/schedule.yml"
182
184
 
183
- if File.exists?(schedule_file)
185
+ if File.exists?(schedule_file) && Sidekiq.server?
184
186
  Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
185
187
  end
186
188
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -1,4 +1,2 @@
1
-
2
1
  require "sidekiq"
3
-
4
- require "sidekiq/cron"
2
+ require "sidekiq/cron"
@@ -1,11 +1,6 @@
1
1
  require "sidekiq/cron/job"
2
-
3
- #require poller only if celluloid is defined
4
- if defined?(Celluloid)
5
- require 'celluloid/autostart'
6
- require "sidekiq/cron/poller"
7
- require "sidekiq/cron/launcher"
8
- end
2
+ require "sidekiq/cron/poller"
3
+ require "sidekiq/cron/launcher"
9
4
 
10
5
  module Sidekiq
11
6
  module Cron
@@ -1,6 +1,5 @@
1
1
  require 'sidekiq'
2
2
  require 'sidekiq/util'
3
- require 'sidekiq/actor'
4
3
  require 'rufus-scheduler'
5
4
 
6
5
  module Sidekiq
@@ -45,7 +44,7 @@ module Sidekiq
45
44
  def enque! time = Time.now
46
45
  @last_enqueue_time = time
47
46
 
48
- if defined?(ActiveJob::Base) && @klass.to_s.constantize < ActiveJob::Base
47
+ if @active_job or defined?(ActiveJob::Base) && @klass.to_s.constantize < ActiveJob::Base
49
48
  Sidekiq::Client.push(active_job_message)
50
49
  else
51
50
  Sidekiq::Client.push(sidekiq_worker_message)
@@ -63,13 +62,22 @@ module Sidekiq
63
62
  # active job has different structure how it is loading data from sidekiq
64
63
  # queue, it createaswrapper arround job
65
64
  def active_job_message
65
+ if !"#{@active_job_queue_name_prefix}".empty?
66
+ queue_name = "#{@active_job_queue_name_prefix}_#{@queue}"
67
+ elsif defined?(ActiveJob::Base) && defined?(ActiveJob::Base.queue_name_prefix) && !"#{ActiveJob::Base.queue_name_prefix}".empty?
68
+ queue_name = "#{ActiveJob::Base.queue_name_prefix}_#{@queue}"
69
+ else
70
+ queue_name = @queue
71
+ end
72
+
66
73
  {
67
- 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
68
- 'queue' => @queue,
69
- 'args' => [{
74
+ 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
75
+ 'queue' => queue_name,
76
+ 'description' => @description,
77
+ 'args' => [{
70
78
  'job_class' => @klass,
71
79
  'job_id' => SecureRandom.uuid,
72
- 'queue_name' => @queue,
80
+ 'queue_name' => queue_name,
73
81
  'arguments' => @args
74
82
  }]
75
83
  }
@@ -79,9 +87,10 @@ module Sidekiq
79
87
  # input structure should look like:
80
88
  # {
81
89
  # 'name_of_job' => {
82
- # 'class' => 'MyClass',
83
- # 'cron' => '1 * * * *',
84
- # 'args' => '(OPTIONAL) [Array or Hash]'
90
+ # 'class' => 'MyClass',
91
+ # 'cron' => '1 * * * *',
92
+ # 'args' => '(OPTIONAL) [Array or Hash]',
93
+ # 'description' => '(OPTIONAL) Description of job'
85
94
  # },
86
95
  # 'My super iber cool job' => {
87
96
  # 'class' => 'SecondClass',
@@ -108,10 +117,11 @@ module Sidekiq
108
117
  # input structure should look like:
109
118
  # [
110
119
  # {
111
- # 'name' => 'name_of_job',
112
- # 'class' => 'MyClass',
113
- # 'cron' => '1 * * * *',
114
- # 'args' => '(OPTIONAL) [Array or Hash]'
120
+ # 'name' => 'name_of_job',
121
+ # 'class' => 'MyClass',
122
+ # 'cron' => '1 * * * *',
123
+ # 'args' => '(OPTIONAL) [Array or Hash]',
124
+ # 'description' => '(OPTIONAL) Description of job'
115
125
  # },
116
126
  # {
117
127
  # 'name' => 'Cool Job for Second Class',
@@ -190,7 +200,7 @@ module Sidekiq
190
200
  end
191
201
  end
192
202
 
193
- attr_accessor :name, :cron, :klass, :args, :message
203
+ attr_accessor :name, :cron, :description, :klass, :args, :message
194
204
  attr_reader :last_enqueue_time
195
205
 
196
206
  def initialize input_args = {}
@@ -198,6 +208,7 @@ module Sidekiq
198
208
 
199
209
  @name = args["name"]
200
210
  @cron = args["cron"]
211
+ @description = args["description"] if args["description"]
201
212
 
202
213
  #get class from klass or class
203
214
  @klass = args["klass"] || args["class"]
@@ -215,6 +226,9 @@ module Sidekiq
215
226
  #get right arguments for job
216
227
  @args = args["args"].nil? ? [] : parse_args( args["args"] )
217
228
 
229
+ @active_job = args["active_job"] == true || ("#{args["active_job"]}" =~ (/^(true|t|yes|y|1)$/i)) == 0 || false
230
+ @active_job_queue_name_prefix = args["queue_name_prefix"]
231
+
218
232
  if args["message"]
219
233
  @message = args["message"]
220
234
  message_data = Sidekiq.load_json(@message) || {}
@@ -268,6 +282,14 @@ module Sidekiq
268
282
  save
269
283
  end
270
284
 
285
+ def enabled?
286
+ @status == "enabled"
287
+ end
288
+
289
+ def disabled?
290
+ !enabled?
291
+ end
292
+
271
293
  def status_from_redis
272
294
  out = "enabled"
273
295
  if exists?
@@ -294,9 +316,12 @@ module Sidekiq
294
316
  name: @name,
295
317
  klass: @klass,
296
318
  cron: @cron,
319
+ description: @description,
297
320
  args: @args.is_a?(String) ? @args : Sidekiq.dump_json(@args || []),
298
321
  message: @message.is_a?(String) ? @message : Sidekiq.dump_json(@message || {}),
299
322
  status: @status,
323
+ active_job: @active_job,
324
+ queue_name_prefix: @active_job_queue_name_prefix,
300
325
  last_enqueue_time: @last_enqueue_time,
301
326
  }
302
327
  end
@@ -409,7 +434,7 @@ module Sidekiq
409
434
  end
410
435
 
411
436
  def formated_last_time now = Time.now
412
- last_time(now).getutc
437
+ last_time(now).getutc.iso8601
413
438
  end
414
439
 
415
440
  def self.exists? name
@@ -1,7 +1,7 @@
1
- #require Sidekiq original launcher
1
+ # require Sidekiq original launcher
2
2
  require 'sidekiq/launcher'
3
3
 
4
- #require cron poller
4
+ # require cron poller
5
5
  require 'sidekiq/cron/poller'
6
6
 
7
7
  # For Cron we need to add some methods to Launcher
@@ -11,39 +11,43 @@ require 'sidekiq/cron/poller'
11
11
  # adding start and stop commands to launcher
12
12
  module Sidekiq
13
13
  class Launcher
14
-
15
- #Add cron poller to launcher
14
+ # Add cron poller to launcher
16
15
  attr_reader :cron_poller
17
16
 
18
-
19
- #remember old initialize
17
+ # remember old initialize
20
18
  alias_method :old_initialize, :initialize
21
19
 
22
- #add cron poller and execute normal initialize of Sidekiq launcher
20
+ # add cron poller and execute normal initialize of Sidekiq launcher
23
21
  def initialize(options)
24
- @cron_poller = Sidekiq::Cron::Poller.new
22
+ @cron_poller = Sidekiq::Cron::Poller.new
25
23
  old_initialize options
26
24
  end
27
25
 
28
-
29
- #remember old run
26
+ # remember old run
30
27
  alias_method :old_run, :run
31
28
 
32
- #execute normal run of launcher and run cron poller
29
+ # execute normal run of launcher and run cron poller
33
30
  def run
34
31
  old_run
35
- cron_poller.async.poll(true)
32
+ cron_poller.start
36
33
  end
37
34
 
35
+ # remember old quiet
36
+ alias_method :old_quiet, :quiet
38
37
 
39
- #remember old stop
38
+ # execute normal quiet of launcher and quiet cron poller
39
+ def quiet
40
+ cron_poller.terminate
41
+ old_quiet
42
+ end
43
+
44
+ # remember old stop
40
45
  alias_method :old_stop, :stop
41
46
 
42
- #execute normal stop of launcher and stop cron poller
47
+ # execute normal stop of launcher and stop cron poller
43
48
  def stop
44
- cron_poller.async.terminate if poller.alive?
49
+ cron_poller.terminate
45
50
  old_stop
46
51
  end
47
-
48
52
  end
49
53
  end
@@ -1,65 +1,32 @@
1
1
  require 'sidekiq'
2
2
  require 'sidekiq/util'
3
- require 'sidekiq/actor'
4
3
  require 'sidekiq/cron'
4
+ require 'sidekiq/scheduled'
5
5
 
6
6
  module Sidekiq
7
7
  module Cron
8
-
9
- POLL_INTERVAL = 10
10
-
11
- ##
12
8
  # The Poller checks Redis every N seconds for sheduled cron jobs
13
- class Poller
14
- include Util
15
- include Actor
16
-
17
- def poll(first_time=false)
18
- watchdog('scheduling cron poller thread died!') do
19
- add_jitter if first_time
20
-
21
- begin
22
- time_now = Time.now
23
-
24
- #go through all jobs
25
- Sidekiq::Cron::Job.all.each do |job|
26
- #test if job should be enequed
27
- # if yes add job to queue
28
- begin
29
- job.test_and_enque_for_time! time_now if job && job.valid?
30
- rescue => ex
31
- #problem somewhere in one job
32
- logger.error "CRON JOB: #{ex.message}"
33
- logger.error "CRON JOB: #{ex.backtrace.first}"
34
- end
35
- end
36
-
37
- rescue Exception => ex
38
- # Most likely a problem with redis networking.
39
- # Punt and try again at the next interval
40
- logger.error ex.message
41
- logger.error ex.backtrace.first
42
- end
43
-
44
- after(poll_interval) { poll }
9
+ class Poller < Sidekiq::Scheduled::Poller
10
+ def enqueue
11
+ Sidekiq::Cron::Job.all.each do |job|
12
+ enqueue_job(job)
45
13
  end
14
+ rescue => ex
15
+ # Most likely a problem with redis networking.
16
+ # Punt and try again at the next interval
17
+ logger.error ex.message
18
+ logger.error ex.backtrace.first
46
19
  end
47
20
 
48
21
  private
49
22
 
50
- def poll_interval
51
- Sidekiq.options[:poll_interval] || POLL_INTERVAL
23
+ def enqueue_job(job)
24
+ job.test_and_enque_for_time! Time.now if job && job.valid?
25
+ rescue => ex
26
+ # problem somewhere in one job
27
+ logger.error "CRON JOB: #{ex.message}"
28
+ logger.error "CRON JOB: #{ex.backtrace.first}"
52
29
  end
53
-
54
- def add_jitter
55
- begin
56
- sleep(poll_interval * rand)
57
- rescue Celluloid::Task::TerminatedError
58
- # Hit Ctrl-C when Sidekiq is finished booting and we have a chance
59
- # to get here.
60
- end
61
- end
62
-
63
30
  end
64
31
  end
65
32
  end
@@ -11,8 +11,7 @@
11
11
  <th><%= t('Name') %></th>
12
12
  <th><%= t('Cron') %></th>
13
13
  <th><%= t('Last enque') %></th>
14
- <th><%= t('Arguments') %></th>
15
- <th width="280"><%= t('Actions')%></th>
14
+ <th width="180"><%= t('Actions')%></th>
16
15
  </thead>
17
16
 
18
17
  <tbody>
@@ -20,10 +19,10 @@
20
19
  <% style = "#{job.status == 'disabled' ? "background: #ecc": ""}" %>
21
20
  <tr>
22
21
  <td style="<%= style %>"><%= t job.status %></td>
23
- <td style="<%= style %>"><%= job.name %></td>
24
- <td style="<%= style %>"><b><%= job.cron.gsub(" ", "&nbsp;") %></b></td>
25
- <td style="<%= style %>"><%= job.last_enqueue_time ? relative_time(job.last_enqueue_time) : "-" %></td>
26
22
  <td style="<%= style %>">
23
+ <b><%= job.name %></b>
24
+ <hr style="margin:3px;border:0;">
25
+ <small>
27
26
  <% if job.message and job.message.to_s.size > 100 %>
28
27
  <button data-toggle="collapse" data-target=".worker_<%= index %>" class="btn btn-xs"><%= t('ShowAll')%></button>
29
28
  <div class="toggle worker_<%= index %>" style="display: inline;"><%= job.message[0..100] + "... " %></div>
@@ -31,7 +30,10 @@
31
30
  <% else %>
32
31
  <%= job.message %>
33
32
  <% end %>
33
+ </small>
34
34
  </td>
35
+ <td style="<%= style %>"><b><%= job.cron.gsub(" ", "&nbsp;") %></b></td>
36
+ <td style="<%= style %>"><%= job.last_enqueue_time ? relative_time(job.last_enqueue_time) : "-" %></td>
35
37
  <td style="<%= style %>">
36
38
  <% if job.status == 'enabled' %>
37
39
  <form action="<%= root_path %>cron/<%= CGI.escape(job.name).gsub('+', '%20') %>/enque" method="post">
@@ -9,8 +9,7 @@ header.row
9
9
  th = t('Name')
10
10
  th = t('Cron')
11
11
  th = t('Last enque')
12
- th = t('Arguments')
13
- th width="253px"
12
+ th width="180px"
14
13
  = t('Actions')
15
14
 
16
15
 
@@ -18,17 +17,20 @@ header.row
18
17
  - style = "#{job.status == 'disabled' ? "background: #ecc": ""}"
19
18
  tr
20
19
  td[style="#{style}"]= job.status
21
- td[style="#{style}"]= job.name
20
+ td[style="#{style}"]
21
+ b job.name
22
+ hr style="margin:3px;border:0;"
23
+ small
24
+ - if job.message and job.message.to_s.size > 100
25
+ button data-toggle="collapse" data-target=".worker_#{index}" class="btn btn-mini" = t('ShowAll')
26
+ .toggle[class="worker_#{index}" style="display: inline;"]= job.message[0..100] + "... "
27
+ .toggle[class="worker_#{index}" style="display: none;"]= job.message
28
+ - else
29
+ = job.message
30
+
22
31
  td[style="#{style}"]
23
32
  b == job.cron.gsub(" ", "&nbsp;")
24
33
  td[style="#{style}"]== job.last_enqueue_time ? relative_time(job.last_enqueue_time) : "-"
25
- td[style="#{style}"]
26
- - if job.message and job.message.to_s.size > 100
27
- button data-toggle="collapse" data-target=".worker_#{index}" class="btn btn-mini" = t('ShowAll')
28
- .toggle[class="worker_#{index}" style="display: inline;"]= job.message[0..100] + "... "
29
- .toggle[class="worker_#{index}" style="display: none;"]= job.message
30
- - else
31
- = job.message
32
34
  td[style="#{style}"]
33
35
  -if job.status == 'enabled'
34
36
  form action="#{root_path}cron/#{CGI.escape(job.name).gsub('+', '%20')}/enque" method="post"
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: sidekiq-cron 0.3.1 ruby lib
5
+ # stub: sidekiq-cron 0.4.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "sidekiq-cron"
9
- s.version = "0.3.1"
9
+ s.version = "0.4.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Ondrej Bartas"]
14
- s.date = "2015-08-21"
14
+ s.date = "2015-11-17"
15
15
  s.description = "Enables to set jobs to be run in specified time (using CRON notation)"
16
16
  s.email = "ondrej@bartas.cz"
17
17
  s.extra_rdoc_files = [
@@ -55,8 +55,9 @@ Gem::Specification.new do |s|
55
55
  s.specification_version = 4
56
56
 
57
57
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
58
- s.add_runtime_dependency(%q<sidekiq>, [">= 2.17.3"])
58
+ s.add_runtime_dependency(%q<sidekiq>, [">= 4.0.0"])
59
59
  s.add_runtime_dependency(%q<rufus-scheduler>, [">= 2.0.24"])
60
+ s.add_runtime_dependency(%q<redis-namespace>, [">= 1.5.2"])
60
61
  s.add_development_dependency(%q<bundler>, [">= 0"])
61
62
  s.add_development_dependency(%q<simplecov>, [">= 0"])
62
63
  s.add_development_dependency(%q<shoulda-context>, [">= 0"])
@@ -72,8 +73,9 @@ Gem::Specification.new do |s|
72
73
  s.add_development_dependency(%q<coveralls>, [">= 0"])
73
74
  s.add_development_dependency(%q<shotgun>, [">= 0"])
74
75
  else
75
- s.add_dependency(%q<sidekiq>, [">= 2.17.3"])
76
+ s.add_dependency(%q<sidekiq>, [">= 4.0.0"])
76
77
  s.add_dependency(%q<rufus-scheduler>, [">= 2.0.24"])
78
+ s.add_dependency(%q<redis-namespace>, [">= 1.5.2"])
77
79
  s.add_dependency(%q<bundler>, [">= 0"])
78
80
  s.add_dependency(%q<simplecov>, [">= 0"])
79
81
  s.add_dependency(%q<shoulda-context>, [">= 0"])
@@ -90,8 +92,9 @@ Gem::Specification.new do |s|
90
92
  s.add_dependency(%q<shotgun>, [">= 0"])
91
93
  end
92
94
  else
93
- s.add_dependency(%q<sidekiq>, [">= 2.17.3"])
95
+ s.add_dependency(%q<sidekiq>, [">= 4.0.0"])
94
96
  s.add_dependency(%q<rufus-scheduler>, [">= 2.0.24"])
97
+ s.add_dependency(%q<redis-namespace>, [">= 1.5.2"])
95
98
  s.add_dependency(%q<bundler>, [">= 0"])
96
99
  s.add_dependency(%q<simplecov>, [">= 0"])
97
100
  s.add_dependency(%q<shoulda-context>, [">= 0"])
@@ -23,7 +23,6 @@ require "rack/test"
23
23
  require "mocha/setup"
24
24
 
25
25
  #SIDEKIQ Require - need to have sidekiq running!
26
- require 'celluloid/autostart'
27
26
  require 'sidekiq'
28
27
  require 'sidekiq/util'
29
28
  require 'sidekiq/web'
@@ -53,13 +53,23 @@ describe "Cron Job" do
53
53
  it "have save method" do
54
54
  assert @job.respond_to?(:save)
55
55
  end
56
+
56
57
  it "have valid? method" do
57
58
  assert @job.respond_to?("valid?".to_sym)
58
59
  end
60
+
59
61
  it "have destroy method" do
60
62
  assert @job.respond_to?(:destroy)
61
63
  end
62
64
 
65
+ it "have enabled? method" do
66
+ assert @job.respond_to?(:enabled?)
67
+ end
68
+
69
+ it "have disabled? method" do
70
+ assert @job.respond_to?(:disabled?)
71
+ end
72
+
63
73
  it 'have sort_name - used for sorting enabled disbaled jobs on frontend' do
64
74
  job = Sidekiq::Cron::Job.new(name: "TestName")
65
75
  assert_equal job.sort_name, "0_testname"
@@ -116,7 +126,7 @@ describe "Cron Job" do
116
126
  end
117
127
 
118
128
  it "have to_hash method" do
119
- [:name,:klass,:cron,:args,:message,:status].each do |key|
129
+ [:name,:klass,:cron,:description,:args,:message,:status].each do |key|
120
130
  assert @job.to_hash.has_key?(key), "to_hash must have key: #{key}"
121
131
  end
122
132
  end
@@ -231,12 +241,68 @@ describe "Cron Job" do
231
241
  describe '#active_job_message' do
232
242
  before do
233
243
  SecureRandom.stubs(:uuid).returns('XYZ')
244
+ module ActiveJob
245
+ class Base
246
+ def self.queue_name_prefix
247
+ @@queue_name_prefix
248
+ end
249
+ def self.queue_name_prefix=(queue_name_prefix)
250
+ @@queue_name_prefix = queue_name_prefix
251
+ end
252
+ end
253
+ end
254
+ class ActiveJobTest < ActiveJob::Base; end
255
+ ActiveJob::Base.queue_name_prefix = ''
234
256
 
235
257
  @args = {
236
258
  name: 'Test',
237
259
  cron: '* * * * *',
238
- klass: 'CronTestClass',
260
+ klass: 'ActiveJobTest',
239
261
  queue: 'super_queue',
262
+ description: nil,
263
+ args: { foo: 'bar' }
264
+ }
265
+ @job = Sidekiq::Cron::Job.new(@args)
266
+ end
267
+
268
+ it 'should return valid payload for Sidekiq::Client' do
269
+ payload = {
270
+ 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
271
+ 'queue' => 'super_queue',
272
+ 'description' => nil,
273
+ 'args' => [{
274
+ 'job_class' => 'ActiveJobTest',
275
+ 'job_id' => 'XYZ',
276
+ 'queue_name' => 'super_queue',
277
+ 'arguments' => [{foo: 'bar'}]
278
+ }]
279
+ }
280
+ assert_equal @job.active_job_message, payload
281
+ end
282
+ end
283
+
284
+ describe '#active_job_message with queue_name_prefix' do
285
+ before do
286
+ SecureRandom.stubs(:uuid).returns('XYZ')
287
+ module ActiveJob
288
+ class Base
289
+ def self.queue_name_prefix
290
+ @@queue_name_prefix
291
+ end
292
+ def self.queue_name_prefix=(queue_name_prefix)
293
+ @@queue_name_prefix = queue_name_prefix
294
+ end
295
+ end
296
+ end
297
+ class ActiveJobTest < ActiveJob::Base; end
298
+ ActiveJob::Base.queue_name_prefix = "prefix"
299
+
300
+ @args = {
301
+ name: 'Test',
302
+ cron: '* * * * *',
303
+ klass: 'ActiveJobTest',
304
+ queue: 'super_queue',
305
+ queue_name_prefix: 'prefix',
240
306
  args: { foo: 'bar' }
241
307
  }
242
308
  @job = Sidekiq::Cron::Job.new(@args)
@@ -245,11 +311,12 @@ describe "Cron Job" do
245
311
  it 'should return valid payload for Sidekiq::Client' do
246
312
  payload = {
247
313
  'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
248
- 'queue' => 'super_queue',
314
+ 'queue' => 'prefix_super_queue',
315
+ 'description' => nil,
249
316
  'args' =>[{
250
- 'job_class' => 'CronTestClass',
317
+ 'job_class' => 'ActiveJobTest',
251
318
  'job_id' => 'XYZ',
252
- 'queue_name' => 'super_queue',
319
+ 'queue_name' => 'prefix_super_queue',
253
320
  'arguments' => [{foo: 'bar'}]
254
321
  }]
255
322
  }
@@ -261,9 +328,17 @@ describe "Cron Job" do
261
328
  describe 'active job' do
262
329
  before do
263
330
  module ActiveJob
264
- class Base; end
331
+ class Base
332
+ def self.queue_name_prefix
333
+ @@queue_name_prefix
334
+ end
335
+ def self.queue_name_prefix=(queue_name_prefix)
336
+ @@queue_name_prefix = queue_name_prefix
337
+ end
338
+ end
265
339
  end
266
340
  class ActiveJobTest < ActiveJob::Base; end
341
+ ActiveJob::Base.queue_name_prefix = ""
267
342
 
268
343
  @args = {
269
344
  name: 'Test',
@@ -280,6 +355,129 @@ describe "Cron Job" do
280
355
  end
281
356
  end
282
357
 
358
+ describe 'active job with queue_name_prefix' do
359
+ before do
360
+ module ActiveJob
361
+ class Base
362
+ def self.queue_name_prefix
363
+ @@queue_name_prefix
364
+ end
365
+ def self.queue_name_prefix=(queue_name_prefix)
366
+ @@queue_name_prefix = queue_name_prefix
367
+ end
368
+ end
369
+ end
370
+ class ActiveJobTest < ActiveJob::Base; end
371
+ ActiveJob::Base.queue_name_prefix = "prefix"
372
+
373
+ @args = {
374
+ name: 'Test',
375
+ cron: '* * * * *',
376
+ klass: 'ActiveJobTest',
377
+ queue: 'cron'
378
+ }
379
+ @job = Sidekiq::Cron::Job.new(@args)
380
+ end
381
+
382
+ it 'pushes to queue active jobs message with queue_name_prefix' do
383
+ @job.expects(:active_job_message)
384
+ .returns('class' => 'Test', 'args' => [], 'queue' => 'prefix_cron')
385
+ @job.enque!
386
+ end
387
+ end
388
+
389
+ describe 'active job via configuration (bool: true)' do
390
+ before do
391
+ @args = {
392
+ name: 'Test',
393
+ cron: '* * * * *',
394
+ klass: 'ActiveJobTest',
395
+ active_job: true
396
+ }
397
+ @job = Sidekiq::Cron::Job.new(@args)
398
+ end
399
+
400
+ it 'pushes to queue active jobs message' do
401
+ @job.expects(:active_job_message)
402
+ .returns('class' => 'Test', 'args' => [])
403
+ @job.enque!
404
+ end
405
+ end
406
+
407
+ describe 'active job via configuration (string: true)' do
408
+ before do
409
+ @args = {
410
+ name: 'Test',
411
+ cron: '* * * * *',
412
+ klass: 'ActiveJobTest',
413
+ active_job: 'true'
414
+ }
415
+ @job = Sidekiq::Cron::Job.new(@args)
416
+ end
417
+
418
+ it 'pushes to queue active jobs message' do
419
+ @job.expects(:active_job_message)
420
+ .returns('class' => 'Test', 'args' => [])
421
+ @job.enque!
422
+ end
423
+ end
424
+
425
+ describe 'active job via configuration (string: yes)' do
426
+ before do
427
+ @args = {
428
+ name: 'Test',
429
+ cron: '* * * * *',
430
+ klass: 'ActiveJobTest',
431
+ active_job: 'yes'
432
+ }
433
+ @job = Sidekiq::Cron::Job.new(@args)
434
+ end
435
+
436
+ it 'pushes to queue active jobs message' do
437
+ @job.expects(:active_job_message)
438
+ .returns('class' => 'Test', 'args' => [])
439
+ @job.enque!
440
+ end
441
+ end
442
+
443
+ describe 'active job via configuration (number: 1)' do
444
+ before do
445
+ @args = {
446
+ name: 'Test',
447
+ cron: '* * * * *',
448
+ klass: 'ActiveJobTest',
449
+ active_job: 1
450
+ }
451
+ @job = Sidekiq::Cron::Job.new(@args)
452
+ end
453
+
454
+ it 'pushes to queue active jobs message' do
455
+ @job.expects(:active_job_message)
456
+ .returns('class' => 'Test', 'args' => [])
457
+ @job.enque!
458
+ end
459
+ end
460
+
461
+ describe 'active job via configuration with queue_name_prefix option' do
462
+ before do
463
+ @args = {
464
+ name: 'Test',
465
+ cron: '* * * * *',
466
+ klass: 'ActiveJobTest',
467
+ queue: 'cron',
468
+ active_job: true,
469
+ queue_name_prefix: 'prefix'
470
+ }
471
+ @job = Sidekiq::Cron::Job.new(@args)
472
+ end
473
+
474
+ it 'pushes to queue active jobs message with queue_name_prefix' do
475
+ @job.expects(:active_job_message)
476
+ .returns('class' => 'Test', 'args' => [], 'queue' => 'prefix_cron')
477
+ @job.enque!
478
+ end
479
+ end
480
+
283
481
  describe 'sidekiq worker' do
284
482
  before do
285
483
  @args = {
@@ -356,14 +554,18 @@ describe "Cron Job" do
356
554
  Sidekiq::Cron::Job.create(@args.merge(status: "disabled"))
357
555
  job = Sidekiq::Cron::Job.find(@args)
358
556
  assert_equal job.status, "disabled"
557
+ assert_equal job.disabled?, true
558
+ assert_equal job.enabled?, false
359
559
  end
360
560
 
361
561
  it "be created with status enabled and disable it afterwards" do
362
562
  Sidekiq::Cron::Job.create(@args)
363
563
  job = Sidekiq::Cron::Job.find(@args)
364
564
  assert_equal job.status, "enabled"
565
+ assert_equal job.enabled?, true
365
566
  job.disable!
366
567
  assert_equal job.status, "disabled", "directly after call"
568
+ assert_equal job.disabled?, true
367
569
  job = Sidekiq::Cron::Job.find(@args)
368
570
  assert_equal job.status, "disabled", "after find"
369
571
  end
@@ -24,14 +24,9 @@ describe 'Cron Poller' do
24
24
  }
25
25
  @args2 = @args.merge(name: 'with_queue', klass: 'CronTestClassWithQueue', cron: "*/10 * * * *")
26
26
 
27
- Celluloid.boot
28
27
  @poller = Sidekiq::Cron::Poller.new
29
28
  end
30
29
 
31
- after do
32
- Celluloid.shutdown
33
- end
34
-
35
30
  it 'not enqueue any job - new jobs' do
36
31
  now = Time.now
37
32
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 5, 1)
@@ -40,7 +35,7 @@ describe 'Cron Poller' do
40
35
  Sidekiq::Cron::Job.create(@args)
41
36
  Sidekiq::Cron::Job.create(@args2)
42
37
 
43
- @poller.poll
38
+ @poller.enqueue
44
39
 
45
40
  Sidekiq.redis do |conn|
46
41
  assert_equal 0, conn.llen("queue:default")
@@ -50,7 +45,7 @@ describe 'Cron Poller' do
50
45
  #30 seconds after!
51
46
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 5, 30)
52
47
  Time.stubs(:now).returns(enqueued_time)
53
- @poller.poll
48
+ @poller.enqueue
54
49
 
55
50
  Sidekiq.redis do |conn|
56
51
  assert_equal 0, conn.llen("queue:default")
@@ -66,7 +61,7 @@ describe 'Cron Poller' do
66
61
  Sidekiq::Cron::Job.create(@args)
67
62
  Sidekiq::Cron::Job.create(@args2)
68
63
 
69
- @poller.poll
64
+ @poller.enqueue
70
65
 
71
66
  Sidekiq.redis do |conn|
72
67
  assert_equal 0, conn.llen("queue:default")
@@ -75,7 +70,7 @@ describe 'Cron Poller' do
75
70
 
76
71
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 6, 1)
77
72
  Time.stubs(:now).returns(enqueued_time)
78
- @poller.poll
73
+ @poller.enqueue
79
74
 
80
75
  Sidekiq.redis do |conn|
81
76
  assert_equal 1, conn.llen("queue:default")
@@ -91,7 +86,7 @@ describe 'Cron Poller' do
91
86
  Sidekiq::Cron::Job.create(@args)
92
87
  Sidekiq::Cron::Job.create(@args2)
93
88
 
94
- @poller.poll
89
+ @poller.enqueue
95
90
 
96
91
  Sidekiq.redis do |conn|
97
92
  assert_equal 0, conn.llen("queue:default")
@@ -100,7 +95,7 @@ describe 'Cron Poller' do
100
95
 
101
96
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 10, 5)
102
97
  Time.stubs(:now).returns(enqueued_time)
103
- @poller.poll
98
+ @poller.enqueue
104
99
 
105
100
  Sidekiq.redis do |conn|
106
101
  assert_equal 1, conn.llen("queue:default")
@@ -116,7 +111,7 @@ describe 'Cron Poller' do
116
111
  Sidekiq::Cron::Job.create(@args)
117
112
  Sidekiq::Cron::Job.create(@args2)
118
113
 
119
- @poller.poll
114
+ @poller.enqueue
120
115
 
121
116
  Sidekiq.redis do |conn|
122
117
  assert_equal 0, conn.llen("queue:default")
@@ -125,7 +120,7 @@ describe 'Cron Poller' do
125
120
 
126
121
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 20, 1)
127
122
  Time.stubs(:now).returns(enqueued_time)
128
- @poller.poll false
123
+ @poller.enqueue
129
124
  Sidekiq.redis do |conn|
130
125
  assert_equal 1, conn.llen("queue:default")
131
126
  assert_equal 1, conn.llen("queue:super")
@@ -133,7 +128,7 @@ describe 'Cron Poller' do
133
128
 
134
129
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 20, 2)
135
130
  Time.stubs(:now).returns(enqueued_time)
136
- @poller.poll false
131
+ @poller.enqueue
137
132
  Sidekiq.redis do |conn|
138
133
  assert_equal 1, conn.llen("queue:default")
139
134
  assert_equal 1, conn.llen("queue:super")
@@ -141,7 +136,7 @@ describe 'Cron Poller' do
141
136
 
142
137
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 20, 20)
143
138
  Time.stubs(:now).returns(enqueued_time)
144
- @poller.poll false
139
+ @poller.enqueue
145
140
  Sidekiq.redis do |conn|
146
141
  assert_equal 1, conn.llen("queue:default")
147
142
  assert_equal 1, conn.llen("queue:super")
@@ -149,7 +144,7 @@ describe 'Cron Poller' do
149
144
 
150
145
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 20, 50)
151
146
  Time.stubs(:now).returns(enqueued_time)
152
- @poller.poll false
147
+ @poller.enqueue
153
148
  Sidekiq.redis do |conn|
154
149
  assert_equal 1, conn.llen("queue:default")
155
150
  assert_equal 1, conn.llen("queue:super")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-cron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ondrej Bartas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-21 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 2.17.3
19
+ version: 4.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 2.17.3
26
+ version: 4.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rufus-scheduler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 2.0.24
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis-namespace
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.5.2
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement