foreverb 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/sample +0 -7
- data/lib/forever.rb +2 -0
- data/lib/forever/base.rb +17 -28
- data/lib/forever/every.rb +0 -2
- data/lib/forever/extensions.rb +25 -0
- data/lib/forever/version.rb +1 -1
- metadata +4 -4
- data/lib/forever/numeric.rb +0 -13
data/examples/sample
CHANGED
@@ -3,14 +3,7 @@ require 'rubygems' unless defined?(Gem)
|
|
3
3
|
require 'bundler/setup'
|
4
4
|
require 'forever'
|
5
5
|
|
6
|
-
$stdout_was = STDOUT.dup
|
7
|
-
|
8
6
|
Forever.run do
|
9
|
-
def puts(text=nil)
|
10
|
-
text = super(text)
|
11
|
-
$stdout_was.print text
|
12
|
-
end
|
13
|
-
|
14
7
|
dir File.expand_path('../', __FILE__) # Default is ../../__FILE__
|
15
8
|
|
16
9
|
on_ready do
|
data/lib/forever.rb
CHANGED
data/lib/forever/base.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'fileutils'
|
2
|
-
require 'forever/every'
|
3
2
|
|
4
3
|
module Forever
|
5
|
-
DATE_FORMAT = "%d/%m %H:%M:%S"
|
6
4
|
|
7
5
|
class Base
|
8
6
|
include Every
|
@@ -34,37 +32,17 @@ module Forever
|
|
34
32
|
STDOUT.reopen(stream)
|
35
33
|
STDERR.reopen(STDOUT)
|
36
34
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
threads
|
41
|
-
|
42
|
-
jobs.each do |job|
|
43
|
-
threads << Thread.new do
|
44
|
-
loop { job.call if job.time?(Time.now); sleep 1 }
|
45
|
-
end
|
35
|
+
threads = []
|
36
|
+
threads << Thread.new { safe_call(on_ready) } if on_ready
|
37
|
+
jobs.each do |job|
|
38
|
+
threads << Thread.new do
|
39
|
+
loop { safe_call(job) if job.time?(Time.now); sleep 1 }
|
46
40
|
end
|
47
|
-
threads.map(&:join)
|
48
|
-
rescue Exception => e
|
49
|
-
on_error[e] if on_error
|
50
|
-
stream.print "\n\n%s\n %s\n\n" % [e.message, e.backtrace.join("\n ")]
|
51
|
-
sleep 30
|
52
|
-
retry
|
53
41
|
end
|
42
|
+
threads.map(&:join)
|
54
43
|
end
|
55
44
|
end
|
56
45
|
|
57
|
-
##
|
58
|
-
# A replacement of puts that has the aim to works correclty with multiple threads
|
59
|
-
# and log date. If you want to personalize date, edit DATE_FORMAT.
|
60
|
-
#
|
61
|
-
def puts(text="")
|
62
|
-
text = "[%s] %s" % [Time.now.strftime(DATE_FORMAT), text.to_s]
|
63
|
-
text += "\n" unless text[-1] == ?\n
|
64
|
-
print text; $stdout.flush
|
65
|
-
text
|
66
|
-
end
|
67
|
-
|
68
46
|
##
|
69
47
|
# Caller file
|
70
48
|
#
|
@@ -154,5 +132,16 @@ module Forever
|
|
154
132
|
def exists?(*values)
|
155
133
|
values.all? { |value| value && File.exist?(value) }
|
156
134
|
end
|
135
|
+
|
136
|
+
def safe_call(block)
|
137
|
+
begin
|
138
|
+
block.call
|
139
|
+
rescue Exception => e
|
140
|
+
on_error[e] if on_error
|
141
|
+
puts "\n\n%s\n %s\n\n" % [e.message, e.backtrace.join("\n ")]
|
142
|
+
sleep 30
|
143
|
+
retry
|
144
|
+
end
|
145
|
+
end
|
157
146
|
end # Base
|
158
147
|
end # Forever
|
data/lib/forever/every.rb
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
LOG_FORMAT = "[%s] %s" unless defined?(LOG_FORMAT)
|
2
|
+
DATE_FORMAT = "%d/%m %H:%M:%S" unless defined?(DATE_FORMAT)
|
3
|
+
|
4
|
+
class Numeric
|
5
|
+
def seconds; self; end
|
6
|
+
alias :second :seconds
|
7
|
+
|
8
|
+
def minutes; self * 60; end
|
9
|
+
alias :minute :minutes
|
10
|
+
|
11
|
+
def hours; self * 3600; end
|
12
|
+
alias :hour :hours
|
13
|
+
|
14
|
+
def days; self * 86400; end
|
15
|
+
alias :day :days
|
16
|
+
end
|
17
|
+
|
18
|
+
module Kernel
|
19
|
+
def puts(text="")
|
20
|
+
text = LOG_FORMAT % [Time.now.strftime(DATE_FORMAT), text.to_s]
|
21
|
+
text += "\n" unless text[-1] == ?\n
|
22
|
+
print text; $stdout.flush
|
23
|
+
text
|
24
|
+
end
|
25
|
+
end
|
data/lib/forever/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 9
|
9
|
+
version: 0.1.9
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- DAddYE
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-07-
|
17
|
+
date: 2011-07-12 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -51,7 +51,7 @@ files:
|
|
51
51
|
- lib/forever.rb
|
52
52
|
- lib/forever/base.rb
|
53
53
|
- lib/forever/every.rb
|
54
|
-
- lib/forever/
|
54
|
+
- lib/forever/extensions.rb
|
55
55
|
- lib/forever/version.rb
|
56
56
|
has_rdoc: true
|
57
57
|
homepage: https://github.com/daddye/forever
|
data/lib/forever/numeric.rb
DELETED