Skip to content

Commit

Permalink
Updated cron generator for rufus-scheduler, start of central EventMac…
Browse files Browse the repository at this point in the history
…hine management code
  • Loading branch information
kennethkalmer committed Jun 22, 2009
1 parent 777a501 commit 5c2b959
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 13 deletions.
1 change: 1 addition & 0 deletions Manifest.txt
Expand Up @@ -79,6 +79,7 @@ lib/daemon_kit/core_ext/string.rb
lib/daemon_kit/cron.rb
lib/daemon_kit/cucumber/world.rb
lib/daemon_kit/deployment/capistrano.rb
lib/daemon_kit/em.rb
lib/daemon_kit/error_handlers/base.rb
lib/daemon_kit/error_handlers/hoptoad.rb
lib/daemon_kit/error_handlers/mail.rb
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Expand Up @@ -10,7 +10,8 @@ $hoe = Hoe.new('daemon-kit', DaemonKit::VERSION) do |p|
p.post_install_message = IO.read( 'PostInstall.txt' ) # TODO remove if post-install message not required
p.rubyforge_name = 'kit' # TODO this is default value
p.extra_deps = [
['rubigen', '>= 1.5.2']
['rubigen', '>= 1.5.2'],
['eventmachine', '>=0.12.8']
]
p.extra_dev_deps = [
['newgem', ">= #{::Newgem::VERSION}"]
Expand Down
6 changes: 5 additions & 1 deletion daemon_generators/cron/templates/config/initializers/cron.rb
@@ -1,7 +1,11 @@
begin
require 'rufus-scheduler'
require 'rufus/scheduler'
rescue LoadError => e
$stderr.puts "Missing rufus-scheduler gem. Please run 'gem install rufus-scheduler'."
exit 1
end

if Rufus::Scheduler::VERSION < "2.0.0"
$stderr.puts "Requires rufus-scheduler-2.0.0 or later"
exit 1
end
8 changes: 6 additions & 2 deletions daemon_generators/cron/templates/libexec/daemon.rb
Expand Up @@ -15,13 +15,17 @@
# An instance of the scheduler is available through
# DaemonKit::Cron.scheduler

# To make use of the EventMachine-powered scheduler, uncomment the
# line below *before* adding any schedules.
# DaemonKit::EM.run

# Some samples to get you going:

# Will call #regenerate_monthly_report in 3 days from starting up
#DaemonKit::Cron.scheduler.in("3d") do
# regenerate_monthly_report()
#end
#
#
#DaemonKit::Cron.scheduler.every "10m10s" do
# check_score(favourite_team) # every 10 minutes and 10 seconds
#end
Expand All @@ -35,5 +39,5 @@
DaemonKit.logger.debug "Scheduled task completed at #{Time.now}"
end

# Run our 'cron' daeon
# Run our 'cron' dameon, suspending the current thread
DaemonKit::Cron.run
5 changes: 4 additions & 1 deletion lib/daemon_kit.rb
@@ -1,13 +1,15 @@
# TODO: Strip this out eventually so we can run without rubygems
require 'rubygems'

require 'eventmachine'

require File.dirname(__FILE__) + '/daemon_kit/core_ext'

$:.unshift( File.dirname(__FILE__).to_absolute_path ) unless
$:.include?( File.dirname(__FILE__).to_absolute_path )

module DaemonKit
VERSION = '0.1.7.6'
VERSION = '0.1.7.7'

autoload :Initializer, 'daemon_kit/initializer'
autoload :Application, 'daemon_kit/application'
Expand All @@ -16,6 +18,7 @@ module DaemonKit
autoload :Safety, 'daemon_kit/safety'
autoload :PidFile, 'daemon_kit/pid_file'
autoload :AbstractLogger, 'daemon_kit/abstract_logger'
autoload :EM, 'daemon_kit/em'
autoload :Configurable, 'daemon_kit/core_ext/configurable'

autoload :Cron, 'daemon_kit/cron'
Expand Down
26 changes: 18 additions & 8 deletions lib/daemon_kit/cron.rb
@@ -1,32 +1,42 @@
module DaemonKit

# Thin wrapper around rufus-scheduler gem, specifically designed to ease
# configuration of a scheduler and provide some added simplicity.
#
# For more information on rufus-scheduler, please visit the RDoc's
# at http://rufus.rubyforge.org/rufus-scheduler/
#
# To use the evented scheduler, call #DaemonKit::EM.run prior to
# setting up your first schedule.
class Cron

@@instance = nil
@instance = nil

attr_reader :scheduler

class << self


# Access to the scheduler instance
def instance
@instance ||= new
end

def scheduler
instance.scheduler
end

private :new

# Once the scheduler has been configured, call #run to block the
# current thread and keep the process alive for the scheduled
# tasks to run
def run
DaemonKit.logger.info "Starting rufus-scheduler"

begin
if instance.is_a?( Rufus::Scheduler::PlainScheduler )
instance.scheduler.join
rescue Interrupt
DaemonKit.logger.warn "Scheduler interrupted"
else
Thread.stop
end
end
end
Expand Down
43 changes: 43 additions & 0 deletions lib/daemon_kit/em.rb
@@ -0,0 +1,43 @@
module DaemonKit

# EventMachine forms a critical part of the daemon-kit toolset, and
# especially of daemon process developers.
#
# This class abstracts away the difficulties of managing multiple
# libraries that all utilize the event reactor.
class EM

class << self

# Start a reactor, just like classical EM.run. If the block is
# provided, the method will block and call the provided block
# argument inside the running reactor. If the block argument is
# not provided the reactor will be started in a separate thread
# and the program will continue to run after the method. All the
# signal traps are configured to shutdown the reactor when the
# daemon exists.
def run(&block)
if ::EM.reactor_running?
DaemonKit.logger.warn "EventMachine reactor already running"
block.call if block_given?

else
if block_given?
::EM.run { block.call }
else
Thread.main[:_dk_reactor] = Thread.new { EM.run {} }
DaemonKit.trap( 'INT' ) { DaemonKit::EM.stop }
DaemonKit.trap( 'TERM' ) { DaemonKit::EM.stop }
end
end
end

# Stop the reactor
def stop
::EM.stop_event_loop if ::EM.reactor_running?
Thread.main[:_dk_reactor].join
end
end

end
end

0 comments on commit 5c2b959

Please sign in to comment.