Permalink
Browse files

releasing 0.2.1

  • Loading branch information...
jeffkreeftmeijer committed Feb 10, 2010
2 parents 004aeea + b8805db commit 0427e8b97756fcb3fd6702a3d561f8521da04b73
View
@@ -1 +1 @@
0.2.0
0.2.1
@@ -4,11 +4,11 @@ def manifest
options = {
:migration_file_name => 'create_jobs'
}
m.migration_template 'migration.rb', 'db/migrate', options
m.file 'script', 'script/navvy', :chmod => 0755
end
end
def banner
"Usage: #{$0} #{spec.name}"
end
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
Navvy::Worker.daemonize
@@ -1,18 +1,24 @@
require 'rails/generators/migration'
class NavvyGenerator < Rails::Generators::Base
include Rails::Generators::Migration
def self.source_root
File.join(File.dirname(__FILE__), '..', '..', 'generators', 'navvy', 'templates')
end
def install_navvy
migration_template(
'migration.rb',
'db/migrate/create_navvy_table.rb'
)
copy_file(
'script',
'script/navvy',
:chmod => 0755
)
end
protected
def next_migration_number(dirname) #:nodoc:
"%.3d" % (current_migration_number(dirname) + 1)
View
@@ -0,0 +1,156 @@
module Navvy
class Job
class << self
attr_writer :limit, :keep, :max_attempts
end
##
# Limit of jobs to be fetched at once. Will use the value stored in
# Navvy.configuration (defaults to 100), or -- for backwards compatibility
# -- Navvy::Job.limit.
#
# @return [Integer] limit
def self.limit
@limit || Navvy.configuration.job_limit
end
##
# If and how long the jobs should be kept. Will use the value stored in
# Navvy.configuration (defaults to false), or -- for backwards
# compatibility -- Navvy::Job.keep.
#
# @return [Fixnum, true, false] keep
def self.keep
@keep || Navvy.configuration.keep_jobs
end
##
# How often should a job be retried? Will use the value stored in
# Navvy.configuration (defaults to 24), or -- for backwards compatibility
# -- Navvy::Job.max_attempts.
#
# @return [Fixnum] max_attempts
def self.max_attempts
@max_attempts || Navvy.configuration.max_attempts
end
##
# Should the job be kept? Will calculate if the keeptime has passed if
# @keep is a Fixnum. Otherwise, it'll just return the @keep value since
# it's probably a boolean.
#
# @return [true, false] keep
def self.keep?
return (Time.now + self.keep) >= Time.now if self.keep.is_a? Fixnum
self.keep
end
##
# Run the job. Will delete the Navvy::Job record and return its return
# value if it runs successfully unless Navvy::Job.keep is set. If a job
# fails, it'll call Navvy::Job#failed and pass the exception message.
# Failed jobs will _not_ get deleted.
#
# @example
# job = Navvy::Job.next # finds the next available job in the queue
# job.run # runs the job and returns the job's return value
#
# @return [String] return value or exception message of the called method.
def run
begin
started
result = Kernel.const_get(object).send(method_name, *args)
Navvy::Job.keep? ? completed : destroy
result
rescue Exception => exception
failed(exception.message)
end
end
##
# Retry the current job. Will add self to the queue again, giving the clone
# a parend_id equal to self.id. Also, the priority of the new job will be
# the same as its parent's and it'll set the :run_at date to N ** 4, where
# N is the times_failed count.
#
# @return [Navvy::Job] job the new job it created.
def retry
self.class.enqueue(
object,
method_name,
*(args << {
:job_options => {
:parent_id => parent_id || id,
:run_at => Time.now + times_failed ** 4,
:priority => priority
}
})
)
end
##
# Check if the job has been run.
#
# @return [true, false] ran
def ran?
completed? || failed?
end
##
# Check how long it took for a job to complete or fail.
#
# @return [Time, Integer] time the time it took.
def duration
ran? ? (completed_at || failed_at) - started_at : 0
end
##
# Check if completed_at is set.
#
# @return [true, false] set?
def completed_at?
!completed_at.nil?
end
##
# Check if failed_at is set.
#
# @return [true, false] set?
def failed_at?
!failed_at.nil?
end
##
# Get the job arguments as an array.
#
# @return [array] arguments
def args
arguments.is_a?(Array) ? arguments : YAML.load(arguments)
end
##
# Get the job status
#
# @return [:pending, :completed, :failed] status
def status
return :completed if completed?
return :failed if failed?
:pending
end
alias_method :completed?, :completed_at?
alias_method :failed?, :failed_at?
end
end
@@ -3,47 +3,6 @@
module Navvy
class Job < ActiveRecord::Base
class << self
attr_writer :limit, :keep, :max_attempts
end
##
# Default limit of jobs to be fetched.
#
# @return [Integer] limit
def self.limit
@limit || Navvy.configuration.job_limit
end
##
# If and how long the jobs should be kept.
#
# @return [Fixnum, true, false] keep
def self.keep
@keep || Navvy.configuration.keep_jobs
end
##
# How often should a job be retried?
#
# @return [Fixnum] max_attempts
def self.max_attempts
@max_attempts || Navvy.configuration.max_attempts
end
##
# Should the job be kept?
#
# @return [true, false] keep
def self.keep?
keep = (self.keep || false)
return keep.from_now >= Time.now if keep.is_a? Fixnum
keep
end
##
# Add a job to the job queue.
@@ -115,31 +74,15 @@ def self.cleanup
end
##
# Run the job. Will delete the Navvy::Job record and return its return
# value if it runs successfully unless Navvy::Job.keep is set. If a job
# fails, it'll update the Navvy::Job record to include the exception
# message it sent back and set the :failed_at date. Failed jobs never get
# deleted.
# Mark the job as started. Will set started_at to the current time.
#
# @example
# job = Navvy::Job.next # finds the next available job in the queue
# job.run # runs the job and returns the job's return value
#
# @return [String] return value of the called method.
# @return [true, false] update_attributes the result of the
# update_attributes call
def run
begin
update_attributes(:started_at => Time.now)
if args.empty?
result = object.constantize.send(method_name)
else
result = object.constantize.send(method_name, *args)
end
Navvy::Job.keep? ? completed : destroy
result
rescue Exception => exception
failed(exception.message)
end
def started
update_attributes({
:started_at => Time.now
})
end
##
@@ -176,26 +119,6 @@ def failed(message = nil)
)
end
##
# Retry the current job. Will add self to the queue again, giving the clone
# a parend_id equal to self.id.
#
# @return [true, false]
def retry
self.class.enqueue(
object,
method_name,
*(args << {
:job_options => {
:parent_id => parent_id || id,
:run_at => Time.now + times_failed ** 4,
:priority => priority
}
})
)
end
##
# Check how many times the job has failed. Will try to find jobs with a
# parent_id that's the same as self.id and count them
@@ -208,64 +131,7 @@ def times_failed
:conditions => "(`id` = '#{i}' OR `parent_id` = '#{i}') AND `failed_at` IS NOT NULL"
)
end
##
# Check if the job has been run.
#
# @return [true, false] ran
def ran?
completed? || failed?
end
##
# Check how long it took for a job to complete or fail
#
# @return [Time, Integer] time the time it took
def duration
ran? ? (completed_at || failed_at) - started_at : 0
end
##
# Check if completed_at is set
#
# @return [true, false] set?
def completed_at?
!completed_at.nil?
end
##
# Check if failed_at is set
#
# @return [true, false] set?
def failed_at?
!failed_at.nil?
end
##
# Get the job arguments as an array
#
# @return [array] arguments
def args
arguments.is_a?(Array) ? arguments : YAML.load(arguments)
end
##
# Get the job status
#
# @return [:pending, :completed, :failed] status
def status
return :completed if completed?
return :failed if failed?
:pending
end
alias_method :completed?, :completed_at?
alias_method :failed?, :failed_at?
end
end
require File.expand_path(File.dirname(__FILE__) + '/../job')
Oops, something went wrong.

0 comments on commit 0427e8b

Please sign in to comment.