foreverb 0.2.0 → 0.2.1

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/README.md CHANGED
@@ -98,32 +98,58 @@ $ bin/foo stop
98
98
 
99
99
  ## Scheduling
100
100
 
101
- You can use ```every``` method to schedule repetitive tasks.
101
+ You can use `every` method to schedule repetitive tasks.
102
+
103
+ Every allow the option `:at` to specify hour or minute and the option `:last` to specify when the `every` must start to loop.
104
+
105
+ `:last`: can be nil or a Time class. Default is 0.<br />
106
+ `:at`: can be nil, a string or an array of formatted strings. Default is nil.
107
+
108
+ ``` rb
109
+ every 1.second, :at => '19:30' # => every second since 19:30
110
+ every 1.minute, :at => ':30' # => every minute but first call wait xx:30
111
+ every 5.minutes, :at => '18:' # => every five minutes but first call was at 18:xx
112
+ every 1.day, :at => ['18:30', '20:30'] # => every day only at 18:30 and 20:30
113
+ every 60.seconds, :last => Time.now # => will be fired 60 seconds after you launch the app
114
+ ```
115
+
116
+ Remember that `:at`:
117
+
118
+ * accept only 24h format
119
+ * you must always provide the colon `:`
120
+
121
+ So looking our [example](https://github.com/DAddYE/foreverb/blob/master/examples/sample):
102
122
 
103
123
  ``` rb
104
- # Taken from, examples/sample
105
124
  Forever.run do
106
125
  dir File.expand_path('../', __FILE__) # Default is ../../__FILE__
107
126
 
108
127
  on_ready do
109
- puts inspect
128
+ puts "All jobs will will wait me for 1 second"; sleep 1
110
129
  end
111
130
 
112
- every 1.seconds do
113
- puts "Every one seconds"
131
+ every 10.seconds, :at => "#{Time.now.hour}:00" do
132
+ puts "Every 10 seconds but first call at #{Time.now.hour}:00"
114
133
  end
115
134
 
116
- every 2.seconds do
117
- puts "Every two seconds"
135
+ every 1.seconds, :at => "#{Time.now.hour}:#{Time.now.min+1}" do
136
+ puts "Every one second but first call at #{Time.now.hour}:#{Time.now.min}"
118
137
  end
119
138
 
120
- every 3.seconds do
121
- puts "Every three seconds, long task"
122
- sleep 10
139
+ every 10.seconds do
140
+ puts "Every 10 second"
123
141
  end
124
142
 
125
- every 1.day, :at => "18:28" do
126
- puts "Every day at 18:28"
143
+ every 20.seconds do
144
+ puts "Every 20 second"
145
+ end
146
+
147
+ every 15.seconds do
148
+ puts "Every 15 seconds, but my task require 10 seconds"; sleep 10
149
+ end
150
+
151
+ every 10.seconds, :at => [":#{Time.now.min+1}", ":#{Time.now.min+2}"] do
152
+ puts "Every 10 seconds but first call at xx:#{Time.now.min}"
127
153
  end
128
154
 
129
155
  on_error do |e|
@@ -136,32 +162,49 @@ Forever.run do
136
162
  end
137
163
  ```
138
164
 
139
- You should see in logs this:
165
+ Running the example with the following code:
166
+
167
+ ``` sh
168
+ $ examples/sample; tail -f -n 150 examples/log/sample.log; examples/sample stop
169
+ ```
170
+
171
+ you should see:
140
172
 
141
173
  ```
142
- $ examples/sample
143
174
  => Pid not found, process seems don't exist!
144
- => Process demonized with pid 1252 with Forever v.0.1.7
145
- [11/07 18:27:08] #<Forever dir:/Developer/src/extras/foreverb/examples, file:/Developer/src/extras/foreverb/examples/sample, log:/Developer/src/extras/foreverb/examples/log/sample.log, pid:/Developer/src/extras/foreverb/examples/tmp/sample.pid jobs:4>
146
- [11/07 18:27:08] Every one seconds
147
- [11/07 18:27:08] Every two seconds
148
- [11/07 18:27:08] Every three seconds, long task
149
- [11/07 18:27:09] Every one seconds
150
- [11/07 18:27:10] Every two seconds
151
- ...
152
- [11/07 18:27:17] Every one seconds
153
- [11/07 18:27:18] Every one seconds
154
- [11/07 18:27:18] Every two seconds
155
- [11/07 18:27:19] Every three seconds, long task
175
+ => Process demonized with pid 11509 with Forever v.0.2.0
176
+ [14/07 15:46:56] All jobs will will wait me for 1 second
177
+ [14/07 15:46:57] Every 10 second
178
+ [14/07 15:46:57] Every 20 second
179
+ [14/07 15:46:57] Every 15 seconds, but my task require 10 seconds
180
+ [14/07 15:47:00] Every one second but first call at 15:47
181
+ [14/07 15:47:00] Every 10 seconds but first call at xx:47
182
+ [14/07 15:47:01] Every one second but first call at 15:47
183
+ [14/07 15:47:02] Every one second but first call at 15:47
184
+ [14/07 15:47:03] Every one second but first call at 15:47
185
+ [14/07 15:47:04] Every one second but first call at 15:47
186
+ [14/07 15:47:05] Every one second but first call at 15:47
187
+ [14/07 15:47:06] Every one second but first call at 15:47
188
+ [14/07 15:47:07] Every 10 second
189
+ [14/07 15:47:07] Every one second but first call at 15:47
190
+ [14/07 15:47:08] Every one second but first call at 15:47
191
+ [14/07 15:47:09] Every one second but first call at 15:47
192
+ [14/07 15:47:10] Every 10 seconds but first call at xx:47
193
+ [14/07 15:47:10] Every one second but first call at 15:47
194
+ [14/07 15:47:11] Every one second but first call at 15:47
195
+ [14/07 15:47:12] Every 15 seconds, but my task require 10 seconds
156
196
  ...
157
- [11/07 18:27:58] Every one seconds
158
- [11/07 18:27:59] Every one seconds
159
- [11/07 18:28:00] Every two seconds
160
- [11/07 18:28:00] Every one seconds
161
- [11/07 18:28:00] Every day at 18:28
162
- => Found pid 1252...
163
- => Killing process 1252...
164
- [11/07 18:28:18] Bye bye
197
+ [14/07 15:47:42] Every 15 seconds, but my task require 10 seconds
198
+ [14/07 15:47:42] Every one second but first call at 15:47
199
+ [14/07 15:47:43] Every one second but first call at 15:47
200
+ [14/07 15:47:44] Every one second but first call at 15:47
201
+ [14/07 15:47:45] Every one second but first call at 15:47
202
+ [14/07 15:47:46] Every one second but first call at 15:47
203
+ [14/07 15:47:47] Every 10 second
204
+ ^C
205
+ => Found pid 11509...
206
+ => Killing process 11509...
207
+ [14/07 15:48:40] Bye bye
165
208
  ```
166
209
 
167
210
  ## Monitor your daemon(s):
@@ -194,4 +237,4 @@ To see a most comprensive app running _foreverb_ + _growl_ see [githubwatcher ge
194
237
 
195
238
  ## Author
196
239
 
197
- DAddYE, you can follow me on twitter [@daddye](http://twitter.com/daddye)
240
+ DAddYE, you can follow me on twitter [@daddye](http://twitter.com/daddye) or take a look at my site [daddye.it](http://www.daddye.it)
@@ -7,24 +7,40 @@ Forever.run do
7
7
  dir File.expand_path('../', __FILE__) # Default is ../../__FILE__
8
8
 
9
9
  on_ready do
10
- puts inspect
10
+ puts "All jobs will will wait me for 1 second"; sleep 1
11
11
  end
12
12
 
13
- every 1.seconds do
14
- puts "Every one seconds"
13
+ every 5.seconds, :last => Time.now do
14
+ puts "Every 5 seconds from start"
15
15
  end
16
16
 
17
- every 2.seconds do
18
- puts "Every two seconds"
17
+ every 30.seconds, :last => Time.now do
18
+ puts "Every 30 seconds from start with boom"
19
+ raise "woooooa"
19
20
  end
20
21
 
21
- every 3.seconds do
22
- puts "Every three seconds, long task"
23
- sleep 10
22
+ every 10.seconds, :at => "#{Time.now.hour}:00" do
23
+ puts "Every 10 seconds but first call at #{Time.now.hour}:00"
24
24
  end
25
25
 
26
- every 1.minutes, :at => "%d:%d" % [Time.now.hour, Time.now.min+1] do
27
- puts "Every minutes at #{"%d:%d" % [Time.now.hour, Time.now.min]}"
26
+ every 1.seconds, :at => "#{Time.now.hour}:#{Time.now.min+1}" do
27
+ puts "Every one second but first call at #{Time.now.hour}:#{Time.now.min}"
28
+ end
29
+
30
+ every 10.seconds do
31
+ puts "Every 10 second"
32
+ end
33
+
34
+ every 20.seconds do
35
+ puts "Every 20 second"
36
+ end
37
+
38
+ every 15.seconds do
39
+ puts "Every 15 seconds, but my task require 10 seconds"; sleep 10
40
+ end
41
+
42
+ every 10.seconds, :at => [":#{Time.now.min+1}", ":#{Time.now.min+2}"] do
43
+ puts "Every 10 seconds but first call at xx:#{Time.now.min}"
28
44
  end
29
45
 
30
46
  on_error do |e|
@@ -36,7 +36,7 @@ module Forever
36
36
  safe_call(on_ready) if on_ready
37
37
  jobs.each do |job|
38
38
  threads << Thread.new do
39
- loop { safe_call(job) if job.time?(Time.now); sleep 1 }
39
+ loop { job.time?(Time.now) ? safe_call(job) : sleep(1) }
40
40
  end
41
41
  end
42
42
  threads.map(&:join)
@@ -139,8 +139,6 @@ module Forever
139
139
  rescue Exception => e
140
140
  puts "\n\n%s\n %s\n\n" % [e.message, e.backtrace.join("\n ")]
141
141
  on_error[e] if on_error
142
- sleep 30
143
- retry
144
142
  end
145
143
  end
146
144
  end # Base
@@ -4,8 +4,9 @@ module Forever
4
4
  attr_accessor :period, :option, :last, :running
5
5
 
6
6
  def initialize(period, options, block)
7
- @period, @options, @last, @running = period, options, 0, false
8
- @at = parse_at(@options[:at])
7
+ @period, @options, @running = period, options, false
8
+ @at = options[:at] ? parse_at(*@options[:at]) : []
9
+ @last = options[:last].to_i
9
10
  @block = block
10
11
  end
11
12
 
@@ -18,18 +19,19 @@ module Forever
18
19
 
19
20
  def time?(t)
20
21
  ellapsed_ready = (t - @last).to_i >= @period
21
- time_ready = @at.nil? || (t.hour == @at[0] && t.min == @at[1])
22
+ time_ready = @at.empty? || @at.any? { |at| (at[0].empty? || t.hour == at[0].to_i) && (at[1].empty? || t.min == at[1].to_i) }
22
23
  !running && ellapsed_ready && time_ready
23
24
  end
24
25
 
25
26
  private
26
- def parse_at(at)
27
- return unless at.is_a?(String)
28
- m = at.match(/^(\d{1,2}):(\d{1,2})$/)
29
- raise "Failed to parse #{at}" unless m
30
- hour, min = m[1].to_i, m[2].to_i
31
- raise "Failed to parse #{at}" if hour >= 24 || min >= 60
32
- [hour, min]
27
+ def parse_at(*args)
28
+ args.map do |at|
29
+ raise "#{at} must be a string" unless at.is_a?(String)
30
+ raise "#{at} has not a colon separator" unless at =~ /:/
31
+ hour, min = at.split(":")
32
+ raise "Failed to parse #{at}" if hour.to_i >= 24 || min.to_i >= 60
33
+ [hour, min]
34
+ end
33
35
  end
34
36
  end # Job
35
37
 
@@ -1,3 +1,3 @@
1
1
  module Forever
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreverb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - DAddYE
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-13 00:00:00 +02:00
18
+ date: 2011-07-14 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency