clockwork 0.5.0 → 0.5.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 +14 -2
 - data/VERSION +1 -1
 - data/lib/clockwork.rb +27 -2
 - data/test/clockwork_test.rb +19 -1
 - metadata +3 -3
 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,4 +1,4 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            Clockwork - a clock process to replace cron
         
     | 
| 
      
 1 
     | 
    
         
            +
            Clockwork - a clock process to replace cron [](http://travis-ci.org/tomykaira/clockwork)
         
     | 
| 
       2 
2 
     | 
    
         
             
            ===========================================
         
     | 
| 
       3 
3 
     | 
    
         | 
| 
       4 
4 
     | 
    
         
             
            Cron is non-ideal for running scheduled application tasks, especially in an app
         
     | 
| 
         @@ -149,6 +149,12 @@ This argument cannot be omitted.  Please use _ as placeholder if not needed. 
     | 
|
| 
       149 
149 
     | 
    
         
             
                Clockwork.every(1.second, 'myjob', :if => lambda { |_| true })
         
     | 
| 
       150 
150 
     | 
    
         | 
| 
       151 
151 
     | 
    
         | 
| 
      
 152 
     | 
    
         
            +
            ### :thread
         
     | 
| 
      
 153 
     | 
    
         
            +
             
     | 
| 
      
 154 
     | 
    
         
            +
            A handler with `:thread` parameter runs in a different thread.
         
     | 
| 
      
 155 
     | 
    
         
            +
             
     | 
| 
      
 156 
     | 
    
         
            +
            If a job is long running or IO-intensive, this option will be useful to keep the clock precise.
         
     | 
| 
      
 157 
     | 
    
         
            +
             
     | 
| 
       152 
158 
     | 
    
         
             
            Configuration
         
     | 
| 
       153 
159 
     | 
    
         
             
            -----------------------
         
     | 
| 
       154 
160 
     | 
    
         | 
| 
         @@ -170,12 +176,18 @@ the `sleep_timeout` configuration option to set like shown below. 
     | 
|
| 
       170 
176 
     | 
    
         
             
            This is the default timezone to use for all events.  When not specified this defaults to the local
         
     | 
| 
       171 
177 
     | 
    
         
             
            timezone.  Specifying :tz in the the parameters for an event overrides anything set here.
         
     | 
| 
       172 
178 
     | 
    
         | 
| 
      
 179 
     | 
    
         
            +
            ### :max_threads
         
     | 
| 
      
 180 
     | 
    
         
            +
             
     | 
| 
      
 181 
     | 
    
         
            +
            Clockwork runs handlers in threads. If it exceeds `max_threads`, it will warn you about missing
         
     | 
| 
      
 182 
     | 
    
         
            +
            jobs.
         
     | 
| 
      
 183 
     | 
    
         
            +
             
     | 
| 
       173 
184 
     | 
    
         
             
            ### Configuration example
         
     | 
| 
       174 
185 
     | 
    
         | 
| 
       175 
186 
     | 
    
         
             
                Clockwork.configure do |config|
         
     | 
| 
       176 
187 
     | 
    
         
             
                  config[:sleep_timeout] = 5
         
     | 
| 
       177 
188 
     | 
    
         
             
                  config[:logger] = Logger.new(log_file_path)
         
     | 
| 
       178 
189 
     | 
    
         
             
                  config[:tz] = 'EST'
         
     | 
| 
      
 190 
     | 
    
         
            +
                  config[:max_threads] = 15
         
     | 
| 
       179 
191 
     | 
    
         
             
                end
         
     | 
| 
       180 
192 
     | 
    
         | 
| 
       181 
193 
     | 
    
         
             
            Anatomy of a clock file
         
     | 
| 
         @@ -251,7 +263,7 @@ Meta 
     | 
|
| 
       251 
263 
     | 
    
         | 
| 
       252 
264 
     | 
    
         
             
            Created by Adam Wiggins
         
     | 
| 
       253 
265 
     | 
    
         | 
| 
       254 
     | 
    
         
            -
            Inspired by [rufus-scheduler](http://rufus.rubyforge.org/rufus-scheduler/) and [resque- 
     | 
| 
      
 266 
     | 
    
         
            +
            Inspired by [rufus-scheduler](http://rufus.rubyforge.org/rufus-scheduler/) and [resque-scheduler](http://github.com/bvandenbos/resque-scheduler)
         
     | 
| 
       255 
267 
     | 
    
         | 
| 
       256 
268 
     | 
    
         
             
            Design assistance from Peter van Hardenberg and Matthew Soldo
         
     | 
| 
       257 
269 
     | 
    
         | 
    
        data/VERSION
    CHANGED
    
    | 
         @@ -1 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            0.5. 
     | 
| 
      
 1 
     | 
    
         
            +
            0.5.1
         
     | 
    
        data/lib/clockwork.rb
    CHANGED
    
    | 
         @@ -69,6 +69,10 @@ module Clockwork 
     | 
|
| 
       69 
69 
     | 
    
         
             
                    end
         
     | 
| 
       70 
70 
     | 
    
         
             
                  end
         
     | 
| 
       71 
71 
     | 
    
         | 
| 
      
 72 
     | 
    
         
            +
                  if options[:thread]
         
     | 
| 
      
 73 
     | 
    
         
            +
                    @thread = options[:thread]
         
     | 
| 
      
 74 
     | 
    
         
            +
                  end
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
       72 
76 
     | 
    
         
             
                  tz = options[:tz] || Clockwork.config[:tz]
         
     | 
| 
       73 
77 
     | 
    
         
             
                  @timezone = TZInfo::Timezone.get(tz) if tz
         
     | 
| 
       74 
78 
     | 
    
         
             
                end
         
     | 
| 
         @@ -87,12 +91,33 @@ module Clockwork 
     | 
|
| 
       87 
91 
     | 
    
         
             
                  elapsed_ready and (@at.nil? or @at.ready?(t)) and (@if.nil? or @if.call(t))
         
     | 
| 
       88 
92 
     | 
    
         
             
                end
         
     | 
| 
       89 
93 
     | 
    
         | 
| 
      
 94 
     | 
    
         
            +
                def thread?
         
     | 
| 
      
 95 
     | 
    
         
            +
                  @thread
         
     | 
| 
      
 96 
     | 
    
         
            +
                end
         
     | 
| 
      
 97 
     | 
    
         
            +
             
     | 
| 
      
 98 
     | 
    
         
            +
                def thread_available?
         
     | 
| 
      
 99 
     | 
    
         
            +
                  Thread.list.count < Clockwork.config[:max_threads]
         
     | 
| 
      
 100 
     | 
    
         
            +
                end
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
       90 
102 
     | 
    
         
             
                def run(t)
         
     | 
| 
       91 
103 
     | 
    
         
             
                  t = convert_timezone(t)
         
     | 
| 
       92 
104 
     | 
    
         
             
                  @last = t
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
      
 106 
     | 
    
         
            +
                  if thread?
         
     | 
| 
      
 107 
     | 
    
         
            +
                    if thread_available?
         
     | 
| 
      
 108 
     | 
    
         
            +
                      Thread.new { execute }
         
     | 
| 
      
 109 
     | 
    
         
            +
                    else
         
     | 
| 
      
 110 
     | 
    
         
            +
                      log_error "Threads exhausted; skipping #{self}"
         
     | 
| 
      
 111 
     | 
    
         
            +
                    end
         
     | 
| 
      
 112 
     | 
    
         
            +
                  else
         
     | 
| 
      
 113 
     | 
    
         
            +
                    execute
         
     | 
| 
      
 114 
     | 
    
         
            +
                  end
         
     | 
| 
      
 115 
     | 
    
         
            +
                end
         
     | 
| 
      
 116 
     | 
    
         
            +
             
     | 
| 
      
 117 
     | 
    
         
            +
                def execute
         
     | 
| 
       93 
118 
     | 
    
         
             
                  @block.call(@job)
         
     | 
| 
       94 
119 
     | 
    
         
             
                rescue => e
         
     | 
| 
       95 
     | 
    
         
            -
                  log_error 
     | 
| 
      
 120 
     | 
    
         
            +
                  log_error e
         
     | 
| 
       96 
121 
     | 
    
         
             
                end
         
     | 
| 
       97 
122 
     | 
    
         | 
| 
       98 
123 
     | 
    
         
             
                def log_error(e)
         
     | 
| 
         @@ -122,7 +147,7 @@ module Clockwork 
     | 
|
| 
       122 
147 
     | 
    
         
             
              extend self
         
     | 
| 
       123 
148 
     | 
    
         | 
| 
       124 
149 
     | 
    
         
             
              def default_configuration
         
     | 
| 
       125 
     | 
    
         
            -
                { :sleep_timeout => 1, :logger => Logger.new(STDOUT) }
         
     | 
| 
      
 150 
     | 
    
         
            +
                { :sleep_timeout => 1, :logger => Logger.new(STDOUT), :max_threads => 10 }
         
     | 
| 
       126 
151 
     | 
    
         
             
              end
         
     | 
| 
       127 
152 
     | 
    
         | 
| 
       128 
153 
     | 
    
         
             
              @@configuration = default_configuration
         
     | 
    
        data/test/clockwork_test.rb
    CHANGED
    
    | 
         @@ -152,6 +152,7 @@ class ClockworkTest < Test::Unit::TestCase 
     | 
|
| 
       152 
152 
     | 
    
         
             
                $set_me = 0
         
     | 
| 
       153 
153 
     | 
    
         
             
                Clockwork.every(1.minute, 'myjob') { $set_me = 2 }
         
     | 
| 
       154 
154 
     | 
    
         
             
                Clockwork.tick(Time.now)
         
     | 
| 
      
 155 
     | 
    
         
            +
             
     | 
| 
       155 
156 
     | 
    
         
             
                assert_equal 2, $set_me
         
     | 
| 
       156 
157 
     | 
    
         
             
              end
         
     | 
| 
       157 
158 
     | 
    
         | 
| 
         @@ -159,7 +160,10 @@ class ClockworkTest < Test::Unit::TestCase 
     | 
|
| 
       159 
160 
     | 
    
         
             
                Clockwork.handler { raise 'boom' }
         
     | 
| 
       160 
161 
     | 
    
         
             
                event = Clockwork.every(1.minute, 'myjob')
         
     | 
| 
       161 
162 
     | 
    
         
             
                event.expects(:log_error)
         
     | 
| 
       162 
     | 
    
         
            -
             
     | 
| 
      
 163 
     | 
    
         
            +
             
     | 
| 
      
 164 
     | 
    
         
            +
                assert_nothing_raised do
         
     | 
| 
      
 165 
     | 
    
         
            +
                  Clockwork.tick(Time.now)
         
     | 
| 
      
 166 
     | 
    
         
            +
                end
         
     | 
| 
       163 
167 
     | 
    
         
             
              end
         
     | 
| 
       164 
168 
     | 
    
         | 
| 
       165 
169 
     | 
    
         
             
              test "exceptions still set the last timestamp to avoid spastic error loops" do
         
     | 
| 
         @@ -174,15 +178,18 @@ class ClockworkTest < Test::Unit::TestCase 
     | 
|
| 
       174 
178 
     | 
    
         
             
                Clockwork.configure do |config|
         
     | 
| 
       175 
179 
     | 
    
         
             
                  config[:sleep_timeout] = 200
         
     | 
| 
       176 
180 
     | 
    
         
             
                  config[:logger] = "A Logger"
         
     | 
| 
      
 181 
     | 
    
         
            +
                  config[:max_threads] = 10
         
     | 
| 
       177 
182 
     | 
    
         
             
                end
         
     | 
| 
       178 
183 
     | 
    
         | 
| 
       179 
184 
     | 
    
         
             
                assert_equal 200, Clockwork.config[:sleep_timeout]
         
     | 
| 
       180 
185 
     | 
    
         
             
                assert_equal "A Logger", Clockwork.config[:logger]
         
     | 
| 
      
 186 
     | 
    
         
            +
                assert_equal 10, Clockwork.config[:max_threads]
         
     | 
| 
       181 
187 
     | 
    
         
             
              end
         
     | 
| 
       182 
188 
     | 
    
         | 
| 
       183 
189 
     | 
    
         
             
              test "configuration should have reasonable defaults" do
         
     | 
| 
       184 
190 
     | 
    
         
             
                assert_equal 1, Clockwork.config[:sleep_timeout]
         
     | 
| 
       185 
191 
     | 
    
         
             
                assert Clockwork.config[:logger].is_a?(Logger)
         
     | 
| 
      
 192 
     | 
    
         
            +
                assert_equal 10, Clockwork.config[:max_threads]
         
     | 
| 
       186 
193 
     | 
    
         
             
              end
         
     | 
| 
       187 
194 
     | 
    
         | 
| 
       188 
195 
     | 
    
         
             
              test "should be able to specify a different timezone than local" do
         
     | 
| 
         @@ -243,4 +250,15 @@ class ClockworkTest < Test::Unit::TestCase 
     | 
|
| 
       243 
250 
     | 
    
         
             
                end
         
     | 
| 
       244 
251 
     | 
    
         
             
              end
         
     | 
| 
       245 
252 
     | 
    
         | 
| 
      
 253 
     | 
    
         
            +
              test "should warn about missing jobs upon exhausting threads" do
         
     | 
| 
      
 254 
     | 
    
         
            +
                Clockwork.configure do |config|
         
     | 
| 
      
 255 
     | 
    
         
            +
                  config[:max_threads] = 0
         
     | 
| 
      
 256 
     | 
    
         
            +
                end
         
     | 
| 
      
 257 
     | 
    
         
            +
             
     | 
| 
      
 258 
     | 
    
         
            +
                event = Clockwork.every(1.minute, 'myjob', :thread => true)
         
     | 
| 
      
 259 
     | 
    
         
            +
                event.expects(:log_error).with("Threads exhausted; skipping #{event}")
         
     | 
| 
      
 260 
     | 
    
         
            +
             
     | 
| 
      
 261 
     | 
    
         
            +
                Clockwork.tick(Time.now)
         
     | 
| 
      
 262 
     | 
    
         
            +
              end
         
     | 
| 
      
 263 
     | 
    
         
            +
             
     | 
| 
       246 
264 
     | 
    
         
             
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: clockwork
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.5. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.5.1
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       6 
6 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       7 
7 
     | 
    
         
             
            authors:
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date: 2013- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2013-05-20 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: tzinfo
         
     | 
| 
         @@ -124,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       124 
124 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       125 
125 
     | 
    
         
             
                  segments:
         
     | 
| 
       126 
126 
     | 
    
         
             
                  - 0
         
     | 
| 
       127 
     | 
    
         
            -
                  hash: - 
     | 
| 
      
 127 
     | 
    
         
            +
                  hash: -423339919
         
     | 
| 
       128 
128 
     | 
    
         
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
       129 
129 
     | 
    
         
             
              none: false
         
     | 
| 
       130 
130 
     | 
    
         
             
              requirements:
         
     |