clockwork 0.6.2 → 0.7.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: 384e3245517c1613d4551a8d401f1f398a5900ee
4
- data.tar.gz: 92d176c5595e3a0c2d456710bc9d527d99f12b0c
3
+ metadata.gz: a05228ef038356c69ac45b0cd26eba0d9571905c
4
+ data.tar.gz: 1202fe6dfa6d4ed81a7850968f99d2512bf5dc5b
5
5
  SHA512:
6
- metadata.gz: 02583e06f9b53687229298b169c703f9be058e507ad95014286538fb5d537c9ecec57f9732903c56ae397dcb605f18ba77998d406248d23465fb34e945692233
7
- data.tar.gz: d72f9f576ecf78cc7870cb317cea3a182052c0b937034e4befa7df483b9711699feb34ca835f5e1f89f2f3bd919a150b45660cf9eafa568360f71c420010c0bc
6
+ metadata.gz: 8353beebe55670202cce99d785d646bfe239e78271f1e211b0e9379d44c785e938f17004c18e8222ec0cad389bdc35b3f5633d767555e45900d9c24c5ba8ae9f
7
+ data.tar.gz: c0d61cafe0c731f02f6b9c4dde2786901cf3f0b11be6e1fd86d8fadb1f73cd14493120f1d805563322e35ece1b41587a7c3c2e472712cc1db45078ff729ec9cc
data/README.md CHANGED
@@ -18,43 +18,16 @@ Create clock.rb:
18
18
 
19
19
  ```ruby
20
20
  require 'clockwork'
21
- include Clockwork
22
-
23
- handler do |job|
24
- puts "Running #{job}"
25
- end
26
-
27
- every(10.seconds, 'frequent.job')
28
- every(3.minutes, 'less.frequent.job')
29
- every(1.hour, 'hourly.job')
30
-
31
- every(1.day, 'midnight.job', :at => '00:00')
32
- ```
33
-
34
- Run it with the clockwork binary:
35
-
36
- ```
37
- $ clockwork clock.rb
38
- Starting clock for 4 events: [ frequent.job less.frequent.job hourly.job midnight.job ]
39
- Triggering frequent.job
40
- ```
41
-
42
- If you would not like to taint the namespace with `include Clockwork`, you can use
43
- it as the module (thanks to [hoverlover](https://github.com/hoverlover/clockwork/)).
44
-
45
- ```ruby
46
- require 'clockwork'
47
-
48
21
  module Clockwork
49
-
50
- configure do |config|
51
- config[:tz] = "America/Chicago"
52
- end
53
-
54
22
  handler do |job|
55
23
  puts "Running #{job}"
56
24
  end
57
25
 
26
+ # handler receives the time when job is prepared to run in the 2nd argument
27
+ # handler do |job, time|
28
+ # puts "Running #{job}, at #{time}"
29
+ # end
30
+
58
31
  every(10.seconds, 'frequent.job')
59
32
  every(3.minutes, 'less.frequent.job')
60
33
  every(1.hour, 'hourly.job')
@@ -63,6 +36,14 @@ module Clockwork
63
36
  end
64
37
  ```
65
38
 
39
+ Run it with the clockwork binary:
40
+
41
+ ```
42
+ $ clockwork clock.rb
43
+ Starting clock for 4 events: [ frequent.job less.frequent.job hourly.job midnight.job ]
44
+ Triggering frequent.job
45
+ ```
46
+
66
47
  If you need to load your entire environment for your jobs, simply add:
67
48
 
68
49
  ```ruby
@@ -100,10 +81,12 @@ For example, if you're using Beanstalk/Staker:
100
81
  ```ruby
101
82
  require 'stalker'
102
83
 
103
- handler { |job| Stalker.enqueue(job) }
84
+ module Clockwork
85
+ handler { |job| Stalker.enqueue(job) }
104
86
 
105
- every(1.hour, 'feeds.refresh')
106
- every(1.day, 'reminders.send', :at => '01:30')
87
+ every(1.hour, 'feeds.refresh')
88
+ every(1.day, 'reminders.send', :at => '01:30')
89
+ end
107
90
  ```
108
91
 
109
92
  Using a queueing system which doesn't require that your full application be
@@ -221,11 +204,13 @@ jobs.
221
204
  ### Configuration example
222
205
 
223
206
  ```ruby
224
- Clockwork.configure do |config|
225
- config[:sleep_timeout] = 5
226
- config[:logger] = Logger.new(log_file_path)
227
- config[:tz] = 'EST'
228
- config[:max_threads] = 15
207
+ module Clockwork
208
+ configure do |config|
209
+ config[:sleep_timeout] = 5
210
+ config[:logger] = Logger.new(log_file_path)
211
+ config[:tz] = 'EST'
212
+ config[:max_threads] = 15
213
+ end
229
214
  end
230
215
  ```
231
216
 
@@ -234,8 +219,10 @@ end
234
219
  You can add error_handler to define your own logging or error rescue.
235
220
 
236
221
  ```ruby
237
- Clockwork.error_handler do |error|
238
- Airbrake.notify_or_ignore(error)
222
+ module Clockwork
223
+ error_handler do |error|
224
+ Airbrake.notify_or_ignore(error)
225
+ end
239
226
  end
240
227
  ```
241
228
 
@@ -246,6 +233,14 @@ Current specifications are as follows.
246
233
 
247
234
  Any suggestion about these specifications is welcome.
248
235
 
236
+ Old style
237
+ ---------
238
+
239
+ `include Clockwork` is old style.
240
+ That is still supported, but not recommended, because it taint global namespace.
241
+
242
+
243
+
249
244
  Anatomy of a clock file
250
245
  -----------------------
251
246
 
@@ -324,6 +319,28 @@ clockworkd -c YOUR_CLOCK.rb start
324
319
 
325
320
  For more details, see help shown by `clockworkd`.
326
321
 
322
+ Issues and Pull requests
323
+ ------------------------
324
+
325
+ Let us know bugs you found as an issue from [Issues · tomykaira/clockwork](https://github.com/tomykaira/clockwork/issues).
326
+
327
+ For a bug fix or a feature request, please send a pull-request.
328
+ Do not forget to add tests to show how your feature works, or what bug is fixed.
329
+ All existing tests and new tests must pass (TravisCI is watching).
330
+
331
+ We want to provide simple and customizable core, so superficial changes will not be merged (e.g. supporting new event registration style).
332
+ In most case, directly operating `Manager` realizes an idea, without touching the core.
333
+ If you get up with a new usage, please create a gist page or an article on your website, then add to the following "Use cases" section.
334
+ This tool is already used in various environment, so backward-incompatible requests will be mostly rejected.
335
+
336
+ Use cases
337
+ ---------
338
+
339
+ Feel free to add your idea or experience and send a pull-request.
340
+
341
+ - [Sending errors to Airbrake](https://github.com/tomykaira/clockwork/issues/58)
342
+ - [Read events from a database](https://github.com/tomykaira/clockwork/issues/25)
343
+
327
344
  Meta
328
345
  ----
329
346
 
@@ -48,6 +48,9 @@ opts = OptionParser.new do |opts|
48
48
  @options[:file] = "./#{@options[:file]}" unless @options[:file].match(/^[\/.]/)
49
49
  @options[:file] = File.expand_path(@options[:file])
50
50
  end
51
+ opts.on('-d', '--dir=DIR', 'Directory to change to once the process starts') do |dir|
52
+ @options[:current_dir] = File.expand_path(dir)
53
+ end
51
54
  end
52
55
 
53
56
  @args = opts.parse!(ARGV)
@@ -79,6 +82,9 @@ else
79
82
  end
80
83
 
81
84
  Daemons.run_proc(process_name, :dir => @options[:pid_dir], :dir_mode => :normal, :monitor => @options[:monitor], :log_dir => @options[:log_dir], :log_output => @options[:log_output], :ARGV => @args) do |*args|
85
+ # daemons changes the current working directory to '/' when a new process is
86
+ # forked. We change it back to the project root directory here.
87
+ Dir.chdir(@options[:current_dir]) if @options[:current_dir]
82
88
  require @options[:file]
83
89
 
84
90
  Clockwork::run
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "clockwork"
3
- s.version = "0.6.2"
3
+ s.version = "0.7.0"
4
4
 
5
5
  s.authors = ["Adam Wiggins", "tomykaira"]
6
6
  s.license = 'MIT'
data/example.rb CHANGED
@@ -1,16 +1,17 @@
1
1
  require 'clockwork'
2
- include Clockwork
3
2
 
4
- handler do |job|
5
- puts "Queueing job: #{job}"
6
- end
3
+ module Clockwork
4
+ handler do |job|
5
+ puts "Queueing job: #{job}"
6
+ end
7
7
 
8
- every(10.seconds, 'run.me.every.10.seconds')
9
- every(1.minute, 'run.me.every.minute')
10
- every(1.hour, 'run.me.every.hour')
8
+ every(10.seconds, 'run.me.every.10.seconds')
9
+ every(1.minute, 'run.me.every.minute')
10
+ every(1.hour, 'run.me.every.hour')
11
11
 
12
- every(1.day, 'run.me.at.midnight', :at => '00:00')
12
+ every(1.day, 'run.me.at.midnight', :at => '00:00')
13
13
 
14
- every(1.day, 'custom.event.handler', :at => '00:30') do
15
- puts 'This event has its own handler'
14
+ every(1.day, 'custom.event.handler', :at => '00:30') do
15
+ puts 'This event has its own handler'
16
+ end
16
17
  end
@@ -6,38 +6,52 @@ require 'clockwork/event'
6
6
  require 'clockwork/manager'
7
7
 
8
8
  module Clockwork
9
- extend self
10
-
11
- @@manager = Manager.new
12
-
13
- def configure(&block)
14
- @@manager.configure(&block)
9
+ class << self
10
+ def included(klass)
11
+ klass.send "include", Methods
12
+ klass.extend Methods
13
+ end
14
+
15
+ def manager
16
+ @manager ||= Manager.new
17
+ end
18
+
19
+ def manager=(manager)
20
+ @manager = manager
21
+ end
15
22
  end
16
23
 
17
- def handler(&block)
18
- @@manager.handler(&block)
19
- end
24
+ module Methods
25
+ def configure(&block)
26
+ Clockwork.manager.configure(&block)
27
+ end
20
28
 
21
- def error_handler(&block)
22
- @@manager.error_handler(&block)
23
- end
29
+ def handler(&block)
30
+ Clockwork.manager.handler(&block)
31
+ end
24
32
 
25
- def on(event, options={}, &block)
26
- @@manager.on(event, options, &block)
27
- end
33
+ def error_handler(&block)
34
+ Clockwork.manager.error_handler(&block)
35
+ end
28
36
 
29
- def every(period, job, options={}, &block)
30
- @@manager.every(period, job, options, &block)
31
- end
37
+ def on(event, options={}, &block)
38
+ Clockwork.manager.on(event, options, &block)
39
+ end
32
40
 
33
- def run
34
- @@manager.run
35
- end
41
+ def every(period, job, options={}, &block)
42
+ Clockwork.manager.every(period, job, options, &block)
43
+ end
44
+
45
+ def run
46
+ Clockwork.manager.run
47
+ end
36
48
 
37
- def clear!
38
- @@manager = Manager.new
49
+ def clear!
50
+ Clockwork.manager = Manager.new
51
+ end
39
52
  end
40
53
 
54
+ extend Methods
41
55
  end
42
56
 
43
57
  unless 1.respond_to?(:seconds)
@@ -48,7 +48,7 @@ module Clockwork
48
48
  end
49
49
 
50
50
  def execute
51
- @block.call(@job)
51
+ @block.call(@job, @last)
52
52
  rescue => e
53
53
  log_error e
54
54
  handle_error e
@@ -59,4 +59,17 @@ class ClockworkTest < Test::Unit::TestCase
59
59
  assert string_io.string.include?('1 events')
60
60
  assert !string_io.string.include?('Triggering')
61
61
  end
62
+
63
+ test 'support module re-open style' do
64
+ $called = false
65
+ module ::Clockwork
66
+ every(1.second, 'myjob') { $called = true }
67
+ end
68
+ set_string_io_logger
69
+ runner = run_in_thread
70
+ sleep 1
71
+ runner.kill
72
+
73
+ assert $called
74
+ end
62
75
  end
@@ -89,6 +89,23 @@ class ManagerTest < Test::Unit::TestCase
89
89
  assert_equal 2, $set_me
90
90
  end
91
91
 
92
+ test "should pass time to the general handler" do
93
+ received = nil
94
+ now = Time.now
95
+ @manager.handler { |job, time| received = time }
96
+ @manager.every(1.minute, 'myjob')
97
+ @manager.tick(now)
98
+ assert_equal now, received
99
+ end
100
+
101
+ test "should pass time to the event-specific handler" do
102
+ received = nil
103
+ now = Time.now
104
+ @manager.every(1.minute, 'myjob') { |job, time| received = time }
105
+ @manager.tick(now)
106
+ assert_equal now, received
107
+ end
108
+
92
109
  test "exceptions are trapped and logged" do
93
110
  @manager.handler { raise 'boom' }
94
111
  @manager.every(1.minute, 'myjob')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clockwork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Wiggins
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-19 00:00:00.000000000 Z
12
+ date: 2013-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: tzinfo