djwrapper

DESCRIPTION:

Wrapper gem for Delayed Job.

FEATURES/PROBLEMS:

A job class that gives you some nifty methods:

- process! (immediately process this job)
- enqueue (wrap a Delayed::Job object around this job so it will be picked up by a worker process)
- log (log messages)
- time (pass a block and it will log the elapsed time)

This gem also does some basic logging for your background tasks. The logs will show up in your log/<environment>.log file with pretty colored output!

SYNOPSIS:

class Myjob < Djwrapper::DelayedBackgroundProcess
   def self.process!(params = {})
       super

time do

	# do stuff here ...

log(“Something happened here!”) end

   end

   # show debug log lines?
   # e.g. log("debug", :debug => true)
   #
   # will only be shown if verbose? if true
   def self.verbose?
       # default is false
       true
   end
end

# Process the job immediately. Useful for testing purposes.
Myjob.process!
# Create a background job.
Myjob.enqueue

SIGNAL HANDLING:

If you have a job that runs for a very long time, it may be killed at some point. You then want it to exit gracefully so you don’t have any errors in your database. Add this piece of code to the start (or end) of your big loop so the loop will at least finish its iteration before the process gets killed.

The signals that are caught by default are TERM and INT. TERM is sent by the ‘daemons’ gem (script/delayed_job stop), and INT corresponds to Ctrl-C in your terminal.

if $_djwrapper_interrupted
  log("Got interrupted. Doing graceful exit ...")

  return true
end

REQUIREMENTS:

- delayed_job
- colors

INSTALL:

- gem install djwrapper

LICENSE:

(The MIT License)

Copyright © 2013 Commander Johnson <[email protected]>

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.