bigben 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +15 -0
  2. data/lib/bigben.rb +57 -0
  3. data/lib/bigben/version.rb +3 -0
  4. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTFjNmY1YjQzMDhiN2EzNTExNTZiOGI1Njg3MWIxMGU5MTcxYTAzOA==
5
+ data.tar.gz: !binary |-
6
+ YjVkZGFkMjZiZGQ5YWJiNjkxNjQ0YjNhYmMyYzkzN2RhZGFkYjMzZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ODU2Yzg5ZmZiODI5ZGRmYTJjOWFiOTQ5NzE5N2NkMTJkN2ZlOWQ0MTEwYzY0
10
+ YjFkNWRlYTY5NTNjNjU4YzQ5MTE1MGE5OThjNzcyYjk4NzE1NzkyMjUzY2Y4
11
+ OWQwOGJjNWZhZGJjMDE4NGRkODQ0ZWRiYjEwOGFlMzI4OTU0NDY=
12
+ data.tar.gz: !binary |-
13
+ ZWFmODk2YmJlNWMyOWE1ODFlNDJmZjNiMjQzYmVhZWY1NzZmMTUzNDNhZGFi
14
+ MDUzODA4ZDRmMzg4YmM0Mjc3MjAyZmI2ZDUyYjI4ZGFmODRmZDVkOTAwYzMw
15
+ ZWQzYzkwNmQxMWE5MmUxMWEyYWUwMGVmOTNlNDg1OTVmMjQ0NWY=
data/lib/bigben.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'active_support/core_ext'
2
+
3
+ class BigBen
4
+ cattr_accessor :notifier
5
+ cattr_accessor :logger
6
+
7
+ def initialize(name, duration, &block)
8
+ @name = name
9
+ @duration = duration.to_f
10
+ @block = block
11
+ @lock = Mutex.new
12
+ end
13
+
14
+ def run_callback
15
+ @block.call
16
+ rescue Exception => e
17
+ # Report the exception only once
18
+ unless @last_exception == e.to_s
19
+ @last_exception = e.to_s
20
+
21
+ self.logger.warn "[#{@name}] #{e}" if self.logger
22
+ self.notifier.call(e) if self.notifier
23
+ end
24
+ end
25
+
26
+ def main_loop(options={})
27
+ Thread.current[:bigben_instance] = self
28
+ options = options.dup
29
+
30
+ loop do
31
+ sleep @duration unless options.delete(:run_immediately)
32
+ @lock.synchronize do
33
+ return unless @thread
34
+ run_callback
35
+ end
36
+ end
37
+ end
38
+
39
+ def start(options={})
40
+ @lock.synchronize do
41
+ @thread ||= Thread.new { main_loop(options) }
42
+ end
43
+ end
44
+
45
+ def reset
46
+ if Thread.current[:bigben_instance] == self
47
+ # We already hold the lock (the callback called reset)
48
+ @thread = nil
49
+ else
50
+ @lock.synchronize do
51
+ @thread.try(:kill)
52
+ @thread = nil
53
+ end
54
+ end
55
+ @last_exception = nil
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ class BigBen
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bigben
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kareem Kouddous
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: Threaded timer
28
+ email:
29
+ - kareeknyc@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/bigben/version.rb
35
+ - lib/bigben.rb
36
+ homepage: http://github.com/crowdtap/bigben
37
+ licenses: []
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.0.3
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Threaded timer
59
+ test_files: []
60
+ has_rdoc: false