zhong 0.1.2 → 0.1.3

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: 58363ac3ea11e4bb20aa41811b6242421c858777
4
- data.tar.gz: 953b201c5d195b2994ab3445b8ccfa1459d93d74
3
+ metadata.gz: 5945fa0ab193d6a729fdf393166c8d380a06558e
4
+ data.tar.gz: 8956f5b216934a2ebc5b9014c53b655c46ff462a
5
5
  SHA512:
6
- metadata.gz: 5ff2cca40da90cc77edd36aeb921e11a6afaf41bcdff80225ea4e3b859ab1af5394664b8aa167da20fe350ff1d623bfa1f2ab660c95530f811db6345f723fc5e
7
- data.tar.gz: 755a8b2ee5bb970a1d54b43977237f647fc1d1258cc4e7a0980ccacb37d356f5104b16cc99a6742304e7b7d11f92a859c2e3ea3a5e8804a5169c23bdec5b32f3
6
+ metadata.gz: 62629516856da87db6bdf7a54dc616efe247b7ce29e039d989ee0b69a7fa9015fb2d43a15b651aea30c604942ce2b2fd0aeecd1961fe63bb9d201bfdbfc0e428
7
+ data.tar.gz: 7afeb0d7ee86d65eed6964c03f161730bd4050ce5d70c1ce86d137d9ae0f22a95e5ff91a0f10a87080d6e192d399afc0664427ac8bfa231bcc4313c3cc437134
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.3
2
+
3
+ - Fix several memory leaks.
4
+ - Add a proper error handler block.
5
+
1
6
  ## 0.1.2
2
7
 
3
8
  - Fix bug with setting `at` like "**:35".
data/README.md CHANGED
@@ -20,11 +20,15 @@ r = Redis.new
20
20
  Zhong.schedule(redis: r) do |s|
21
21
  s.category "stuff" do
22
22
  s.every(5.seconds, "foo") { puts "foo" }
23
+ s.every(1.minute, "biz", at: ["**:26", "**:27"]) { puts "biz" }
23
24
  s.every(1.week, "baz", at: ["mon 22:45", "wed 23:13"]) { puts "baz" }
25
+ s.every(10.seconds, "boom") { raise "fail" }
24
26
  end
25
27
 
26
28
  s.category "clutter" do
27
- s.every(1.second, "compute", if: -> (t) { t.wday == 3 && rand < 0.5 }) { puts "something happened on wednesday" }
29
+ s.every(1.second, "compute", if: -> (t) { t.wday == 3 && rand < 0.5 }) do
30
+ puts "something happened on wednesday, maybe"
31
+ end
28
32
  end
29
33
 
30
34
  # note: callbacks that return nil or false will cause event to not run
@@ -37,8 +41,11 @@ Zhong.schedule(redis: r) do |s|
37
41
  puts "dong"
38
42
  true
39
43
  end
40
- end
41
44
 
45
+ s.error_handler do |e, job|
46
+ puts "damn, #{job} messed up: #{e}"
47
+ end
48
+ end
42
49
  ```
43
50
 
44
51
  ## TODO
data/bin/setup CHANGED
@@ -3,5 +3,3 @@ set -euo pipefail
3
3
  IFS=$'\n\t'
4
4
 
5
5
  bundle install
6
-
7
- # Do any other automated setup that you need to do here
data/bin/zhong CHANGED
@@ -4,6 +4,7 @@ STDERR.sync = STDOUT.sync = true
4
4
 
5
5
  require "bundler/setup"
6
6
  require "zhong"
7
+ require "memory_profiler"
7
8
 
8
9
  usage = "zhong <zhong.rb>"
9
10
  file = ARGV.shift || abort(usage)
@@ -12,8 +13,4 @@ file = "./#{file}" unless file.match(/^[\/.]/)
12
13
 
13
14
  require file
14
15
 
15
- require "irb"
16
- IRB.start
17
-
18
-
19
16
  Zhong.start
data/lib/zhong/job.rb CHANGED
@@ -31,7 +31,7 @@ module Zhong
31
31
  run_every?(time) && run_at?(time) && run_if?(time)
32
32
  end
33
33
 
34
- def run(time = Time.now)
34
+ def run(time = Time.now, error_handler = nil)
35
35
  return unless run?(time)
36
36
 
37
37
  if running?
@@ -40,25 +40,41 @@ module Zhong
40
40
  end
41
41
 
42
42
  @thread = nil
43
+ locked = false
44
+
45
+ @lock.lock do
46
+ locked = true
43
47
 
44
- ran_set = @lock.lock do
45
48
  refresh_last_ran
46
49
 
47
- break true unless run?(time)
50
+ # we need to check again, as another process might have acquired
51
+ # the lock right before us and obviated our need to do anything
52
+ break unless run?(time)
48
53
 
49
54
  if disabled?
50
55
  @logger.info "disabled: #{self}"
51
- break true
56
+ break
52
57
  end
53
58
 
54
59
  @logger.info "running: #{self}"
55
60
 
56
- @thread = Thread.new { @block.call } if @block
61
+ if @block
62
+ @thread = Thread.new do
63
+ begin
64
+ @block.call
65
+ rescue => boom
66
+ @logger.error "#{self} failed: #{boom}"
67
+ error_handler.call(boom, self) if error_handler
68
+ end
69
+
70
+ nil # do not retain thread return value
71
+ end
72
+ end
57
73
 
58
74
  ran!(time)
59
75
  end
60
76
 
61
- @logger.info "unable to acquire exclusive run lock: #{self}" unless ran_set
77
+ @logger.info "unable to acquire exclusive run lock: #{self}" unless locked
62
78
  end
63
79
 
64
80
  def stop
@@ -39,7 +39,7 @@ module Zhong
39
39
  end
40
40
 
41
41
  def on(event, &block)
42
- fail "Unsupported callback #{event}" unless [:before_tick, :after_tick, :before_run, :after_run].include?(event.to_sym)
42
+ fail "unknown callback #{event}" unless [:before_tick, :after_tick, :before_run, :after_run].include?(event.to_sym)
43
43
  (@callbacks[event.to_sym] ||= []) << block
44
44
  end
45
45
 
@@ -56,13 +56,15 @@ module Zhong
56
56
 
57
57
  jobs.each do |_, job|
58
58
  if fire_callbacks(:before_run, job, now)
59
- job.run(now)
59
+ job.run(now, error_handler)
60
60
  fire_callbacks(:after_run, job, now)
61
61
  end
62
62
  end
63
63
 
64
64
  fire_callbacks(:after_tick)
65
65
 
66
+ GC.start
67
+
66
68
  sleep(interval)
67
69
  end
68
70
 
data/lib/zhong/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Zhong
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/zhong.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency "redis"
27
27
  spec.add_dependency "tzinfo"
28
28
  spec.add_dependency "activesupport"
29
+ spec.add_dependency "memory_profiler"
29
30
 
30
31
  spec.add_development_dependency "bundler", "~> 1.5"
31
32
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zhong
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Elser
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: memory_profiler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: bundler
71
85
  requirement: !ruby/object:Gem::Requirement