foreverb 0.3.0.b → 0.3.0.c

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,5 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
- examples/**/log
6
- examples/**/tmp
5
+ examples/log
6
+ examples/tmp
data/Rakefile CHANGED
@@ -31,7 +31,8 @@ namespace :example do
31
31
  desc "Run example #{name}"
32
32
  task name, :fork do |t, args|
33
33
  ENV['FORK'] = args[:fork]
34
- exec "#{Gem.ruby} #{path} && sleep 3 && tail -f -n 150 #{path}/../log/#{name}.log; #{path} stop"
34
+ log = File.expand_path("../log/#{name}.log", path)
35
+ exec "#{Gem.ruby} #{path} && sleep 5 && tail -f -n 150 #{log}; #{path} stop"
35
36
  end
36
37
  end
37
38
  end
data/examples/simple CHANGED
@@ -5,6 +5,7 @@ require 'forever'
5
5
 
6
6
  Forever.run :fork => ENV['FORK'] do
7
7
  dir File.expand_path('../', __FILE__) # Default is ../../__FILE__
8
+ queue 2
8
9
 
9
10
  before :each do
10
11
  puts 'before all'
@@ -15,7 +16,8 @@ Forever.run :fork => ENV['FORK'] do
15
16
  end
16
17
 
17
18
  every 1.seconds do
18
- puts 'wait me 10 seconds'; sleep 10
19
+ puts 'wait me 10 seconds'
20
+ sleep 10
19
21
  end
20
22
 
21
23
  every 2.seconds do
data/lib/forever/base.rb CHANGED
@@ -17,8 +17,8 @@ module Forever
17
17
 
18
18
  # Setup directories
19
19
  Dir.chdir(dir)
20
- FileUtils.rm_rf(tmp) if File.exist?(tmp)
21
- Dir.mkdir(tmp)
20
+ clean_tmp!
21
+ Dir.mkdir(tmp) unless File.exist?(tmp)
22
22
  Dir.mkdir(File.dirname(log)) if log && !File.exist?(File.dirname(log))
23
23
 
24
24
  write_config!
@@ -79,15 +79,16 @@ module Forever
79
79
  @started_at = Time.now
80
80
 
81
81
  # Invoke our before :all filters
82
- before_filters[:all].each { |block| safe_call(block) }
82
+ filters[:before][:all].each { |block| safe_call(block) }
83
83
 
84
84
  # Start deamons
85
85
  until stopping?
86
86
  current_queue = 1
87
- jobs.select { |job| job.time?(Time.now) }.each do |job|
88
- if queue && queue > current_queue
89
- puts "\n\nThe queue limit has been exceeded\n\n"
90
- sleep 60
87
+ jobs.each do |job|
88
+ next unless job.time?(Time.now)
89
+ if queue && current_queue > queue
90
+ puts "\n\nThe queue limit has been exceeded. You are using #{current_queue} of #{queue} slots.\n\n"
91
+ on_limit_exceeded ? on_limit_exceeded.call : sleep(60)
91
92
  break
92
93
  end
93
94
  if forking
@@ -102,13 +103,14 @@ module Forever
102
103
  end
103
104
  current_queue += 1
104
105
  end
106
+ sleep 0.5
105
107
  end
106
108
 
107
109
  # Invoke our after :all filters
108
- after_filters[:all].each { |block| safe_call(block) }
110
+ filters[:after][:all].each { |block| safe_call(block) }
109
111
 
110
112
  # If we are here it means we are exiting so we can remove the pid and pending stop.txt
111
- FileUtils.rm_rf(tmp)
113
+ clean_tmp!
112
114
  end
113
115
 
114
116
  self
@@ -197,8 +199,8 @@ module Forever
197
199
  if running?
198
200
  pid_was = File.read(pid).to_i
199
201
  print "[\e[90m%s\e[0m] Killing process \e[1m%d\e[0m...\n" % [name, pid_was]
200
- after_filters[:all].each { |block| safe_call(block) }
201
- FileUtils.rm_rf(tmp)
202
+ filters[:after][:all].each { |block| safe_call(block) }
203
+ clean_tmp!
202
204
  Process.kill(:KILL, pid_was)
203
205
  else
204
206
  print "[\e[90m%s\e[0m] Process with \e[1mnot found\e[0m" % name
@@ -227,6 +229,13 @@ module Forever
227
229
  block_given? ? @_on_error = block : @_on_error
228
230
  end
229
231
 
232
+ ##
233
+ # Callback raised when queue limit was exceeded
234
+ #
235
+ def on_limit_exceeded(&block)
236
+ block_given? ? @_on_limit_exceeded = block : @_on_limit_exceeded
237
+ end
238
+
230
239
  ##
231
240
  # Callback raised when at exit
232
241
  #
@@ -262,32 +271,43 @@ module Forever
262
271
  is_running
263
272
  end
264
273
 
265
- def to_s
266
- "#<Forever dir:#{dir}, file:#{file}, log:#{log}, pid:#{pid} jobs:#{jobs.size}>"
267
- end
268
- alias :inspect :to_s
269
-
270
- def config
271
- { :dir => dir, :file => file, :log => log, :pid => pid }
272
- end
273
-
274
+ ##
275
+ # Before :all or :each jobs hook
276
+ #
274
277
  def before(filter, &block)
275
278
  raise "Filter #{filter.inspect} not supported, available options are: :each, :all" unless [:each, :all].include?(filter)
276
- before_filters[filter] << block
279
+ filters[:before][filter] << block
277
280
  end
278
281
 
282
+ ##
283
+ # After :all or :each jobs hook
284
+ #
279
285
  def after(filter, &block)
280
286
  raise "Filter #{filter.inspect} not supported, available options are: :each, :all" unless [:each, :all].include?(filter)
281
- after_filters[filter] << block
287
+ filters[:after][filter] << block
282
288
  end
283
289
 
284
- private
285
- def before_filters
286
- @_before_filters ||= Hash.new { |hash, k| hash[k] = [] }
290
+ ##
291
+ # Return config of current worker in a hash
292
+ #
293
+ def config
294
+ { :dir => dir, :file => file, :log => log, :pid => pid }
295
+ end
296
+
297
+ ##
298
+ # Convert forever object in a readable string showing current config
299
+ #
300
+ def to_s
301
+ "#<Forever dir:#{dir}, file:#{file}, log:#{log}, pid:#{pid} jobs:#{jobs.size}>"
287
302
  end
303
+ alias :inspect :to_s
288
304
 
289
- def after_filters
290
- @_after_filters ||= Hash.new { |hash, k| hash[k] = [] }
305
+ private
306
+ def filters
307
+ @_filters ||= {
308
+ :before => { :each => [], :all => [] },
309
+ :after => { :each => [], :all => [] }
310
+ }
291
311
  end
292
312
 
293
313
  def stopping?
@@ -308,9 +328,9 @@ module Forever
308
328
  def job_call(job)
309
329
  return unless job.time?(Time.now)
310
330
  job.run!
311
- before_filters[:each].each { |block| safe_call(block) }
331
+ filters[:before][:each].each { |block| safe_call(block) }
312
332
  safe_call(job)
313
- after_filters[:each].each { |block| safe_call(block) }
333
+ filters[:after][:each].each { |block| safe_call(block) }
314
334
  ensure
315
335
  job.stop!
316
336
  end
@@ -327,5 +347,11 @@ module Forever
327
347
  def stop_txt
328
348
  @_stop_txt ||= File.join(tmp, 'stop.txt')
329
349
  end
350
+
351
+ def clean_tmp!
352
+ return unless File.exist?(tmp)
353
+ Dir[File.join(tmp, '*.job')].each { |f| FileUtils.rm_rf(f) }
354
+ FileUtils.rm_rf(pid)
355
+ end
330
356
  end # Base
331
357
  end # Forever
@@ -1,3 +1,3 @@
1
1
  module Forever
2
- VERSION = "0.3.0.b" unless defined?(Forever::VERSION)
2
+ VERSION = "0.3.0.c" unless defined?(Forever::VERSION)
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreverb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
4
+ hash: 48
5
5
  prerelease: 6
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
9
  - 0
10
- - b
11
- version: 0.3.0.b
10
+ - c
11
+ version: 0.3.0.c
12
12
  platform: ruby
13
13
  authors:
14
14
  - DAddYE