0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Use Robot to run scheduled jobs
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

Robot

DESCRIPTION:

Robot does your bidding. Use it for good, not for evil.

FEATURES/PROBLEMS:

  • Logs the start / end times of each task.
  • Rescues exceptions so that if a task crashes, it won't take down the Robot itself.
  • Synchronizes tasks using a mutex (either a standard Ruby Mutex or something that implements its interface). This ensures only one instance of Robot will run at a time.

SYNOPSIS:

Add the following to an initializer (config/initializers/robot.rb):

# the following options can be set here and they wont need to be set in each definition below
#this sets default behavoir for robots. redefining these will overwrite the default
#set a log file
Robot.logger = Logger.new("log/robot.log")
#specifying a pid root will default all robots to create a pid at <PID_ROOT>/robot.<DEFINED_NAME>.pid
Robot.pid_root = Rails.root("tmp","pids")
#will default hoptoad in each robot
Robot.hoptoad = HoptoadNotifier
#this will default the failsafe ... it already defaults to true ... use this to default to false
Robot.failsafe = true

Robot.define(:hourly) do |robot|
  # Omit this line if you don't want logging, or define it as a custom logger or it is defined globally
  # if you want a separate log file for your Robot tasks.
  robot.logger = Logger.new("log/robot.log")

  # Omit this line if you don't want to synchronize tasks or it is defined globally. Adding a PIDMutex
  # here will ensure that only 1 process is running this task at any time.
  # Note that since PIDMutex uses a file as a locking mechanism, this method
  # doesn't scale beyond a single filesystem.
  robot.mutex = PIDMutex.new("tmp/pids/hourly.pid")

  # Omit this line if you're not using Hoptoad or it is defined globally.
  robot.hoptoad = HoptoadNotifier

  # This is true by default, I'm just including it here as an example. If
  # you set it to false, then there won't be a failsafe mechanism, meaning
  # any exceptions raised by tasks will be unchecked. You'll usually want to
  # leave this set to true.
  robot.failsafe = true

  robot.perform("Some long-running task") { LongTask.run }
end

And then add an entry to crontab to run your new Robot.hourly method:

@hourly script/runner -e production "Robot.run(:hourly)"