pax_global_header00006660000000000000000000000064135163304430014514gustar00rootroot0000000000000052 comment=e2e0c0a628cbc64d9c113efe288d153f8241e884 clockwork-2.0.4/000077500000000000000000000000001351633044300135155ustar00rootroot00000000000000clockwork-2.0.4/.gitignore000066400000000000000000000000541351633044300155040ustar00rootroot00000000000000pkg tmp /.bundle Gemfile.lock .ruby-version clockwork-2.0.4/.travis.yml000066400000000000000000000007031351633044300156260ustar00rootroot00000000000000language: ruby sudo: false cache: bundler before_install: - gem update --system # This is required to support ActiveSupport 4 # https://docs.travis-ci.com/user/languages/ruby/#bundler-20 - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true - gem install bundler -v '< 2' rvm: - 2.3.8 - 2.4.5 - 2.5.3 - jruby-9.1.17.0 - jruby-9.2.6.0 gemfile: - gemfiles/activesupport4.gemfile - gemfiles/activesupport5.gemfile clockwork-2.0.4/CHANGELOG.md000066400000000000000000000003731351633044300153310ustar00rootroot00000000000000## 2.0.4 ## * Reverts the breaking changes in PR #18 that went out in patch 2.0.3 *Javier Julio* ## 2.0.3 (February 15, 2018) ## * [See the version release](https://github.com/Rykian/clockwork/releases) for the commits that were included. clockwork-2.0.4/Gemfile000066400000000000000000000000471351633044300150110ustar00rootroot00000000000000source 'https://rubygems.org' gemspec clockwork-2.0.4/LICENSE000066400000000000000000000021351351633044300145230ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2010-2014 Adam Wiggins, tomykaira Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. clockwork-2.0.4/README.md000066400000000000000000000476771351633044300150210ustar00rootroot00000000000000Clockwork - a clock process to replace cron [![Build Status](https://api.travis-ci.org/Rykian/clockwork.png?branch=master)](https://travis-ci.org/Rykian/clockwork) =========================================== Cron is non-ideal for running scheduled application tasks, especially in an app deployed to multiple machines. [More details.](http://adam.herokuapp.com/past/2010/4/13/rethinking_cron/) Clockwork is a cron replacement. It runs as a lightweight, long-running Ruby process which sits alongside your web processes (Mongrel/Thin) and your worker processes (DJ/Resque/Minion/Stalker) to schedule recurring work at particular times or dates. For example, refreshing feeds on an hourly basis, or send reminder emails on a nightly basis, or generating invoices once a month on the 1st. Quickstart ---------- Create clock.rb: ```ruby require 'clockwork' require 'active_support/time' # Allow numeric durations (eg: 1.minutes) module Clockwork handler do |job| puts "Running #{job}" end # handler receives the time when job is prepared to run in the 2nd argument # handler do |job, time| # puts "Running #{job}, at #{time}" # end every(10.seconds, 'frequent.job') every(3.minutes, 'less.frequent.job') every(1.hour, 'hourly.job') every(1.day, 'midnight.job', :at => '00:00') end ``` Run it with the clockwork executable: ``` $ clockwork clock.rb Starting clock for 4 events: [ frequent.job less.frequent.job hourly.job midnight.job ] Triggering frequent.job ``` If you need to load your entire environment for your jobs, simply add: ```ruby require './config/boot' require './config/environment' ``` under the `require 'clockwork'` declaration. Quickstart for Heroku --------------------- Clockwork fits well with heroku's cedar stack. Consider using [clockwork-init.sh](https://gist.github.com/tomykaira/1312172) to create a new project for Heroku. Use with queueing ----------------- The clock process only makes sense as a place to schedule work to be done, not to do the work. It avoids locking by running as a single process, but this makes it impossible to parallelize. For doing the work, you should be using a job queueing system, such as [Delayed Job](http://www.therailsway.com/2009/7/22/do-it-later-with-delayed-job), [Beanstalk/Stalker](http://adam.herokuapp.com/past/2010/4/24/beanstalk_a_simple_and_fast_queueing_backend/), [RabbitMQ/Minion](http://adam.herokuapp.com/past/2009/9/28/background_jobs_with_rabbitmq_and_minion/), [Resque](http://github.com/blog/542-introducing-resque), or [Sidekiq](https://github.com/mperham/sidekiq). This design allows a simple clock process with no locks, but also offers near infinite horizontal scalability. For example, if you're using Beanstalk/Stalker: ```ruby require 'stalker' require 'active_support/time' module Clockwork handler { |job| Stalker.enqueue(job) } every(1.hour, 'feeds.refresh') every(1.day, 'reminders.send', :at => '01:30') end ``` Using a queueing system which doesn't require that your full application be loaded is preferable, because the clock process can keep a tiny memory footprint. If you're using DJ or Resque, however, you can go ahead and load your full application environment, and use per-event blocks to call DJ or Resque enqueue methods. For example, with DJ/Rails: ```ruby require 'config/boot' require 'config/environment' every(1.hour, 'feeds.refresh') { Feed.send_later(:refresh) } every(1.day, 'reminders.send', :at => '01:30') { Reminder.send_later(:send_reminders) } ``` Use with database events ----------------------- In addition to managing static events in your `clock.rb`, you can configure clockwork to synchronise with dynamic events from a database. Like static events, these database-backed events say when they should be run, and how frequently; the difference being that if you change those settings in the database, they will be reflected in clockwork. To keep the database events in sync with clockwork, a special manager class `DatabaseEvents::Manager` is used. You tell it to sync a database-backed model using the `sync_database_events` method, and then, at the frequency you specify, it will fetch all the events from the database, and ensure clockwork is using the latest settings. ### Example `clock.rb` file Here we're using an `ActiveRecord` model called `ClockworkDatabaseEvent` to store events in the database: ```ruby require 'clockwork' require 'clockwork/database_events' require_relative './config/boot' require_relative './config/environment' module Clockwork # required to enable database syncing support Clockwork.manager = DatabaseEvents::Manager.new sync_database_events model: ClockworkDatabaseEvent, every: 1.minute do |model_instance| # do some work e.g... # running a DelayedJob task, where #some_action is a method # you've defined on the model, which does the work you need model_instance.delay.some_action # performing some work with Sidekiq YourSidekiqWorkerClass.perform_async end [other events if you have] end ``` This tells clockwork to fetch all `ClockworkDatabaseEvent` instances from the database, and create an internal clockwork event for each one. Each clockwork event will be configured based on the instance's `frequency` and, optionally, its `at`, `if?`, `ignored_attributes`, `name`, and, `tz` methods. The code above also says to reload the events from the database every `1.minute`; we need pick up any changes in the database frequently (choose a sensible reload frequency by changing the `every:` option). When one of the events is ready to be run (based on it's `frequency`, and possible `at`, `if?`, `ignored attributes`, and `tz` methods), clockwork arranges for the block passed to `sync_database_events` to be run. The above example shows how you could use either DelayedJob or Sidekiq to kick off a worker job. This approach is good because the ideal is to use clockwork as a simple scheduler, and avoid making it carry out any long-running tasks. ### Your Model Classes `ActiveRecord` models are a perfect candidate for the model class. Having said that, the only requirements are: 1. the class responds to `all` returning an array of instances from the database 2. the instances returned respond to: - `id` returning a unique identifier (this is needed to track changes to event settings) - `frequency` returning the how frequently (in seconds) the database event should be run - `attributes` returning a hash of [attribute name] => [attribute value] values (or really anything that we can use store on registering the event, and then compare again to see if the state has changed later) - `at` *(optional)* return any acceptable clockwork `:at` string - `name` *(optional)* returning the name for the event (used to identify it in the Clockwork output) - `if?`*(optional)* returning either true or false, depending on whether the database event should run at the given time (this method will be passed the time as a parameter, much like the standard clockwork `:if`) - `ignored_attributes` *(optional)* returning an array of model attributes (as symbols) to ignore when determining whether the database event has been modified since our last run - `tz` *(optional)* returning the timezone to use (default is the local timezone) #### Example Setup Here's an example of one way of setting up your ActiveRecord models: ```ruby # db/migrate/20140302220659_create_frequency_periods.rb class CreateFrequencyPeriods < ActiveRecord::Migration def change create_table :frequency_periods do |t| t.string :name t.timestamps end end end # 20140302221102_create_clockwork_database_events.rb class CreateClockworkDatabaseEvents < ActiveRecord::Migration def change create_table :clockwork_database_events do |t| t.integer :frequency_quantity t.references :frequency_period t.string :at t.timestamps end add_index :clockwork_database_events, :frequency_period_id end end # app/models/clockwork_database_event.rb class ClockworkDatabaseEvent < ActiveRecord::Base belongs_to :frequency_period attr_accessible :frequency_quantity, :frequency_period_id, :at # Used by clockwork to schedule how frequently this event should be run # Should be the intended number of seconds between executions def frequency frequency_quantity.send(frequency_period.name.pluralize) end end # app/models/frequency_period.rb class FrequencyPeriod < ActiveRecord::Base attr_accessible :name end # db/seeds.rb ... # creating the FrequencyPeriods [:second, :minute, :hour, :day, :week, :month].each do |period| FrequencyPeriod.create(name: period) end ... ``` #### Example use of `if?` Database events support the ability to run events if certain conditions are met. This can be used to only run events on a given day, week, or month, or really any criteria you could conceive. Best of all, these criteria e.g. which day to run it on can be attributes on your Model, and therefore change dynamically as you change the Model in the database. So for example, if you had a Model that had a `day` and `month` integer attribute, you could specify that the Database event should only run on a particular day of a particular month as follows: ```ruby # app/models/clockwork_database_event.rb class ClockworkDatabaseEvent < ActiveRecord::Base ... def if?(time) time.day == day && time.month == month end ... end ``` #### Example use of `ignored_attributes` Clockwork compares all attributes of the model between runs to determine if the model has changed, and if it has, it runs the event if all other conditions are met. However, in certain cases, you may want to store additional attributes in your model that you don't want to affect whether a database event is executed prior to its next interval. So for example, you may update an attribute of your model named `last_scheduled_at` on each run to track the last time it was successfully scheduled. You can tell Clockwork to ignore that attribute in its comparison as follows: ```ruby # app/models/clockwork_database_event.rb class ClockworkDatabaseEvent < ActiveRecord::Base ... def ignored_attributes [ :last_scheduled_at ] end ... end ``` Event Parameters ---------- ### :at `:at` parameter specifies when to trigger the event: #### Valid formats: HH:MM H:MM **:MM HH:** (Mon|mon|Monday|monday) HH:MM #### Examples The simplest example: ```ruby every(1.day, 'reminders.send', :at => '01:30') ``` You can omit the leading 0 of the hour: ```ruby every(1.day, 'reminders.send', :at => '1:30') ``` Wildcards for hour and minute are supported: ```ruby every(1.hour, 'reminders.send', :at => '**:30') every(10.seconds, 'frequent.job', :at => '9:**') ``` You can set more than one timing: ```ruby every(1.day, 'reminders.send', :at => ['12:00', '18:00']) # send reminders at noon and evening ``` You can specify the day of week to run: ```ruby every(1.week, 'myjob', :at => 'Monday 16:20') ``` If another task is already running at the specified time, clockwork will skip execution of the task with the `:at` option. If this is a problem, please use the `:thread` option to prevent the long running task from blocking clockwork's scheduler. ### :tz `:tz` parameter lets you specify a timezone (default is the local timezone): ```ruby every(1.day, 'reminders.send', :at => '00:00', :tz => 'UTC') # Runs the job each day at midnight, UTC. # The value for :tz can be anything supported by [TZInfo](https://github.com/tzinfo/tzinfo) # Using the 'tzinfo' gem, run TZInfo::Timezone.all_identifiers to get a list of acceptable identifiers. ``` ### :if `:if` parameter is invoked every time the task is ready to run, and run if the return value is true. Run on every first day of month. ```ruby Clockwork.every(1.day, 'myjob', :if => lambda { |t| t.day == 1 }) ``` The argument is an instance of `ActiveSupport::TimeWithZone` if the `:tz` option is set. Otherwise, it's an instance of `Time`. This argument cannot be omitted. Please use _ as placeholder if not needed. ```ruby Clockwork.every(1.second, 'myjob', :if => lambda { |_| true }) ``` ### :thread By default, clockwork runs in a single-process and single-thread. If an event handler takes a long time, the main routine of clockwork is blocked until it ends. Clockwork does not misbehave, but the next event is blocked, and runs when the process is returned to the clockwork routine. The `:thread` option is to avoid blocking. An event with `thread: true` runs in a different thread. ```ruby Clockwork.every(1.day, 'run.me.in.new.thread', :thread => true) ``` If a job is long-running or IO-intensive, this option helps keep the clock precise. ### :skip_first_run Normally, a clockwork process that is defined to run in a specified period will run at startup. This is sometimes undesired behaviour, if the action being run relies on other processes booting which may be slower than clock. To avoid this problem, `:skip_first_run` can be used. ```ruby Clockwork.every(5.minutes, 'myjob', :skip_first_run => true) ``` The above job will not run at initial boot, and instead run every 5 minutes after boot. Configuration ----------------------- Clockwork exposes a couple of configuration options: ### :logger By default Clockwork logs to `STDOUT`. In case you prefer your own logger implementation you have to specify the `logger` configuration option. See example below. ### :sleep_timeout Clockwork wakes up once a second and performs its duties. To change the number of seconds Clockwork sleeps, set the `sleep_timeout` configuration option as shown below in the example. From 1.1.0, Clockwork does not accept `sleep_timeout` less than 1 seconds. ### :tz This is the default timezone to use for all events. When not specified this defaults to the local timezone. Specifying :tz in the parameters for an event overrides anything set here. ### :max_threads Clockwork runs handlers in threads. If it exceeds `max_threads`, it will warn you (log an error) about missing jobs. ### :thread Boolean true or false. Default is false. If set to true, every event will be run in its own thread. Can be overridden on a per event basis (see the ```:thread``` option in the Event Parameters section above) ### Configuration example ```ruby module Clockwork configure do |config| config[:sleep_timeout] = 5 config[:logger] = Logger.new(log_file_path) config[:tz] = 'EST' config[:max_threads] = 15 config[:thread] = true end end ``` ### error_handler You can add error_handler to define your own logging or error rescue. ```ruby module Clockwork error_handler do |error| Airbrake.notify_or_ignore(error) end end ``` Current specifications are as follows. - defining error_handler does not disable original logging - errors from error_handler itself are not rescued, and stop clockwork Any suggestion about these specifications is welcome. Old style --------- `include Clockwork` is old style. The old style is still supported, though not recommended, because it pollutes the global namespace. Anatomy of a clock file ----------------------- clock.rb is standard Ruby. Since we include the Clockwork module (the clockwork executable does this automatically, or you can do it explicitly), this exposes a small DSL to define the handler for events, and then the events themselves. The handler typically looks like this: ```ruby handler { |job| enqueue_your_job(job) } ``` This block will be invoked every time an event is triggered, with the job name passed in. In most cases, you should be able to pass the job name directly through to your queueing system. The second part of the file, which lists the events, roughly resembles a crontab: ```ruby every(5.minutes, 'thing.do') every(1.hour, 'otherthing.do') ``` In the first line of this example, an event will be triggered once every five minutes, passing the job name 'thing.do' into the handler. The handler shown above would thus call enqueue_your_job('thing.do'). You can also pass a custom block to the handler, for job queueing systems that rely on classes rather than job names (i.e. DJ and Resque). In this case, you need not define a general event handler, and instead provide one with each event: ```ruby every(5.minutes, 'thing.do') { Thing.send_later(:do) } ``` If you provide a custom handler for the block, the job name is used only for logging. You can also use blocks to do more complex checks: ```ruby every(1.day, 'check.leap.year') do Stalker.enqueue('leap.year.party') if Date.leap?(Time.now.year) end ``` In addition, Clockwork also supports `:before_tick`, `:after_tick`, `:before_run`, and `:after_run` callbacks. All callbacks are optional. The `tick` callbacks run every tick (a tick being whatever your `:sleep_timeout` is set to, default is 1 second): ```ruby on(:before_tick) do puts "tick" end on(:after_tick) do puts "tock" end on(:before_run) do |event, t| puts "job_started: #{event}" end on(:after_run) do |event, t| puts "job_finished: #{event}" end ``` Finally, you can use tasks synchronised from a database as described in detail above: ```ruby sync_database_events model: MyEvent, every: 1.minute do |instance_job_name| # what to do with each instance end ``` You can use multiple `sync_database_events` if you wish, so long as you use different model classes for each (ActiveRecord Single Table Inheritance could be a good idea if you're doing this). In production ------------- Only one clock process should ever be running across your whole application deployment. For example, if your app is running on three VPS machines (two app servers and one database), your app machines might have the following process topography: * App server 1: 3 web (thin start), 3 workers (rake jobs:work), 1 clock (clockwork clock.rb) * App server 2: 3 web (thin start), 3 workers (rake jobs:work) You should use [Monit](http://mmonit.com/monit/), [God](https://github.com/mojombo/god), [Upstart](http://upstart.ubuntu.com/), or [Inittab](http://www.tldp.org/LDP/sag/html/config-init.html) to keep your clock process running the same way you keep your web and workers running. Daemonization ------------- Thanks to @fddayan, `clockworkd` executes clockwork script as a daemon. You will need the [daemons gem](https://github.com/ghazel/daemons) to use `clockworkd`. It is not automatically installed, please install by yourself. Then, ``` clockworkd -c YOUR_CLOCK.rb start ``` For more details, you can run `clockworkd -h`. Integration Testing ------------------- You could take a look at: * [clockwork-mocks](https://github.com/dpoetzsch/clockwork-mocks) that helps with running scheduled tasks during integration testing. * [clockwork-test](https://github.com/kevin-j-m/clockwork-test) which ensures that tasks are triggered at the right time Issues and Pull requests ------------------------ If you find a bug, please create an issue - [Issues · Rykian/clockwork](https://github.com/Rykian/clockwork/issues). For a bug fix or a feature request, please send a pull-request. Do not forget to add tests to show how your feature works, or what bug is fixed. All existing tests and new tests must pass (TravisCI is watching). We want to provide simple and customizable core, so superficial changes will not be merged (e.g. supporting new event registration style). In most cases, directly operating `Manager` realizes an idea, without touching the core. If you discover a new way to use Clockwork, please create a gist page or an article on your website, then add it to the following "Use cases" section. This tool is already used in various environment, so backward-incompatible requests will be mostly rejected. Use cases --------- Feel free to add your idea or experience and send a pull-request. - Sending errors to Airbrake - Read events from a database Meta ---- Created by Adam Wiggins Inspired by [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler) and [resque-scheduler](https://github.com/bvandenbos/resque-scheduler) Design assistance from Peter van Hardenberg and Matthew Soldo Patches contributed by Mark McGranaghan and Lukáš Konarovský Released under the MIT License: http://www.opensource.org/licenses/mit-license.php https://github.com/Rykian/clockwork clockwork-2.0.4/Rakefile000066400000000000000000000002561351633044300151650ustar00rootroot00000000000000require 'bundler/gem_tasks' require 'rake/testtask' Rake::TestTask.new do |t| t.test_files = FileList['test/**/*_test.rb'] t.verbose = false end task :default => :test clockwork-2.0.4/bin/000077500000000000000000000000001351633044300142655ustar00rootroot00000000000000clockwork-2.0.4/bin/clockwork000077500000000000000000000003411351633044300162070ustar00rootroot00000000000000#!/usr/bin/env ruby STDERR.sync = STDOUT.sync = true require 'clockwork' usage = 'Usage: clockwork ' file = ARGV.shift or abort usage file = "./#{file}" unless file.match(/^[\/.]/) require file Clockwork::run clockwork-2.0.4/bin/clockworkd000077500000000000000000000065441351633044300163660ustar00rootroot00000000000000#!/usr/bin/env ruby STDERR.sync = STDOUT.sync = true require 'clockwork' require 'optparse' require 'pathname' begin require 'daemons' rescue LoadError raise "You need to add gem 'daemons' to your Gemfile or Rubygems if you wish to use it." end @options = { :quiet => false, :pid_dir => File.expand_path("./tmp"), :log_output => false, :monitor => false, :file => File.expand_path("./clock.rb") } bin_basename = File.basename($0) opts = OptionParser.new do |opts| opts.banner = "Usage: #{bin_basename} -c FILE [options] start|stop|restart|run" opts.separator '' opts.on_tail('-h', '--help', 'Show this message') do puts opts exit 1 end opts.on("--pid-dir=DIR", "Alternate directory in which to store the process ids. Default is #{@options[:pid_dir]}.") do |dir| @options[:pid_dir] = dir end opts.on('-i', '--identifier=STR', 'An identifier for the process. Default is clock file name.') do |n| @options[:identifier] = n end opts.on('-l', '--log', "Redirect both STDOUT and STDERR to a logfile named #{bin_basename}[.].output in the pid-file directory.") do @options[:log_output] = true end opts.on('--log-dir=DIR', 'A specific directory to put the log files into (default location is pid directory).') do | log_dir| @options[:log_dir] = File.expand_path(log_dir) end opts.on('-m', '--monitor', 'Start monitor process.') do @options[:monitor] = true end opts.on('-c', '--clock=FILE',"Clock .rb file. Default is #{@options[:file]}.") do |clock_file| @options[:file] = clock_file @options[:file] = "./#{@options[:file]}" unless Pathname.new(@options[:file]).absolute? @options[:file] = File.expand_path(@options[:file]) end opts.on('-d', '--dir=DIR', 'Directory to change to once the process starts') do |dir| @options[:current_dir] = File.expand_path(dir) end end @args = opts.parse!(ARGV) def optparser_abort(opts,message) $stderr.puts message puts opts exit 1 end optparser_abort opts, "ERROR: --clock=FILE is required." unless @options[:file] optparser_abort opts, "ERROR: clock file #{@options[:file]} does not exist." unless File.exists?(@options[:file]) optparser_abort opts, "ERROR: File extension specified in --clock must be '.rb'" unless File.extname(@options[:file]) == ".rb" @options[:identifier] ||= "#{File.basename(@options[:file],'.*')}" process_name = "#{bin_basename}.#{@options[:identifier]}" @options[:log_dir] ||= @options[:pid_dir] Dir.mkdir(@options[:pid_dir]) unless File.exists?(@options[:pid_dir]) Dir.mkdir(@options[:log_dir]) unless File.exists?(@options[:log_dir]) puts "#{process_name}: pid file: #{File.expand_path(File.join(@options[:pid_dir],process_name + '.pid'))}" if @options[:log_output] puts "#{process_name}: output log file: #{File.expand_path(File.join(@options[:log_dir],process_name + '.output'))}" else puts "#{process_name}: No output will be printed out (run with --log if needed)" end Daemons.run_proc(process_name, :dir => @options[:pid_dir], :dir_mode => :normal, :monitor => @options[:monitor], :log_dir => @options[:log_dir], :log_output => @options[:log_output], :ARGV => @args) do |*args| # daemons changes the current working directory to '/' when a new process is # forked. We change it back to the project root directory here. Dir.chdir(@options[:current_dir]) if @options[:current_dir] require @options[:file] Clockwork::run end clockwork-2.0.4/clockwork.gemspec000066400000000000000000000017751351633044300170720ustar00rootroot00000000000000Gem::Specification.new do |s| s.name = "clockwork" s.version = "2.0.4" s.authors = ["Adam Wiggins", "tomykaira"] s.license = 'MIT' s.description = "A scheduler process to replace cron, using a more flexible Ruby syntax running as a single long-running process. Inspired by rufus-scheduler and resque-scheduler." s.email = ["adam@heroku.com", "tomykaira@gmail.com"] s.extra_rdoc_files = [ "README.md" ] s.homepage = "http://github.com/Rykian/clockwork" s.summary = "A scheduler process to replace cron." s.files = `git ls-files`.split($/) s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) } s.test_files = s.files.grep(%r{^(test|spec|features)/}) s.require_paths = ["lib"] s.add_dependency(%q) s.add_dependency(%q) s.add_development_dependency "rake" s.add_development_dependency "daemons" s.add_development_dependency "minitest", "~> 5.8" s.add_development_dependency "mocha" s.add_development_dependency "test-unit" end clockwork-2.0.4/clockworkd.1000066400000000000000000000033501351633044300157420ustar00rootroot00000000000000.TH CLOCKWORKD 1 "August 2014" "Ruby Gem" "clockwork" .SH NAME clockworkd - daemon executing clockwork scripts .SH SYNOPSIS \fBclockworkd\fR [-c \fIFILE\fR] [\fIOPTIONS\fR] {start|stop|restart|run} .SH DESCRIPTION \fBclockworkd\fR executes clockwork script as a daemon. You will need the \fBdaemons\fR gem to use \fBclockworkd\fR. It is not automatically installed, please install by yourself. .SH OPTIONS .TP \fB--pid-dir\fR=\fIDIR\fR Alternate directory in which to store the process ids. Default is \fIGEM_LOCATION\fR/tmp. .TP \fB-i\fR, \fB--identifier\fR=\fISTR\fR An identifier for the process. Default is clock file name. .TP \fB-l\fR, \fB--log\fR Redirect both STDOUT and STDERR to a logfile named clockworkd[.\fIidentifier\fR].output in the pid-file directory. .TP \fB--log-dir\fR=\fIDIR\fR A specific directory to put the log files into. Default location is pid directory. .TP \fB-m\fR, \fB--monitor\fR Start monitor process. .TP \fB-c\fR, \fB--clock\fR=\fIFILE\fR Clock .rb file. Default is \fIGEM_LOCATION\fR/clock.rb. .TP \fB-d\fR, \fB--dir\fR=\fIDIR\fR Directory to change to once the process starts .TP \fB-h\fR, \fB--help\fR Show help message. .SH BUGS If you find a bug, please create an issue \fIhttps://github.com/tomykaira/clockwork/issues\fR. For a bug fix or a feature request, please send a pull-request. Do not forget to add tests to show how your feature works, or what bug is fixed. All existing tests and new tests must pass (TravisCI is watching). .SH AUTHORS Created by Adam Wiggins. Inspired by rufus-scheduler and resque-scheduler. Design assistance from Peter van Hardenberg and Matthew Soldo. Patches contributed by Mark McGranaghan and Lukáš Konarovský. .SH SEE ALSO \fIhttps://github.com/tomykaira/clockwork\fR. clockwork-2.0.4/example.rb000066400000000000000000000010661351633044300155000ustar00rootroot00000000000000require 'clockwork' module Clockwork handler do |job| puts "Queueing job: #{job}" end every(10.seconds, 'run.me.every.10.seconds') every(1.minute, 'run.me.every.minute') every(1.hour, 'run.me.every.hour') every(1.day, 'run.me.at.midnight', :at => '00:00') every(1.day, 'custom.event.handler', :at => '00:30') do puts 'This event has its own handler' end # note: callbacks that return nil or false will cause event to not run on(:before_tick) do puts "tick" true end on(:after_tick) do puts "tock" true end end clockwork-2.0.4/gemfiles/000077500000000000000000000000001351633044300153105ustar00rootroot00000000000000clockwork-2.0.4/gemfiles/activesupport4.gemfile000066400000000000000000000003251351633044300216360ustar00rootroot00000000000000source 'https://rubygems.org' platforms :rbx do gem 'rubysl', '~> 2.0' gem 'rubysl-test-unit' gem 'rubinius-developer_tools' end gem 'activesupport', '~> 4.2' gem 'minitest', '~> 5.0' gemspec :path=>"../" clockwork-2.0.4/gemfiles/activesupport5.gemfile000066400000000000000000000003251351633044300216370ustar00rootroot00000000000000source 'https://rubygems.org' platforms :rbx do gem 'rubysl', '~> 2.0' gem 'rubysl-test-unit' gem 'rubinius-developer_tools' end gem 'activesupport', '~> 5.0' gem 'minitest', '~> 5.0' gemspec :path=>"../" clockwork-2.0.4/lib/000077500000000000000000000000001351633044300142635ustar00rootroot00000000000000clockwork-2.0.4/lib/clockwork.rb000066400000000000000000000017251351633044300166130ustar00rootroot00000000000000require 'logger' require 'active_support/time' require 'clockwork/at' require 'clockwork/event' require 'clockwork/manager' module Clockwork class << self def included(klass) klass.send "include", Methods klass.extend Methods end def manager @manager ||= Manager.new end def manager=(manager) @manager = manager end end module Methods def configure(&block) Clockwork.manager.configure(&block) end def handler(&block) Clockwork.manager.handler(&block) end def error_handler(&block) Clockwork.manager.error_handler(&block) end def on(event, options={}, &block) Clockwork.manager.on(event, options, &block) end def every(period, job, options={}, &block) Clockwork.manager.every(period, job, options, &block) end def run Clockwork.manager.run end def clear! Clockwork.manager = Manager.new end end extend Methods end clockwork-2.0.4/lib/clockwork/000077500000000000000000000000001351633044300162615ustar00rootroot00000000000000clockwork-2.0.4/lib/clockwork/at.rb000066400000000000000000000031201351633044300172060ustar00rootroot00000000000000module Clockwork class At class FailedToParse < StandardError; end NOT_SPECIFIED = nil WDAYS = %w[sunday monday tuesday wednesday thursday friday saturday].each.with_object({}).with_index do |(w, wdays), index| [w, w.capitalize, w[0...3], w[0...3].capitalize].each do |k| wdays[k] = index end end def self.parse(at) return unless at case at when /\A([[:alpha:]]+)\s(.*)\z/ if wday = WDAYS[$1] parsed_time = parse($2) parsed_time.wday = wday parsed_time else raise FailedToParse, at end when /\A(\d{1,2}):(\d\d)\z/ new($2.to_i, $1.to_i) when /\A\*{1,2}:(\d\d)\z/ new($1.to_i) when /\A(\d{1,2}):\*\*\z/ new(NOT_SPECIFIED, $1.to_i) else raise FailedToParse, at end rescue ArgumentError raise FailedToParse, at end attr_accessor :min, :hour, :wday def initialize(min, hour=NOT_SPECIFIED, wday=NOT_SPECIFIED) @min = min @hour = hour @wday = wday raise ArgumentError unless valid? end def ready?(t) (@min == NOT_SPECIFIED or t.min == @min) and (@hour == NOT_SPECIFIED or t.hour == @hour) and (@wday == NOT_SPECIFIED or t.wday == @wday) end def == other @min == other.min && @hour == other.hour && @wday == other.wday end private def valid? @min == NOT_SPECIFIED || (0..59).cover?(@min) && @hour == NOT_SPECIFIED || (0..23).cover?(@hour) && @wday == NOT_SPECIFIED || (0..6).cover?(@wday) end end end clockwork-2.0.4/lib/clockwork/database_events.rb000066400000000000000000000013701351633044300217370ustar00rootroot00000000000000require_relative 'database_events/event' require_relative 'database_events/synchronizer' require_relative 'database_events/event_store' require_relative 'database_events/manager' # TERMINOLOGY # # For clarity, we have chosen to define terms as follows for better communication in the code, and when # discussing the database event implementation. # # "Event": "Native" Clockwork events, whether Clockwork::Event or Clockwork::DatabaseEvents::Event # "Model": Database-backed model instances representing events to be created in Clockwork module Clockwork module Methods def sync_database_events(options={}, &block) DatabaseEvents::Synchronizer.setup(options, &block) end end extend Methods module DatabaseEvents end end clockwork-2.0.4/lib/clockwork/database_events/000077500000000000000000000000001351633044300214115ustar00rootroot00000000000000clockwork-2.0.4/lib/clockwork/database_events/event.rb000066400000000000000000000012471351633044300230630ustar00rootroot00000000000000module Clockwork module DatabaseEvents class Event < Clockwork::Event attr_accessor :event_store, :model_attributes def initialize(manager, period, job, block, event_store, model_attributes, options={}) super(manager, period, job, block, options) @event_store = event_store @event_store.register(self, job) @model_attributes = model_attributes end def name (job_has_name? && job.name) ? job.name : "#{job.class}:#{job.id}" end def job_has_name? job.respond_to?(:name) end def to_s name end def frequency @period end end end end clockwork-2.0.4/lib/clockwork/database_events/event_collection.rb000066400000000000000000000017661351633044300253040ustar00rootroot00000000000000module Clockwork module DatabaseEvents class EventCollection def initialize(manager=Clockwork.manager) @events = [] @manager = manager end def add(event) @events << event end def has_changed?(model) return true if event.nil? ignored_attributes = model.ignored_attributes if model.respond_to?(:ignored_attributes) ignored_attributes ||= [] model_attributes = model.attributes.select do |k, _| not ignored_attributes.include?(k.to_sym) end event.model_attributes != model_attributes end def unregister events.each{|e| manager.unregister(e) } end private attr_reader :events, :manager # All events in the same collection (for a model instance) are equivalent # so we can use any of them. Only their @at variable will be different, # but we don't care about that here. def event events.first end end end end clockwork-2.0.4/lib/clockwork/database_events/event_store.rb000066400000000000000000000112251351633044300242740ustar00rootroot00000000000000require_relative './event_collection' # How EventStore and Clockwork manager events are kept in sync... # # The normal Clockwork::Manager is responsible for keeping track of # Clockwork events, and ensuring they are scheduled at the correct time. # It has an @events array for this purpose. # For keeping track of Database-backed events though, we need to keep # track of more information about the events, e.g. the block which should # be triggered when they are run, which model the event comes from, the # model ID it relates to etc. Therefore, we devised a separate mechanism # for keeping track of these database-backed events: the per-model EventStore. # Having two classes responsible for keeping track of events though leads to # a slight quirk, in that these two have to be kept in sync. The way this is # done is by letting the EventStore largely defer to the Clockwork Manager. # 1. When the EventStore wishes to recreate events: # - it asks the Clockwork.manager to do this for it # - by calling Clockwork.manager.every # 2. When the DatabaseEvents::Manager creates events (via its #register) # - it creates a new DatabaseEvents::Event # - DatabaseEvents::Event#initialize registers it with the EventStore module Clockwork module DatabaseEvents class EventStore def initialize(block_to_perform_on_event_trigger) @related_events = {} @block_to_perform_on_event_trigger = block_to_perform_on_event_trigger end # DatabaseEvents::Manager#register creates a new DatabaseEvents::Event, whose # #initialize method registers the new database event with the EventStore by # calling this method. def register(event, model) related_events_for(model.id).add(event) end def update(current_model_objects) unregister_all_except(current_model_objects) update_registered_models(current_model_objects) register_new_models(current_model_objects) end def unregister_all_except(model_objects) ids = model_objects.collect(&:id) (@related_events.keys - ids).each{|id| unregister(id) } end def update_registered_models(model_objects) registered_models(model_objects).each do |model| if has_changed?(model) unregister(model.id) register_with_manager(model) end end end def register_new_models(model_objects) unregistered_models(model_objects).each do |new_model_object| register_with_manager(new_model_object) end end private attr_reader :related_events def registered?(model) related_events_for(model.id) != nil end def has_changed?(model) related_events_for(model.id).has_changed?(model) end def related_events_for(id) related_events[id] ||= EventCollection.new end def registered_models(model_objects) model_objects.select{|m| registered?(m) } end def unregistered_models(model_objects) model_objects.select{|m| !registered?(m) } end def unregister(id) related_events_for(id).unregister related_events.delete(id) end # When re-creating events, the Clockwork.manager must be used to # create them, as it is ultimately responsible for ensuring that # the events actually get run when they should. We call its #every # method, which will result in DatabaseEvent::Manager#register being # called, which creates a new DatabaseEvent::Event, which will be # registered with the EventStore on #initialize. def register_with_manager(model) Clockwork.manager. every(model.frequency, model, options(model), &@block_to_perform_on_event_trigger) end def options(model) options = { :from_database => true, :synchronizer => self, :ignored_attributes => [], } options[:at] = at_strings_for(model) if model.respond_to?(:at) options[:if] = ->(time){ model.if?(time) } if model.respond_to?(:if?) options[:tz] = model.tz if model.respond_to?(:tz) options[:ignored_attributes] = model.ignored_attributes if model.respond_to?(:ignored_attributes) # store the state of the model at time of registering so we can # easily compare and determine if state has changed later options[:model_attributes] = model.attributes.select do |k, v| not options[:ignored_attributes].include?(k.to_sym) end options end def at_strings_for(model) return nil if model.at.to_s.empty? model.at.split(',').map(&:strip) end end end end clockwork-2.0.4/lib/clockwork/database_events/manager.rb000066400000000000000000000011711351633044300233500ustar00rootroot00000000000000module Clockwork module DatabaseEvents class Manager < Clockwork::Manager def unregister(event) @events.delete(event) end def register(period, job, block, options) @events << if options[:from_database] synchronizer = options.fetch(:synchronizer) model_attributes = options.fetch(:model_attributes) Clockwork::DatabaseEvents::Event. new(self, period, job, (block || handler), synchronizer, model_attributes, options) else Clockwork::Event.new(self, period, job, block || handler, options) end end end end end clockwork-2.0.4/lib/clockwork/database_events/synchronizer.rb000066400000000000000000000013411351633044300244720ustar00rootroot00000000000000require_relative '../database_events' module Clockwork module DatabaseEvents class Synchronizer def self.setup(options={}, &block_to_perform_on_event_trigger) model_class = options.fetch(:model) { raise KeyError, ":model must be set to the model class" } every = options.fetch(:every) { raise KeyError, ":every must be set to the database sync frequency" } event_store = EventStore.new(block_to_perform_on_event_trigger) # create event that syncs clockwork events with events coming from database-backed model Clockwork.manager.every every, "sync_database_events_for_model_#{model_class}" do event_store.update(model_class.all) end end end end end clockwork-2.0.4/lib/clockwork/event.rb000066400000000000000000000035001351633044300177250ustar00rootroot00000000000000module Clockwork class Event attr_accessor :job, :last def initialize(manager, period, job, block, options={}) validate_if_option(options[:if]) @manager = manager @period = period @job = job @at = At.parse(options[:at]) @block = block @if = options[:if] @thread = options.fetch(:thread, @manager.config[:thread]) @timezone = options.fetch(:tz, @manager.config[:tz]) @skip_first_run = options[:skip_first_run] @last = @skip_first_run ? convert_timezone(Time.now) : nil end def convert_timezone(t) @timezone ? t.in_time_zone(@timezone) : t end def run_now?(t) t = convert_timezone(t) return false unless elapsed_ready?(t) return false unless run_at?(t) return false unless run_if?(t) true end def thread? @thread end def run(t) @manager.log "Triggering '#{self}'" @last = convert_timezone(t) if thread? if @manager.thread_available? t = Thread.new do execute end t['creator'] = @manager else @manager.log_error "Threads exhausted; skipping #{self}" end else execute end end def to_s job.to_s end private def execute @block.call(@job, @last) rescue => e @manager.log_error e @manager.handle_error e end def elapsed_ready?(t) @last.nil? || (t - @last.to_i).to_i >= @period end def run_at?(t) @at.nil? || @at.ready?(t) end def run_if?(t) @if.nil? || @if.call(t) end def validate_if_option(if_option) if if_option && !if_option.respond_to?(:call) raise ArgumentError.new(':if expects a callable object, but #{if_option} does not respond to call') end end end end clockwork-2.0.4/lib/clockwork/manager.rb000066400000000000000000000100771351633044300202250ustar00rootroot00000000000000module Clockwork class Manager class NoHandlerDefined < RuntimeError; end attr_reader :config def initialize @events = [] @callbacks = {} @config = default_configuration @handler = nil @mutex = Mutex.new @condvar = ConditionVariable.new @finish = false end def thread_available? Thread.list.select { |t| t['creator'] == self }.count < config[:max_threads] end def configure yield(config) if config[:sleep_timeout] < 1 config[:logger].warn 'sleep_timeout must be >= 1 second' end end def default_configuration { :sleep_timeout => 1, :logger => Logger.new(STDOUT), :thread => false, :max_threads => 10 } end def handler(&block) @handler = block if block_given? raise NoHandlerDefined unless @handler @handler end def error_handler(&block) @error_handler = block if block_given? @error_handler end def on(event, options={}, &block) raise "Unsupported callback #{event}" unless [:before_tick, :after_tick, :before_run, :after_run].include?(event.to_sym) (@callbacks[event.to_sym]||=[]) << block end def every(period, job='unnamed', options={}, &block) if job.is_a?(Hash) and options.empty? options = job job = "unnamed" end if options[:at].respond_to?(:each) every_with_multiple_times(period, job, options, &block) else register(period, job, block, options) end end def fire_callbacks(event, *args) @callbacks[event].nil? || @callbacks[event].all? { |h| h.call(*args) } end def run log "Starting clock for #{@events.size} events: [ #{@events.map(&:to_s).join(' ')} ]" sig_read, sig_write = IO.pipe (%w[INT TERM HUP] & Signal.list.keys).each do |sig| trap sig do sig_write.puts(sig) end end run_tick_loop while io = IO.select([sig_read]) sig = io.first[0].gets.chomp handle_signal(sig) end end def handle_signal(sig) logger.debug "Got #{sig} signal" case sig when 'INT' shutdown when 'TERM' # Heroku sends TERM signal, and waits 10 seconds before exit graceful_shutdown when 'HUP' graceful_shutdown end end def shutdown logger.info 'Shutting down' stop_tick_loop exit(0) end def graceful_shutdown logger.info 'Gracefully shutting down' stop_tick_loop wait_tick_loop_finishes exit(0) end def stop_tick_loop @finish = true end def wait_tick_loop_finishes @mutex.synchronize do # wait by synchronize @condvar.signal end end def run_tick_loop Thread.new do @mutex.synchronize do until @finish tick interval = config[:sleep_timeout] - Time.now.subsec + 0.001 @condvar.wait(@mutex, interval) if interval > 0 end end end end def tick(t=Time.now) if (fire_callbacks(:before_tick)) events = events_to_run(t) events.each do |event| if (fire_callbacks(:before_run, event, t)) event.run(t) fire_callbacks(:after_run, event, t) end end end fire_callbacks(:after_tick) events end def logger config[:logger] end def log_error(e) config[:logger].error(e) end def handle_error(e) error_handler.call(e) if error_handler end def log(msg) config[:logger].info(msg) end private def events_to_run(t) @events.select{ |event| event.run_now?(t) } end def register(period, job, block, options) event = Event.new(self, period, job, block || handler, options) @events << event event end def every_with_multiple_times(period, job, options={}, &block) each_options = options.clone options[:at].each do |at| each_options[:at] = at register(period, job, block, each_options) end end end end clockwork-2.0.4/test/000077500000000000000000000000001351633044300144745ustar00rootroot00000000000000clockwork-2.0.4/test/at_test.rb000066400000000000000000000061261351633044300164710ustar00rootroot00000000000000require File.expand_path('../../lib/clockwork', __FILE__) require "minitest/autorun" require 'mocha/setup' require 'time' require 'active_support/time' describe 'Clockwork::At' do def time_in_day(hour, minute) Time.new(2013, 1, 1, hour, minute, 0) end it '16:20' do at = Clockwork::At.parse('16:20') assert !at.ready?(time_in_day(16, 19)) assert at.ready?(time_in_day(16, 20)) assert !at.ready?(time_in_day(16, 21)) end it '8:20' do at = Clockwork::At.parse('8:20') assert !at.ready?(time_in_day(8, 19)) assert at.ready?(time_in_day(8, 20)) assert !at.ready?(time_in_day(8, 21)) end it '**:20 with two stars' do at = Clockwork::At.parse('**:20') assert !at.ready?(time_in_day(15, 19)) assert at.ready?(time_in_day(15, 20)) assert !at.ready?(time_in_day(15, 21)) assert !at.ready?(time_in_day(16, 19)) assert at.ready?(time_in_day(16, 20)) assert !at.ready?(time_in_day(16, 21)) end it '*:20 with one star' do at = Clockwork::At.parse('*:20') assert !at.ready?(time_in_day(15, 19)) assert at.ready?(time_in_day(15, 20)) assert !at.ready?(time_in_day(15, 21)) assert !at.ready?(time_in_day(16, 19)) assert at.ready?(time_in_day(16, 20)) assert !at.ready?(time_in_day(16, 21)) end it '16:**' do at = Clockwork::At.parse('16:**') assert !at.ready?(time_in_day(15, 59)) assert at.ready?(time_in_day(16, 00)) assert at.ready?(time_in_day(16, 30)) assert at.ready?(time_in_day(16, 59)) assert !at.ready?(time_in_day(17, 00)) end it '8:**' do at = Clockwork::At.parse('8:**') assert !at.ready?(time_in_day(7, 59)) assert at.ready?(time_in_day(8, 00)) assert at.ready?(time_in_day(8, 30)) assert at.ready?(time_in_day(8, 59)) assert !at.ready?(time_in_day(9, 00)) end it 'Saturday 12:00' do at = Clockwork::At.parse('Saturday 12:00') assert !at.ready?(Time.new(2010, 1, 1, 12, 00)) assert at.ready?(Time.new(2010, 1, 2, 12, 00)) # Saturday assert !at.ready?(Time.new(2010, 1, 3, 12, 00)) assert at.ready?(Time.new(2010, 1, 9, 12, 00)) end it 'sat 12:00' do at = Clockwork::At.parse('sat 12:00') assert !at.ready?(Time.new(2010, 1, 1, 12, 00)) assert at.ready?(Time.new(2010, 1, 2, 12, 00)) assert !at.ready?(Time.new(2010, 1, 3, 12, 00)) end it 'invalid time 32:00' do assert_raises Clockwork::At::FailedToParse do Clockwork::At.parse('32:00') end end it 'invalid multi-line with Sat 12:00' do assert_raises Clockwork::At::FailedToParse do Clockwork::At.parse("sat 12:00\nreally invalid time") end end it 'invalid multi-line with 8:30' do assert_raises Clockwork::At::FailedToParse do Clockwork::At.parse("8:30\nreally invalid time") end end it 'invalid multi-line with *:10' do assert_raises Clockwork::At::FailedToParse do Clockwork::At.parse("*:10\nreally invalid time") end end it 'invalid multi-line with 12:**' do assert_raises Clockwork::At::FailedToParse do Clockwork::At.parse("12:**\nreally invalid time") end end end clockwork-2.0.4/test/clockwork_test.rb000066400000000000000000000044711351633044300200640ustar00rootroot00000000000000require File.expand_path('../../lib/clockwork', __FILE__) require 'minitest/autorun' require 'mocha/setup' describe Clockwork do before do @log_output = StringIO.new Clockwork.configure do |config| config[:sleep_timeout] = 0 config[:logger] = Logger.new(@log_output) end IO.stubs(:select) end after do Clockwork.clear! end it 'should run events with configured logger' do run = false Clockwork.handler do |job| run = job == 'myjob' end Clockwork.every(1.minute, 'myjob') Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick) Clockwork.run assert run assert @log_output.string.include?('Triggering') end it 'should log event correctly' do run = false Clockwork.handler do |job| run = job == 'an event' end Clockwork.every(1.minute, 'an event') Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick) Clockwork.run assert run assert @log_output.string.include?("Triggering 'an event'") end it 'should pass event without modification to handler' do event_object = Object.new run = false Clockwork.handler do |job| run = job == event_object end Clockwork.every(1.minute, event_object) Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick) Clockwork.run assert run end it 'should not run anything after reset' do Clockwork.every(1.minute, 'myjob') { } Clockwork.clear! Clockwork.configure do |config| config[:sleep_timeout] = 0 config[:logger] = Logger.new(@log_output) end Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick) Clockwork.run assert @log_output.string.include?('0 events') end it 'should pass all arguments to every' do Clockwork.every(1.second, 'myjob', if: lambda { |_| false }) { } Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick) Clockwork.run assert @log_output.string.include?('1 events') assert !@log_output.string.include?('Triggering') end it 'support module re-open style' do $called = false module ::Clockwork every(1.second, 'myjob') { $called = true } end Clockwork.manager.stubs(:run_tick_loop).returns(Clockwork.manager.tick) Clockwork.run assert $called end end clockwork-2.0.4/test/database_events/000077500000000000000000000000001351633044300176245ustar00rootroot00000000000000clockwork-2.0.4/test/database_events/event_store_test.rb000066400000000000000000000012531351633044300235460ustar00rootroot00000000000000require "minitest/autorun" require 'clockwork/database_events/event_store' require 'clockwork/database_events/event_collection' describe Clockwork::DatabaseEvents::EventStore do described_class = Clockwork::DatabaseEvents::EventStore EventCollection = Clockwork::DatabaseEvents::EventCollection describe '#register' do it 'adds the event to the event group' do event_group = EventCollection.new EventCollection.stubs(:new).returns(event_group) event = OpenStruct.new model = OpenStruct.new id: 1 subject = described_class.new(Proc.new {}) event_group.expects(:add).with(event) subject.register(event, model) end end endclockwork-2.0.4/test/database_events/support/000077500000000000000000000000001351633044300213405ustar00rootroot00000000000000clockwork-2.0.4/test/database_events/support/active_record_fake.rb000066400000000000000000000022721351633044300254670ustar00rootroot00000000000000module ActiveRecordFake def self.included(base) base.instance_variable_set(:@items, []) base.instance_variable_set(:@next_id, 1) base.extend(ClassMethods) end attr_accessor :id def initialize options={} @id = options.fetch(:id) { self.class.get_next_id } set_attribute_values_from_options options.reject{|key, value| key == :id } self.class.add self end def delete! self.class.remove(self) end def update options={} set_attribute_values_from_options options end def attributes Hash[instance_variables.map { |name| [name, instance_variable_get(name)] } ] end module ClassMethods def create *args new *args end def delete_all @items.clear reset_id true end def all @items.dup end def add instance @items << instance end def remove instance @items.delete(instance) end def get_next_id id = @next_id @next_id += 1 id end def reset_id @next_id = 1 end end private def set_attribute_values_from_options options options.each{|attr, value| self.send("#{attr}=".to_sym, value) } end endclockwork-2.0.4/test/database_events/synchronizer_test.rb000066400000000000000000000265421351633044300237560ustar00rootroot00000000000000require "minitest/autorun" require 'mocha/setup' require 'time' require 'active_support/time' require_relative '../../lib/clockwork' require_relative '../../lib/clockwork/database_events' require_relative 'test_helpers' describe Clockwork::DatabaseEvents::Synchronizer do before do @now = Time.now Clockwork.manager = @manager = Clockwork::DatabaseEvents::Manager.new class << @manager def log(msg); end # silence log output end end after do Clockwork.clear! DatabaseEventModel.delete_all DatabaseEventModel2.delete_all DatabaseEventModelWithIf.delete_all end describe "setup" do before do @subject = Clockwork::DatabaseEvents::Synchronizer end describe "arguments" do it 'raises argument error if model is not set' do error = assert_raises KeyError do @subject.setup(every: 1.minute) {} end assert_equal error.message, ":model must be set to the model class" end it 'raises argument error if every is not set' do error = assert_raises KeyError do @subject.setup(model: DatabaseEventModel) {} end assert_equal error.message, ":every must be set to the database sync frequency" end end describe "when database reload frequency is greater than model frequency period" do before do @events_run = [] @sync_frequency = 1.minute end it 'fetches and registers event from database' do DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) tick_at(@now, :and_every_second_for => 1.second) assert_equal ["DatabaseEventModel:1"], @events_run end it 'fetches and registers multiple events from database' do DatabaseEventModel.create(:frequency => 10) DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) tick_at(@now, :and_every_second_for => 1.second) assert_equal ["DatabaseEventModel:1", "DatabaseEventModel:2"], @events_run end it 'does not run event again before frequency specified in database' do model = DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) tick_at(@now, :and_every_second_for => model.frequency - 1.second) assert_equal 1, @events_run.length end it 'runs event repeatedly with frequency specified in database' do model = DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) tick_at(@now, :and_every_second_for => (2 * model.frequency) + 1.second) assert_equal 3, @events_run.length end it 'runs reloaded events from database repeatedly' do model = DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) tick_at(@now, :and_every_second_for => @sync_frequency - 1) model.update(:name => "DatabaseEventModel:1:Reloaded") tick_at(@now + @sync_frequency, :and_every_second_for => model.frequency * 2) assert_equal ["DatabaseEventModel:1:Reloaded", "DatabaseEventModel:1:Reloaded"], @events_run[-2..-1] end it 'updates modified event frequency with event reloading' do model = DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) tick_at(@now, :and_every_second_for => @sync_frequency - 1.second) model.update(:frequency => 5) tick_at(@now + @sync_frequency, :and_every_second_for => 6.seconds) # model runs at: 1, 11, 21, 31, 41, 51 (6 runs) # database sync happens at: 60 # modified model runs at: 61 (next tick after reload) and then 66 (2 runs) assert_equal 8, @events_run.length end it 'stoped running deleted events from database' do model = DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) tick_at(@now, :and_every_second_for => (@sync_frequency - 1.second)) before = @events_run.dup model.delete! tick_at(@now + @sync_frequency, :and_every_second_for => @sync_frequency) after = @events_run assert_equal before, after end it 'updates event name with new name' do model = DatabaseEventModel.create(:frequency => 10.seconds) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) tick_at @now, :and_every_second_for => @sync_frequency - 1.second @events_run.clear model.update(:name => "DatabaseEventModel:1_modified") tick_at @now + @sync_frequency, :and_every_second_for => (model.frequency * 2) assert_equal ["DatabaseEventModel:1_modified", "DatabaseEventModel:1_modified"], @events_run end it 'updates event frequency with new frequency' do model = DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) tick_at @now, :and_every_second_for => @sync_frequency - 1.second @events_run.clear model.update(:frequency => 30) tick_at @now + @sync_frequency, :and_every_second_for => @sync_frequency - 1.seconds assert_equal 2, @events_run.length end it 'updates event at with new at' do model = DatabaseEventModel.create(:frequency => 1.day, :at => '10:30') setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) assert_will_run 'jan 1 2010 10:30:00' assert_wont_run 'jan 1 2010 09:30:00' model.update(:at => '09:30') tick_at @now, :and_every_second_for => @sync_frequency + 1.second assert_will_run 'jan 1 2010 09:30:00' assert_wont_run 'jan 1 2010 10:30:00' end describe "when #name is defined" do it 'runs daily event with at from databse only once' do DatabaseEventModel.create(:frequency => 1.day, :at => next_minute(@now).strftime('%H:%M')) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) # tick from now, though specified :at time tick_at(@now, :and_every_second_for => (2 * @sync_frequency) + 1.second) assert_equal 1, @events_run.length end end describe "when #name is not defined" do it 'runs daily event with at from databse only once' do DatabaseEventModelWithoutName.create(:frequency => 1.day, :at => next_minute(next_minute(@now)).strftime('%H:%M')) setup_sync(model: DatabaseEventModelWithoutName, :every => @sync_frequency, :events_run => @events_run) # tick from now, though specified :at time tick_at(@now, :and_every_second_for => (2 * @sync_frequency) + 1.second) assert_equal 1, @events_run.length end end it 'creates multiple event ats with comma separated at string' do DatabaseEventModel.create(:frequency => 1.day, :at => '16:20, 18:10') setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) tick_at @now, :and_every_second_for => 1.second assert_wont_run 'jan 1 2010 16:19:59' assert_will_run 'jan 1 2010 16:20:00' assert_wont_run 'jan 1 2010 16:20:01' assert_wont_run 'jan 1 2010 18:09:59' assert_will_run 'jan 1 2010 18:10:00' assert_wont_run 'jan 1 2010 18:10:01' end it 'allows syncing multiple database models' do DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => @sync_frequency, :events_run => @events_run) DatabaseEventModel2.create(:frequency => 10) setup_sync(model: DatabaseEventModel2, :every => @sync_frequency, :events_run => @events_run) tick_at(@now, :and_every_second_for => 1.second) assert_equal ["DatabaseEventModel:1", "DatabaseEventModel2:1"], @events_run end end describe "when database reload frequency is less than model frequency period" do before do @events_run = [] end it 'runs event only once within the model frequency period' do DatabaseEventModel.create(:frequency => 5.minutes) setup_sync(model: DatabaseEventModel, :every => 1.minute, :events_run => @events_run) tick_at(@now, :and_every_second_for => 5.minutes) assert_equal 1, @events_run.length end end describe "with database event :at set to empty string" do before do @events_run = [] DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => 1.minute, :events_run => @events_run) end it 'does not raise an error' do begin tick_at(Time.now, :and_every_second_for => 10.seconds) rescue => e assert false, "Raised an error: #{e.message}" end end it 'runs the event' do begin tick_at(Time.now, :and_every_second_for => 10.seconds) rescue end assert_equal 1, @events_run.length end end describe "with model that responds to `if?`" do before do @events_run = [] end describe "when model.if? is true" do it 'runs' do DatabaseEventModelWithIf.create(:if_state => true, :frequency => 10) setup_sync(model: DatabaseEventModelWithIf, :every => 1.minute, :events_run => @events_run) tick_at(@now, :and_every_second_for => 9.seconds) assert_equal 1, @events_run.length end end describe "when model.if? is false" do it 'does not run' do DatabaseEventModelWithIf.create(:if_state => false, :frequency => 10, :name => 'model with if?') setup_sync(model: DatabaseEventModelWithIf, :every => 1.minute, :events_run => @events_run) tick_at(@now, :and_every_second_for => 1.minute) # require 'byebug' # byebug if events_run.length > 0 assert_equal 0, @events_run.length end end end describe "with task that responds to `tz`" do before do @events_run = [] @utc_time_now = Time.now.utc DatabaseEventModel.create(:frequency => 1.days, :at => @utc_time_now.strftime('%H:%M'), :tz => 'America/Montreal') setup_sync(model: DatabaseEventModel, :every => 1.minute, :events_run => @events_run) end it 'does not raise an error' do begin tick_at(@utc_time_now, :and_every_second_for => 10.seconds) rescue => e assert false, "Raised an error: #{e.message}" end end it 'does not run the event based on UTC' do begin tick_at(@utc_time_now, :and_every_second_for => 3.hours) rescue end assert_equal 0, @events_run.length end it 'runs the event based on America/Montreal tz' do begin tick_at(@utc_time_now, :and_every_second_for => 5.hours) rescue end assert_equal 1, @events_run.length end end end end clockwork-2.0.4/test/database_events/test_helpers.rb000066400000000000000000000032321351633044300226520ustar00rootroot00000000000000require_relative 'support/active_record_fake' def setup_sync(options={}) model_class = options.fetch(:model) { raise KeyError, ":model must be set to the model class" } frequency = options.fetch(:every) { raise KeyError, ":every must be set to the database sync frequency" } events_run = options.fetch(:events_run) { raise KeyError, ":events_run must be provided"} Clockwork::DatabaseEvents::Synchronizer.setup model: model_class, every: frequency do |model| name = model.respond_to?(:name) ? model.name : model.to_s events_run << name end end def assert_will_run(t) assert_equal 1, @manager.tick(normalize_time(t)).size end def assert_wont_run(t) assert_equal 0, @manager.tick(normalize_time(t)).size end def tick_at(now = Time.now, options = {}) seconds_to_tick_for = options[:and_every_second_for] || 0 number_of_ticks = 1 + seconds_to_tick_for number_of_ticks.times{|i| @manager.tick(now + i) } end def next_minute(now = Time.now) Time.at((now.to_i / 60 + 1) * 60) end def normalize_time t t.is_a?(String) ? Time.parse(t) : t end class DatabaseEventModel include ActiveRecordFake attr_accessor :name, :frequency, :at, :tz def name @name || "#{self.class}:#{id}" end end class DatabaseEventModel2 include ActiveRecordFake attr_accessor :name, :frequency, :at, :tz def name @name || "#{self.class}:#{id}" end end class DatabaseEventModelWithoutName include ActiveRecordFake attr_accessor :frequency, :at end class DatabaseEventModelWithIf include ActiveRecordFake attr_accessor :name, :frequency, :at, :tz, :if_state def name @name || "#{self.class}:#{id}" end def if?(time) @if_state end endclockwork-2.0.4/test/event_test.rb000066400000000000000000000041431351633044300172030ustar00rootroot00000000000000require File.expand_path('../../lib/clockwork', __FILE__) require "minitest/autorun" describe Clockwork::Event do describe '#thread?' do before do @manager = Class.new end describe 'manager config thread option set to true' do before do @manager.stubs(:config).returns({ :thread => true }) end it 'is true' do event = Clockwork::Event.new(@manager, nil, nil, nil) assert_equal true, event.thread? end it 'is false when event thread option set' do event = Clockwork::Event.new(@manager, nil, nil, nil, :thread => false) assert_equal false, event.thread? end end describe 'manager config thread option not set' do before do @manager.stubs(:config).returns({}) end it 'is true if event thread option is true' do event = Clockwork::Event.new(@manager, nil, nil, nil, :thread => true) assert_equal true, event.thread? end end end describe '#run_now?' do before do @manager = Class.new @manager.stubs(:config).returns({}) end describe 'event skip_first_run option set to true' do it 'returns false on first attempt' do event = Clockwork::Event.new(@manager, 1, nil, nil, :skip_first_run => true) assert_equal false, event.run_now?(Time.now) end it 'returns true on subsequent attempts' do event = Clockwork::Event.new(@manager, 1, nil, nil, :skip_first_run => true) # first run event.run_now?(Time.now) # second run assert_equal true, event.run_now?(Time.now + 1) end end describe 'event skip_first_run option not set' do it 'returns true on first attempt' do event = Clockwork::Event.new(@manager, 1, nil, nil) assert_equal true, event.run_now?(Time.now + 1) end end describe 'event skip_first_run option set to false' do it 'returns true on first attempt' do event = Clockwork::Event.new(@manager, 1, nil, nil, :skip_first_run => false) assert_equal true, event.run_now?(Time.now) end end end end clockwork-2.0.4/test/manager_test.rb000066400000000000000000000252001351633044300174710ustar00rootroot00000000000000require File.expand_path('../../lib/clockwork', __FILE__) require "minitest/autorun" require 'mocha/setup' require 'time' require 'active_support/time' describe Clockwork::Manager do before do @manager = Clockwork::Manager.new class << @manager def log(msg); end end @manager.handler { } end def assert_will_run(t) if t.is_a? String t = Time.parse(t) end assert_equal 1, @manager.tick(t).size end def assert_wont_run(t) if t.is_a? String t = Time.parse(t) end assert_equal 0, @manager.tick(t).size end it "once a minute" do @manager.every(1.minute, 'myjob') assert_will_run(t=Time.now) assert_wont_run(t+30) assert_will_run(t+60) end it "every three minutes" do @manager.every(3.minutes, 'myjob') assert_will_run(t=Time.now) assert_wont_run(t+2*60) assert_will_run(t+3*60) end it "once an hour" do @manager.every(1.hour, 'myjob') assert_will_run(t=Time.now) assert_wont_run(t+30*60) assert_will_run(t+60*60) end it "once a week" do @manager.every(1.week, 'myjob') assert_will_run(t=Time.now) assert_wont_run(t+60*60*24*6) assert_will_run(t+60*60*24*7) end it "won't drift later and later" do @manager.every(1.hour, 'myjob') assert_will_run(Time.parse("10:00:00.5")) assert_wont_run(Time.parse("10:59:59.999")) assert_will_run(Time.parse("11:00:00.0")) end it "aborts when no handler defined" do manager = Clockwork::Manager.new assert_raises(Clockwork::Manager::NoHandlerDefined) do manager.every(1.minute, 'myjob') end end it "aborts when fails to parse" do assert_raises(Clockwork::At::FailedToParse) do @manager.every(1.day, "myjob", :at => "a:bc") end end it "general handler" do $set_me = 0 @manager.handler { $set_me = 1 } @manager.every(1.minute, 'myjob') @manager.tick(Time.now) assert_equal 1, $set_me end it "event-specific handler" do $set_me = 0 @manager.every(1.minute, 'myjob') { $set_me = 2 } @manager.tick(Time.now) assert_equal 2, $set_me end it "should pass time to the general handler" do received = nil now = Time.now @manager.handler { |job, time| received = time } @manager.every(1.minute, 'myjob') @manager.tick(now) assert_equal now, received end it "should pass time to the event-specific handler" do received = nil now = Time.now @manager.every(1.minute, 'myjob') { |job, time| received = time } @manager.tick(now) assert_equal now, received end it "exceptions are trapped and logged" do @manager.handler { raise 'boom' } @manager.every(1.minute, 'myjob') mocked_logger = MiniTest::Mock.new mocked_logger.expect :error, true, [RuntimeError] @manager.configure { |c| c[:logger] = mocked_logger } @manager.tick(Time.now) mocked_logger.verify end it "exceptions still set the last timestamp to avoid spastic error loops" do @manager.handler { raise 'boom' } event = @manager.every(1.minute, 'myjob') @manager.stubs(:log_error) @manager.tick(t = Time.now) assert_equal t, event.last end it "should be configurable" do @manager.configure do |config| config[:sleep_timeout] = 200 config[:logger] = "A Logger" config[:max_threads] = 10 config[:thread] = true end assert_equal 200, @manager.config[:sleep_timeout] assert_equal "A Logger", @manager.config[:logger] assert_equal 10, @manager.config[:max_threads] assert_equal true, @manager.config[:thread] end it "configuration should have reasonable defaults" do assert_equal 1, @manager.config[:sleep_timeout] assert @manager.config[:logger].is_a?(Logger) assert_equal 10, @manager.config[:max_threads] assert_equal false, @manager.config[:thread] end it "should accept unnamed job" do event = @manager.every(1.minute) assert_equal 'unnamed', event.job end it "should accept options without job name" do event = @manager.every(1.minute, {}) assert_equal 'unnamed', event.job end describe ':at option' do it "once a day at 16:20" do @manager.every(1.day, 'myjob', :at => '16:20') assert_wont_run 'jan 1 2010 16:19:59' assert_will_run 'jan 1 2010 16:20:00' assert_wont_run 'jan 1 2010 16:20:01' assert_wont_run 'jan 2 2010 16:19:59' assert_will_run 'jan 2 2010 16:20:00' end it "twice a day at 16:20 and 18:10" do @manager.every(1.day, 'myjob', :at => ['16:20', '18:10']) assert_wont_run 'jan 1 2010 16:19:59' assert_will_run 'jan 1 2010 16:20:00' assert_wont_run 'jan 1 2010 16:20:01' assert_wont_run 'jan 1 2010 18:09:59' assert_will_run 'jan 1 2010 18:10:00' assert_wont_run 'jan 1 2010 18:10:01' end end describe ':tz option' do it "time zone is not set by default" do assert @manager.config[:tz].nil? end it "should be able to specify a different timezone than local" do @manager.every(1.day, 'myjob', :at => '10:00', :tz => 'UTC') assert_wont_run 'jan 1 2010 10:00:00 EST' assert_will_run 'jan 1 2010 10:00:00 UTC' end it "should be able to specify a different timezone than local for multiple times" do @manager.every(1.day, 'myjob', :at => ['10:00', '8:00'], :tz => 'UTC') assert_wont_run 'jan 1 2010 08:00:00 EST' assert_will_run 'jan 1 2010 08:00:00 UTC' assert_wont_run 'jan 1 2010 10:00:00 EST' assert_will_run 'jan 1 2010 10:00:00 UTC' end it "should be able to configure a default timezone to use for all events" do @manager.configure { |config| config[:tz] = 'UTC' } @manager.every(1.day, 'myjob', :at => '10:00') assert_wont_run 'jan 1 2010 10:00:00 EST' assert_will_run 'jan 1 2010 10:00:00 UTC' end it "should be able to override a default timezone in an event" do @manager.configure { |config| config[:tz] = 'UTC' } @manager.every(1.day, 'myjob', :at => '10:00', :tz => 'EST') assert_will_run 'jan 1 2010 10:00:00 EST' assert_wont_run 'jan 1 2010 10:00:00 UTC' end end describe ':if option' do it ":if true then always run" do @manager.every(1.second, 'myjob', :if => lambda { |_| true }) assert_will_run 'jan 1 2010 16:20:00' end it ":if false then never run" do @manager.every(1.second, 'myjob', :if => lambda { |_| false }) assert_wont_run 'jan 1 2010 16:20:00' end it ":if the first day of month" do @manager.every(1.second, 'myjob', :if => lambda { |t| t.day == 1 }) assert_will_run 'jan 1 2010 16:20:00' assert_wont_run 'jan 2 2010 16:20:00' assert_will_run 'feb 1 2010 16:20:00' end it ":if it is compared to a time with zone" do tz = 'America/Chicago' time = Time.utc(2012,5,25,10,00) @manager.every(1.second, 'myjob', tz: tz, :if => lambda { |t| ((time - 1.hour)..(time + 1.hour)).cover? t }) assert_will_run time end it ":if is not callable then raise ArgumentError" do assert_raises(ArgumentError) do @manager.every(1.second, 'myjob', :if => true) end end end describe "max_threads" do it "should warn when an event tries to generate threads more than max_threads" do logger = Logger.new(STDOUT) @manager.configure do |config| config[:max_threads] = 1 config[:logger] = logger end @manager.every(1.minute, 'myjob1', :thread => true) { sleep 2 } @manager.every(1.minute, 'myjob2', :thread => true) { sleep 2 } logger.expects(:error).with("Threads exhausted; skipping myjob2") @manager.tick(Time.now) end it "should not warn when thread is managed by others" do begin t = Thread.new { sleep 5 } logger = Logger.new(StringIO.new) @manager.configure do |config| config[:max_threads] = 1 config[:logger] = logger end @manager.every(1.minute, 'myjob', :thread => true) logger.expects(:error).never @manager.tick(Time.now) ensure t.kill end end end describe "callbacks" do it "should not accept unknown callback name" do assert_raises(RuntimeError, "Unsupported callback unknown_callback") do @manager.on(:unknown_callback) do true end end end it "should run before_tick callback once on tick" do counter = 0 @manager.on(:before_tick) do counter += 1 end @manager.tick assert_equal 1, counter end it "should not run events if before_tick returns false" do @manager.on(:before_tick) do false end @manager.every(1.second, 'myjob') { raise "should not run" } @manager.tick end it "should run before_run twice if two events are registered" do counter = 0 @manager.on(:before_run) do counter += 1 end @manager.every(1.second, 'myjob') @manager.every(1.second, 'myjob2') @manager.tick assert_equal 2, counter end it "should run even jobs only" do counter = 0 ran = false @manager.on(:before_run) do counter += 1 counter % 2 == 0 end @manager.every(1.second, 'myjob') { raise "should not ran" } @manager.every(1.second, 'myjob2') { ran = true } @manager.tick assert ran end it "should run after_run callback for each event" do counter = 0 @manager.on(:after_run) do counter += 1 end @manager.every(1.second, 'myjob') @manager.every(1.second, 'myjob2') @manager.tick assert_equal 2, counter end it "should run after_tick callback once" do counter = 0 @manager.on(:after_tick) do counter += 1 end @manager.tick assert_equal 1, counter end end describe 'error_handler' do before do @errors = [] @manager.error_handler do |e| @errors << e end # block error log @string_io = StringIO.new @manager.configure do |config| config[:logger] = Logger.new(@string_io) end @manager.every(1.second, 'myjob') { raise 'it error' } end it 'registered error_handler handles error from event' do @manager.tick assert_equal ['it error'], @errors.map(&:message) end it 'error is notified to logger and handler' do @manager.tick assert @string_io.string.include?('it error') end it 'error in handler will NOT be suppressed' do @manager.error_handler do |e| raise e.message + ' re-raised' end assert_raises(RuntimeError, 'it error re-raised') do @manager.tick end end end end clockwork-2.0.4/test/samples/000077500000000000000000000000001351633044300161405ustar00rootroot00000000000000clockwork-2.0.4/test/samples/signal_test.rb000066400000000000000000000006211351633044300210000ustar00rootroot00000000000000require 'clockwork' require 'active_support/time' module Clockwork LOGFILE = File.expand_path('../../tmp/signal_test.log', __FILE__) handler do |job| File.write(LOGFILE, 'start') sleep 0.1 File.write(LOGFILE, 'done') end configure do |config| config[:sleep_timeout] = 0 config[:logger] = Logger.new(StringIO.new) end every(1.seconds, 'run.me.every.1.seconds') end clockwork-2.0.4/test/signal_test.rb000066400000000000000000000014441351633044300173400ustar00rootroot00000000000000require 'test/unit' require 'mocha/setup' require 'fileutils' class SignalTest < Test::Unit::TestCase CMD = File.expand_path('../../bin/clockwork', __FILE__) SAMPLE = File.expand_path('../samples/signal_test.rb', __FILE__) LOGFILE = File.expand_path('../tmp/signal_test.log', __FILE__) setup do FileUtils.mkdir_p(File.dirname(LOGFILE)) @pid = spawn(CMD, SAMPLE) until File.exist?(LOGFILE) sleep 0.1 end end teardown do FileUtils.rm_r(File.dirname(LOGFILE)) end test 'should gracefully shutdown with SIGTERM' do Process.kill(:TERM, @pid) sleep 0.2 assert_equal 'done', File.read(LOGFILE) end test 'should forcely shutdown with SIGINT' do Process.kill(:INT, @pid) sleep 0.2 assert_equal 'start', File.read(LOGFILE) end end