Module: ActsAsService
- Defined in:
- lib/acts_as_service.rb
Overview
module that makes it easy to turn a class into a service, giving it methods like stop, start, restart for easy cronjobbage, etc.
simply require ‘acts_as_service’ in your class’ file and then call acts_as_service in your class definition, then implement a few required and option methods:
- required:
- self.perform_work_chunk: do a chunk of work that the service is intended
to do. note that this method should return
periodically to enable shutdown. alternatively,
your method /could/ (but not recommended) check
the status to see if it's set to SHUTTING_DOWN
and return if so. otherwise, the shutdown
by this module will not work.
-
optional:
- self.service_name: return the string name of the service. default is the class name (without module prefixes) - self.service_pid_filename: path to the file that should contain the PID for the process while it's running. default is #{RAILS_ROOT}/tmp/pids/<underscore version of service name> - self.sleep_time: seconds to sleep between calls to peform_work_chunk default: no sleep till brooklyn! - self.sleep_check_timeout: while 'sleeping' between work chunks, how many seconds to sleep between checking if the service has been shut down. default is 2 seconds, but you may want to override to make shorter (will give better resolution on sleep_time, but uses a little more cpu) or longer (if you don't care about how quickly the service shuts down) - self.after_start: a hook to run a method after the service is started but before first call to perform_work_chunk - self.before_stop: a hook to run a method before final shutdown (and after last run to perform_work_chunk)
you can also call self.shutdown() from within your perform_work_chunk code to initiate a shutdown (don’t just exit() because there’s pidfile cleanup etc. to peform).
if you’d like to have long-running work chunks that periodically check for shutdowns, you can call _shutting_down? at any point, which will return true if a shutdown has been initiated. However, recommend putting reasonable amounts of work in each chunk and letting this service infrastructure manage the decision-making about shutting down…it’ll in general keep your code cleaner.
# my_service.rb:
require ‘acts_as_service’
class MyService
acts_as_service
# ... define methods
end
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- ACTS_AS_SERVICE_RUNNING =
'running'
- ACTS_AS_SERVICE_OTHER_RUNNING =
'other running'
- ACTS_AS_SERVICE_STOPPED =
'stopped'
- ACTS_AS_SERVICE_SHUTTING_DOWN =
'shutting down'
- ACTS_AS_SERVICE_PID_NO_PROCESS =
'pid no process'
- SLEEP_CHECK_TIMEOUT =
how long to sleep before checking to see if sleep time has elapsed…allows for sleeping for a long time between work chunks, but quicker response to shutdowns. this is measured in seconds this is the DEFAULT value if the service doesn’t define the
sleep_check_timeout
method itself 2
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
74 75 76 |
# File 'lib/acts_as_service.rb', line 74 def self.included(base) base.extend ClassMethods end |