clockwork-1.2.0/0000755000004100000410000000000012556451264013552 5ustar www-datawww-dataclockwork-1.2.0/Rakefile0000644000004100000410000000025612556451264015222 0ustar www-datawww-datarequire '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-1.2.0/bin/0000755000004100000410000000000012556451264014322 5ustar www-datawww-dataclockwork-1.2.0/bin/clockworkd0000755000004100000410000000651112556451264016415 0ustar www-datawww-data#!/usr/bin/env ruby STDERR.sync = STDOUT.sync = true require 'clockwork' require 'optparse' 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 @options[:file].match(/^[\/.]/) @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-1.2.0/bin/clockwork0000755000004100000410000000041012556451264016241 0ustar www-datawww-data#!/usr/bin/env ruby STDERR.sync = STDOUT.sync = true require 'clockwork' usage = 'clockwork ' file = ARGV.shift or abort usage file = "./#{file}" unless file.match(/^[\/.]/) require file trap('INT') do puts "\rExiting" exit end Clockwork::run clockwork-1.2.0/Gemfile0000644000004100000410000000004712556451264015046 0ustar www-datawww-datasource 'https://rubygems.org' gemspec clockwork-1.2.0/clockwork.gemspec0000644000004100000410000000206312556451264017116 0ustar www-datawww-dataGem::Specification.new do |s| s.name = "clockwork" s.version = "1.2.0" 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/tomykaira/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 "bundler", "~> 1.3" s.add_development_dependency "rake" s.add_development_dependency "daemons" s.add_development_dependency "test-unit" s.add_development_dependency "minitest", "~> 4.0" s.add_development_dependency "mocha" end clockwork-1.2.0/example.rb0000644000004100000410000000106612556451264015535 0ustar www-datawww-datarequire '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-1.2.0/.travis.yml0000644000004100000410000000030512556451264015661 0ustar www-datawww-datalanguage: ruby sudo: false cache: bundler rvm: - 1.9.3 - 2.0.0 - 2.1 - 2.2 - jruby-19mode - # rbx-2.2.7 gemfile: - gemfiles/activesupport3.gemfile - gemfiles/activesupport4.gemfile clockwork-1.2.0/lib/0000755000004100000410000000000012556451264014320 5ustar www-datawww-dataclockwork-1.2.0/lib/clockwork/0000755000004100000410000000000012556451264016316 5ustar www-datawww-dataclockwork-1.2.0/lib/clockwork/database_events/0000755000004100000410000000000012556451264021446 5ustar www-datawww-dataclockwork-1.2.0/lib/clockwork/database_events/sync_performer.rb0000644000004100000410000000531412556451264025033 0ustar www-datawww-datarequire_relative '../database_events' module Clockwork module DatabaseEvents class SyncPerformer PERFORMERS = [] def self.setup(options={}, &block) 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" } sync_performer = self.new(model_class, &block) # 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 sync_performer.sync end end def initialize(model_class, &proc) @model_class = model_class @block = proc @database_event_registry = Registry.new PERFORMERS << self end # delegates to Registry def register(event, model) @database_event_registry.register(event, model) end # Ensure clockwork events reflect events from database-backed model # Adds any new events, modifies updated ones, and delets removed ones def sync model_ids_that_exist = [] @model_class.all.each do |model| model_ids_that_exist << model.id if are_different?(@database_event_registry.event_for(model), model) create_or_recreate_event(model) end end @database_event_registry.unregister_all_except(model_ids_that_exist) end private def are_different?(event, model) return true if event.nil? event.name_or_frequency_has_changed?(model) || ats_have_changed?(model) end def ats_have_changed?(model) model_ats = ats_array_for_event(model) event_ats = ats_array_from_model(model) model_ats != event_ats end def ats_array_for_event(model) @database_event_registry.events_for(model).collect{|event| event.at }.compact end def ats_array_from_model(model) (at_strings_for(model) || []).collect{|at| At.parse(at) } end def at_strings_for(model) model.at.to_s.empty? ? nil : model.at.split(',').map(&:strip) end def create_or_recreate_event(model) if @database_event_registry.event_for(model) @database_event_registry.unregister(model) end options = { :from_database => true, :sync_performer => self, :at => at_strings_for(model) } options[:tz] = model.tz if model.respond_to?(:tz) # we pass actual model instance as the job, rather than just name Clockwork.manager.every model.frequency, model, options, &@block end end end endclockwork-1.2.0/lib/clockwork/database_events/manager.rb0000644000004100000410000000076712556451264023417 0ustar www-datawww-datamodule 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] Clockwork::DatabaseEvents::Event.new(self, period, job, (block || handler), options.fetch(:sync_performer), options) else Clockwork::Event.new(self, period, job, block || handler, options) end end end end end clockwork-1.2.0/lib/clockwork/database_events/event.rb0000644000004100000410000000151012556451264023111 0ustar www-datawww-datamodule Clockwork module DatabaseEvents class Event < Clockwork::Event attr_accessor :sync_performer, :at def initialize(manager, period, job, block, sync_performer, options={}) super(manager, period, job, block, options) @sync_performer = sync_performer @sync_performer.register(self, job) end def name (job.respond_to?(:name) && job.name) ? job.name : "#{job.class}:#{job.id}" end def to_s name end def name_or_frequency_has_changed?(model) name_has_changed?(model) || frequency_has_changed?(model) end private def name_has_changed?(model) job.respond_to?(:name) && job.name != model.name end def frequency_has_changed?(model) @period != model.frequency end end end endclockwork-1.2.0/lib/clockwork/database_events/registry.rb0000644000004100000410000000140512556451264023643 0ustar www-datawww-datamodule Clockwork module DatabaseEvents class Registry def initialize @events = Hash.new [] end def register(event, model) @events[model.id] = @events[model.id] + [event] end def unregister(model) unregister_by_id(model.id) end def unregister_by_id(id) @events[id].each{|e| Clockwork.manager.unregister(e) } @events.delete(id) end def unregister_all_except(ids) (@events.keys - ids).each{|id| unregister_by_id(id) } end # all events of same id will have same frequency/name, just different ats def event_for(model) events_for(model).first end def events_for(model) @events[model.id] end end end endclockwork-1.2.0/lib/clockwork/manager.rb0000644000004100000410000000546712556451264020271 0ustar www-datawww-datamodule Clockwork class Manager class NoHandlerDefined < RuntimeError; end attr_reader :config def initialize @events = [] @callbacks = {} @config = default_configuration @handler = nil 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(' ')} ]" loop do tick interval = config[:sleep_timeout] - Time.now.subsec + 0.001 sleep(interval) if interval > 0 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 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-1.2.0/lib/clockwork/database_events.rb0000644000004100000410000000136712556451264022002 0ustar www-datawww-datarequire_relative 'database_events/event' require_relative 'database_events/sync_performer' require_relative 'database_events/registry' 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::SyncPerformer.setup(options, &block) end end extend Methods module DatabaseEvents end endclockwork-1.2.0/lib/clockwork/event.rb0000644000004100000410000000307512556451264017771 0ustar www-datawww-datamodule 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]) @last = nil @block = block @if = options[:if] @thread = options.fetch(:thread, @manager.config[:thread]) @timezone = options.fetch(:tz, @manager.config[:tz]) end def convert_timezone(t) @timezone ? t.in_time_zone(@timezone) : t end def run_now?(t) t = convert_timezone(t) elapsed_ready(t) and (@at.nil? or @at.ready?(t)) and (@if.nil? or @if.call(t)) 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 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-1.2.0/lib/clockwork/at.rb0000644000004100000410000000312012556451264017243 0ustar www-datawww-datamodule 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-1.2.0/lib/clockwork.rb0000644000004100000410000000172512556451264016650 0ustar www-datawww-datarequire '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-1.2.0/gemfiles/0000755000004100000410000000000012556451264015345 5ustar www-datawww-dataclockwork-1.2.0/gemfiles/activesupport4.gemfile0000644000004100000410000000027612556451264021700 0ustar www-datawww-datasource 'https://rubygems.org' platforms :rbx do gem 'rubysl', '~> 2.0' gem 'rubysl-test-unit' gem 'rubinius-developer_tools' end gem 'activesupport', '~> 4.0.0' gemspec :path=>"../" clockwork-1.2.0/gemfiles/activesupport3.gemfile0000644000004100000410000000027712556451264021700 0ustar www-datawww-datasource 'https://rubygems.org' platforms :rbx do gem 'rubysl', '~> 2.0' gem 'rubysl-test-unit' gem 'rubinius-developer_tools' end gem 'activesupport', '~> 3.2.14' gemspec :path=>"../" clockwork-1.2.0/metadata.yml0000644000004100000410000001156412556451264016064 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: clockwork version: !ruby/object:Gem::Version version: 1.2.0 platform: ruby authors: - Adam Wiggins - tomykaira autorequire: bindir: bin cert_chain: [] date: 2015-04-14 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: tzinfo requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: activesupport requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: bundler requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '1.3' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '1.3' - !ruby/object:Gem::Dependency name: rake requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: daemons requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: test-unit requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: minitest requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '4.0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '4.0' - !ruby/object:Gem::Dependency name: mocha requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' 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. email: - adam@heroku.com - tomykaira@gmail.com executables: - clockwork - clockworkd extensions: [] extra_rdoc_files: - README.md files: - ".gitignore" - ".travis.yml" - Gemfile - LICENSE - README.md - Rakefile - bin/clockwork - bin/clockworkd - clockwork.gemspec - clockworkd.1 - example.rb - gemfiles/activesupport3.gemfile - gemfiles/activesupport4.gemfile - lib/clockwork.rb - lib/clockwork/at.rb - lib/clockwork/database_events.rb - lib/clockwork/database_events/event.rb - lib/clockwork/database_events/manager.rb - lib/clockwork/database_events/registry.rb - lib/clockwork/database_events/sync_performer.rb - lib/clockwork/event.rb - lib/clockwork/manager.rb - test/at_test.rb - test/clockwork_test.rb - test/database_events/support/active_record_fake.rb - test/database_events/sync_performer_test.rb - test/database_events/test_helpers.rb - test/event_test.rb - test/manager_test.rb homepage: http://github.com/tomykaira/clockwork licenses: - MIT metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 2.2.2 signing_key: specification_version: 4 summary: A scheduler process to replace cron. test_files: - test/at_test.rb - test/clockwork_test.rb - test/database_events/support/active_record_fake.rb - test/database_events/sync_performer_test.rb - test/database_events/test_helpers.rb - test/event_test.rb - test/manager_test.rb clockwork-1.2.0/test/0000755000004100000410000000000012556451264014531 5ustar www-datawww-dataclockwork-1.2.0/test/database_events/0000755000004100000410000000000012556451264017661 5ustar www-datawww-dataclockwork-1.2.0/test/database_events/test_helpers.rb0000644000004100000410000000271312556451264022712 0ustar www-datawww-datarequire_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::SyncPerformer.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 endclockwork-1.2.0/test/database_events/sync_performer_test.rb0000644000004100000410000002614412556451264024311 0ustar www-datawww-datarequire 'test/unit' require 'mocha/setup' require 'time' require 'active_support/time' require 'active_support/test_case' require_relative '../../lib/clockwork' require_relative '../../lib/clockwork/database_events' require_relative 'test_helpers' module DatabaseEvents class SyncPerformerTest < ActiveSupport::TestCase setup do @now = Time.now DatabaseEventModel.delete_all DatabaseEventModel2.delete_all Clockwork.manager = @manager = Clockwork::DatabaseEvents::Manager.new class << @manager def log(msg); end # silence log output end end describe "setup" do setup do @subject = Clockwork::DatabaseEvents::SyncPerformer end describe "arguments" do def test_does_not_raise_error_with_valid_arguments @subject.setup(model: DatabaseEventModel, every: 1.minute) {} end def test_raises_argument_error_if_model_is_not_set error = assert_raises KeyError do @subject.setup(every: 1.minute) {} end assert_equal error.message, ":model must be set to the model class" end def test_raises_argument_error_if_every_is_not_set 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 context "when database reload frequency is greater than model frequency period" do setup do @events_run = [] @sync_frequency = 1.minute end def test_fetches_and_registers_event_from_database 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 def test_multiple_events_from_database_can_be_registered 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 def test_event_from_database_does_not_run_again_before_frequency_specified_in_database 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 def test_event_from_database_runs_repeatedly_with_frequency_specified_in_database 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 def test_reloaded_events_from_database_run_repeatedly 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 def test_reloading_events_from_database_with_modified_frequency_will_run_with_new_frequency 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 def test_stops_running_deleted_events_from_database 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 def test_event_from_database_with_edited_name_switches_to_new_name 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 def test_event_from_database_with_edited_frequency_switches_to_new_frequency 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 def test_event_from_database_with_edited_at_runs_at_new_at 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 context "when #name is defined" do def test_daily_event_from_database_with_at_should_only_run_once 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 context "when #name is not defined" do def test_daily_event_from_database_with_at_should_only_run_once 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 def test_event_from_database_with_comma_separated_at_leads_to_multiple_event_ats 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 def test_syncing_multiple_database_models_works 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 context "when database reload frequency is less than model frequency period" do setup do @events_run = [] end def test_the_event_only_runs_once_within_the_model_frequency_period 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 context "with database event with :at as empty string" do setup do @events_run = [] DatabaseEventModel.create(:frequency => 10) setup_sync(model: DatabaseEventModel, :every => 1.minute, :events_run => @events_run) end def test_it_does_not_raise_an_error begin tick_at(Time.now, :and_every_second_for => 10.seconds) rescue => e assert false, "Raised an error: #{e.message}" end end def test_the_event_runs begin tick_at(Time.now, :and_every_second_for => 10.seconds) rescue => e end assert_equal 1, @events_run.length end end context "with task that respond to `tz`" do setup 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 def test_it_does_not_raise_an_error begin tick_at(@utc_time_now, :and_every_second_for => 10.seconds) rescue => e assert false, "Raised an error: #{e.message}" end end def test_it_do_not_runs_the_task_as_utc begin tick_at(@utc_time_now, :and_every_second_for => 3.hours) rescue => e end assert_equal 0, @events_run.length end def test_it_does_runs_the_task_as_est begin tick_at(@utc_time_now, :and_every_second_for => 5.hours) rescue => e end assert_equal 1, @events_run.length end end end end end clockwork-1.2.0/test/database_events/support/0000755000004100000410000000000012556451264021375 5ustar www-datawww-dataclockwork-1.2.0/test/database_events/support/active_record_fake.rb0000644000004100000410000000212112556451264025515 0ustar www-datawww-datamodule 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 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-1.2.0/test/event_test.rb0000644000004100000410000000176412556451264017246 0ustar www-datawww-datarequire File.expand_path('../../lib/clockwork', __FILE__) require 'active_support/test_case' class EventTest < ActiveSupport::TestCase describe "#thread?" do setup do @manager = mock end describe "manager config thread option set to true" do setup do @manager.stubs(:config).returns({ :thread => true }) end test "is true" do event = Clockwork::Event.new(@manager, nil, nil, nil) assert_equal true, event.thread? end test "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 setup do @manager.stubs(:config).returns({}) end test "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 end clockwork-1.2.0/test/at_test.rb0000644000004100000410000000624612556451264016531 0ustar www-datawww-datarequire File.expand_path('../../lib/clockwork', __FILE__) require 'rubygems' require 'test/unit' require 'mocha/setup' require 'time' require 'active_support/time' require 'active_support/test_case' class AtTest < ActiveSupport::TestCase def time_in_day(hour, minute) Time.new(2013, 1, 1, hour, minute, 0) end test '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 test '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 test '**: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 test '*: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 test '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 test '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 test '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 test '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 test 'invalid time 32:00' do assert_raise Clockwork::At::FailedToParse do Clockwork::At.parse('32:00') end end test 'invalid multi-line with Sat 12:00' do assert_raise Clockwork::At::FailedToParse do Clockwork::At.parse("sat 12:00\nreally invalid time") end end test 'invalid multi-line with 8:30' do assert_raise Clockwork::At::FailedToParse do Clockwork::At.parse("8:30\nreally invalid time") end end test 'invalid multi-line with *:10' do assert_raise Clockwork::At::FailedToParse do Clockwork::At.parse("*:10\nreally invalid time") end end test 'invalid multi-line with 12:**' do assert_raise Clockwork::At::FailedToParse do Clockwork::At.parse("12:**\nreally invalid time") end end end clockwork-1.2.0/test/manager_test.rb0000644000004100000410000002537312556451264017541 0ustar www-datawww-datarequire File.expand_path('../../lib/clockwork', __FILE__) require 'rubygems' require 'test/unit' require 'mocha/setup' require 'time' require 'active_support/time' require 'active_support/test_case' class ManagerTest < ActiveSupport::TestCase setup 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 test "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 test "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 test "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 test "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 test "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 test "aborts when no handler defined" do manager = Clockwork::Manager.new assert_raise(Clockwork::Manager::NoHandlerDefined) do manager.every(1.minute, 'myjob') end end test "aborts when fails to parse" do assert_raise(Clockwork::At::FailedToParse) do @manager.every(1.day, "myjob", :at => "a:bc") end end test "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 test "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 test "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 test "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 test "exceptions are trapped and logged" do @manager.handler { raise 'boom' } @manager.every(1.minute, 'myjob') logger = Logger.new(StringIO.new) @manager.configure { |c| c[:logger] = logger } logger.expects(:error) assert_nothing_raised do @manager.tick(Time.now) end end test "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 test "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 test "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 test "should accept unnamed job" do event = @manager.every(1.minute) assert_equal 'unnamed', event.job end test "should accept options without job name" do event = @manager.every(1.minute, {}) assert_equal 'unnamed', event.job end describe ':at option' do test "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 test "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 test "time zone is not set by default" do assert @manager.config[:tz].nil? end test "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 test "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 test "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 test "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 test ":if true then always run" do @manager.every(1.second, 'myjob', :if => lambda { |_| true }) assert_will_run 'jan 1 2010 16:20:00' end test ":if false then never run" do @manager.every(1.second, 'myjob', :if => lambda { |_| false }) assert_wont_run 'jan 1 2010 16:20:00' end test ":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 test ":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 test ":if is not callable then raise ArgumentError" do assert_raise(ArgumentError) do @manager.every(1.second, 'myjob', :if => true) end end end describe "max_threads" do test "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 test "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 test "should not accept unknown callback name" do assert_raise(RuntimeError, "Unsupported callback unknown_callback") do @manager.on(:unknown_callback) do true end end end test "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 test "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 test "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 test "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 test "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 test "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 setup 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 'test error' } end test 'registered error_handler handles error from event' do @manager.tick assert_equal ['test error'], @errors.map(&:message) end test 'error is notified to logger and handler' do @manager.tick assert @string_io.string.include?('test error') end test 'error in handler will NOT be suppressed' do @manager.error_handler do |e| raise e.message + ' re-raised' end assert_raise(RuntimeError, 'test error re-raised') do @manager.tick end end end end clockwork-1.2.0/test/clockwork_test.rb0000644000004100000410000000431412556451264020115 0ustar www-datawww-datarequire File.expand_path('../../lib/clockwork', __FILE__) require 'test/unit' require 'mocha/setup' class ClockworkTest < Test::Unit::TestCase setup do @log_output = StringIO.new Clockwork.configure do |config| config[:sleep_timeout] = 0 config[:logger] = Logger.new(@log_output) end end teardown do Clockwork.clear! end test 'should run events with configured logger' do run = false Clockwork.handler do |job| run = job == 'myjob' end Clockwork.every(1.minute, 'myjob') Clockwork.manager.expects(:loop).yields.then.returns Clockwork.run assert run assert @log_output.string.include?('Triggering') end test 'should log event correctly' do run = false Clockwork.handler do |job| run = job == 'an event' end Clockwork.every(1.minute, 'an event') Clockwork.manager.expects(:loop).yields.then.returns Clockwork.run assert run assert @log_output.string.include?("Triggering 'an event'") end test '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.expects(:loop).yields.then.returns Clockwork.run assert run end test '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.expects(:loop).yields.then.returns Clockwork.run assert @log_output.string.include?('0 events') end test 'should pass all arguments to every' do Clockwork.every(1.second, 'myjob', if: lambda { |_| false }) { } Clockwork.manager.expects(:loop).yields.then.returns Clockwork.run assert @log_output.string.include?('1 events') assert !@log_output.string.include?('Triggering') end test 'support module re-open style' do $called = false module ::Clockwork every(1.second, 'myjob') { $called = true } end Clockwork.manager.expects(:loop).yields.then.returns Clockwork.run assert $called end end clockwork-1.2.0/.gitignore0000644000004100000410000000005412556451264015541 0ustar www-datawww-datapkg tmp /.bundle Gemfile.lock .ruby-version clockwork-1.2.0/LICENSE0000644000004100000410000000213512556451264014560 0ustar www-datawww-dataThe 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-1.2.0/README.md0000644000004100000410000004163412556451264015041 0ustar www-datawww-dataClockwork - a clock process to replace cron [![Build Status](https://secure.travis-ci.org/tomykaira/clockwork.png?branch=master)](http://travis-ci.org/tomykaira/clockwork) [![Dependency Status](https://gemnasium.com/tomykaira/clockwork.png)](https://gemnasium.com/tomykaira/clockwork) =========================================== Cron is non-ideal for running scheduled application tasks, especially in an app deployed to multiple machines. [More details.](http://adam.heroku.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' 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 to use [clockwork-init.sh](https://gist.github.com/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.heroku.com/past/2010/4/24/beanstalk_a_simple_and_fast_queueing_backend/), [RabbitMQ/Minion](http://adam.heroku.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' 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 enviroment, 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, creating an internal clockwork event for each one, configured based on the instance's `frequency`, `at` and optionally `name` and `tz` methods. It 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`, `at` and possible `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 simply 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 - `at` return nil or `''` if not using `:at`, or otherwise any acceptable clockwork `:at` string - (optionally) `name` returning the name for the event (used to identify it in the Clcockwork output) - (optionally) `tz` 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 ... ``` 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](http://tzinfo.rubyforge.org/) ``` ### :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. 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. This restriction is introduced to solve more severe bug [#135](https://github.com/tomykaira/clockwork/pull/135). ### :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` and `after_tick` callbacks. They are optional, and 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 ``` 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`. Issues and Pull requests ------------------------ If you find a bug, please create an issue - [Issues · tomykaira/clockwork](https://github.com/tomykaira/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](https://github.com/tomykaira/clockwork/issues/58) - [Read events from a database](https://github.com/tomykaira/clockwork/issues/25) 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 http://github.com/tomykaira/clockwork clockwork-1.2.0/clockworkd.10000644000004100000410000000335012556451264015777 0ustar www-datawww-data.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.