rufus-scheduler 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.txt CHANGED
@@ -2,6 +2,15 @@
2
2
  = rufus-scheduler CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-scheduler - 2.0.2 not yet released
6
+
7
+ - unified JobQueue and CronJobQueue, and handed @last_second management to the
8
+ latter
9
+ - #trigger_block method for easier override
10
+ - passing :job => job among Schedulable trigger parameters
11
+
12
+
13
+ == rufus-scheduler - 2.0.1 released 2009/05/07
5
14
  == rufus-scheduler - 2.0.0 released 2009/05/07
6
15
 
7
16
  - initial release
data/CREDITS.txt CHANGED
@@ -10,6 +10,7 @@
10
10
 
11
11
  == Feedback
12
12
 
13
+ - Aldric (readme errors)
13
14
  - Kenneth Kalmer (daemon-kit)
14
15
  - Chris Evans, :timeout tests on JRuby
15
16
  - Tim Uckun, :timeout concept
data/README.rdoc CHANGED
@@ -42,7 +42,7 @@ The usage is similar to the one of the old rufus-scheduler. There are a few diff
42
42
  end
43
43
 
44
44
  scheduler.cron '0 22 * * 1-5' do
45
- # every day of the week at 00:22
45
+ # every day of the week at 22:00 (10pm)
46
46
  puts 'activate security system'
47
47
  end
48
48
 
@@ -50,10 +50,6 @@ The usage is similar to the one of the old rufus-scheduler. There are a few diff
50
50
  puts 'check blood pressure'
51
51
  end
52
52
 
53
- # ...
54
-
55
- scheduler.stop
56
-
57
53
 
58
54
  This code summons a plain version of the scheduler, this can be made more explicit via :
59
55
 
data/TODO.txt CHANGED
@@ -49,3 +49,6 @@
49
49
  [o] check :blocking and every (reschedule blocking...)
50
50
  [o] document :thread_name scheduler option
51
51
 
52
+ [o] unify cron_jobs#trigger_matching_jobs(now) and jobs#job_to_trigger
53
+ [o] pluggable job queues
54
+
@@ -50,16 +50,14 @@ module Scheduler
50
50
  @jobs = []
51
51
  end
52
52
 
53
- # Returns the next job to trigger. Returns nil if none eligible.
53
+ # Triggers all the jobs that are scheduled for 'now'.
54
54
  #
55
- def job_to_trigger
55
+ def trigger_matching_jobs
56
56
 
57
- @mutex.synchronize do
58
- if @jobs.size > 0 && Time.now.to_f >= @jobs.first.at
59
- @jobs.shift
60
- else
61
- nil
62
- end
57
+ now = Time.now
58
+
59
+ while job = job_to_trigger(now)
60
+ job.trigger
63
61
  end
64
62
  end
65
63
 
@@ -104,51 +102,56 @@ module Scheduler
104
102
  protected
105
103
 
106
104
  def delete (job_id)
105
+
107
106
  j = @jobs.find { |j| j.job_id == job_id }
108
107
  @jobs.delete(j) if j
109
- j
108
+ end
109
+
110
+ # Returns the next job to trigger. Returns nil if none eligible.
111
+ #
112
+ def job_to_trigger (now)
113
+
114
+ @mutex.synchronize do
115
+ if @jobs.size > 0 && now.to_f >= @jobs.first.at
116
+ @jobs.shift
117
+ else
118
+ nil
119
+ end
120
+ end
110
121
  end
111
122
  end
112
123
 
113
124
  #
114
125
  # Tracking cron jobs.
115
126
  #
116
- # (mostly synchronizing access to the map of cron jobs)
117
- #
118
- class CronJobQueue
127
+ class CronJobQueue < JobQueue
119
128
 
120
129
  def initialize
121
130
 
122
- @mutex = Mutex.new
123
- @jobs = {}
131
+ super
132
+ @last_cron_second = nil
124
133
  end
125
134
 
126
- def unschedule (job_id)
135
+ def trigger_matching_jobs
127
136
 
128
- @mutex.synchronize { @jobs.delete(job_id) }
129
- end
137
+ now = Time.now
130
138
 
131
- def trigger_matching_jobs (now)
139
+ return if now.sec == @last_cron_second
140
+ @last_cron_second = now.sec
141
+ #
142
+ # ensuring the crons are checked within 1 second (not 1.2 second)
132
143
 
133
- js = @mutex.synchronize { @jobs.values }
134
- # maybe this sync is a bit paranoid
144
+ jobs = @mutex.synchronize { @jobs.dup }
135
145
 
136
- js.each { |job| job.trigger_if_matches(now) }
146
+ jobs.each { |job| job.trigger_if_matches(now) }
137
147
  end
138
148
 
139
149
  def << (job)
140
150
 
141
- @mutex.synchronize { @jobs[job.job_id] = job }
142
- end
143
-
144
- def size
145
-
146
- @jobs.size
147
- end
148
-
149
- def to_h
150
-
151
- @jobs.dup
151
+ @mutex.synchronize do
152
+ delete(job.job_id)
153
+ @jobs << job
154
+ end
152
155
  end
153
156
  end
154
157
 
data/lib/rufus/sc/jobs.rb CHANGED
@@ -33,7 +33,7 @@ module Scheduler
33
33
 
34
34
  # A reference to the scheduler owning this job
35
35
  #
36
- attr_reader :scheduler
36
+ attr_accessor :scheduler
37
37
 
38
38
  # The initial, raw, scheduling info (at / in / every / cron)
39
39
  #
@@ -127,13 +127,7 @@ module Scheduler
127
127
 
128
128
  begin
129
129
 
130
- #args = prepare_args
131
- #@block.call(*args)
132
-
133
- #@block.call(self)
134
-
135
- @block.respond_to?(:call) ?
136
- @block.call(self) : @block.trigger(@params)
130
+ trigger_block
137
131
 
138
132
  @job_thread = nil
139
133
 
@@ -158,6 +152,15 @@ module Scheduler
158
152
  end
159
153
  end
160
154
 
155
+ # Simply encapsulating the block#call/trigger operation, for easy
156
+ # override.
157
+ #
158
+ def trigger_block
159
+
160
+ @block.respond_to?(:call) ?
161
+ @block.call(self) : @block.trigger(@params.merge(:job => self))
162
+ end
163
+
161
164
  # Unschedules this job.
162
165
  #
163
166
  def unschedule
@@ -33,7 +33,7 @@ module Rufus::Scheduler
33
33
 
34
34
  # This gem's version
35
35
  #
36
- VERSION = '2.0.1'
36
+ VERSION = '2.0.2'
37
37
 
38
38
  #
39
39
  # It's OK to pass an object responding to :trigger when scheduling a job
@@ -104,8 +104,8 @@ module Rufus::Scheduler
104
104
 
105
105
  @options = opts
106
106
 
107
- @jobs = JobQueue.new
108
- @cron_jobs = CronJobQueue.new
107
+ @jobs = get_queue(:at, opts)
108
+ @cron_jobs = get_queue(:cron, opts)
109
109
 
110
110
  @frequency = @options[:frequency] || 0.330
111
111
  end
@@ -207,6 +207,7 @@ module Rufus::Scheduler
207
207
  puts '=' * 80
208
208
  puts "scheduler caught exception :"
209
209
  puts exception
210
+ exception.backtrace.each { |l| puts l }
210
211
  puts '=' * 80
211
212
  end
212
213
  end
@@ -245,6 +246,23 @@ module Rufus::Scheduler
245
246
 
246
247
  protected
247
248
 
249
+ # Returns a job queue instance.
250
+ #
251
+ # (made it into a method for easy override)
252
+ #
253
+ def get_queue (type, opts)
254
+
255
+ q = if type == :cron
256
+ opts[:cron_job_queue] || Rufus::Scheduler::CronJobQueue.new
257
+ else
258
+ opts[:job_queue] || Rufus::Scheduler::JobQueue.new
259
+ end
260
+
261
+ q.scheduler = self if q.respond_to?(:scheduler=)
262
+
263
+ q
264
+ end
265
+
248
266
  def combine_opts (schedulable, opts)
249
267
 
250
268
  if schedulable.respond_to?(:trigger)
@@ -263,28 +281,9 @@ module Rufus::Scheduler
263
281
  # triggered.
264
282
  #
265
283
  def step
266
- cron_step
267
- at_step
268
- end
269
-
270
- # calls every second
271
- #
272
- def cron_step
273
284
 
274
- now = Time.now
275
- return if now.sec == @last_cron_second
276
- @last_cron_second = now.sec
277
- #
278
- # ensuring the crons are checked within 1 second (not 1.2 second)
279
-
280
- @cron_jobs.trigger_matching_jobs(now)
281
- end
282
-
283
- def at_step
284
-
285
- while job = @jobs.job_to_trigger
286
- job.trigger
287
- end
285
+ @cron_jobs.trigger_matching_jobs
286
+ @jobs.trigger_matching_jobs
288
287
  end
289
288
 
290
289
  def add_job (job)
@@ -374,6 +373,27 @@ module Rufus::Scheduler
374
373
  # # use a Queue and a worker thread for the 'blocking' jobs
375
374
  #end
376
375
 
376
+ #
377
+ # A rufus-scheduler that steps only when the ruby process receives the
378
+ # 10 / USR1 signal.
379
+ #
380
+ class SignalScheduler < SchedulerCore
381
+
382
+ def initialize (opts={})
383
+
384
+ super(opts)
385
+
386
+ trap(@options[:signal] || 10) do
387
+ step
388
+ end
389
+ end
390
+
391
+ def stop
392
+
393
+ trap(@options[:signal] || 10)
394
+ end
395
+ end
396
+
377
397
  #
378
398
  # A rufus-scheduler that uses an EventMachine periodic timer instead of a
379
399
  # loop.
@@ -386,7 +406,7 @@ module Rufus::Scheduler
386
406
  'EventMachine missing, "require \'eventmachine\'" might help'
387
407
  ) unless defined?(EM)
388
408
 
389
- super
409
+ super(opts)
390
410
  end
391
411
 
392
412
  def start
@@ -49,7 +49,7 @@ module Rufus::Scheduler
49
49
  #
50
50
  def self.is_cron_string (s)
51
51
 
52
- s.match('.+ .+ .+ .+ .+') # well...
52
+ s.match(/.+ .+ .+ .+ .+/) # well...
53
53
  end
54
54
  end
55
55
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-scheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-07 00:00:00 +09:00
12
+ date: 2009-10-28 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  requirements: []
63
63
 
64
64
  rubyforge_project: rufus
65
- rubygems_version: 1.3.2
65
+ rubygems_version: 1.3.5
66
66
  signing_key:
67
67
  specification_version: 3
68
68
  summary: job scheduler for Ruby (at, cron, in and every jobs)