This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
553 additions
and
408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/navvy/job') | ||
require File.expand_path(File.dirname(__FILE__) + '/navvy/worker') | ||
require File.expand_path(File.dirname(__FILE__) + '/navvy/log') | ||
require File.expand_path(File.dirname(__FILE__) + '/navvy/configuration') | ||
require File.expand_path(File.dirname(__FILE__) + '/navvy/monitor') | ||
|
||
module Navvy | ||
class << self | ||
attr_writer :configuration | ||
end | ||
|
||
def self.configuration | ||
@configuration ||= Configuration.new | ||
end | ||
|
||
def self.configure | ||
yield(self.configuration) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Navvy | ||
class Configuration | ||
attr_accessor :job_limit, :keep_jobs, :logger, :quiet, :sleep_time, | ||
:max_attempts | ||
|
||
def initialize | ||
@job_limit = 100 | ||
@keep_jobs = false | ||
@logger = nil | ||
@quiet = false | ||
@sleep_time = 5 | ||
@max_attempts = 25 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
module Navvy | ||
class Job | ||
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 = (@keep || false) | ||
return (Time.now + keep) >= Time.now if keep.is_a? Fixnum | ||
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 update the Navvy::Job record to include the exception | ||
# message it sent back and set the :failed_at date. Failed jobs never 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 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. | ||
# | ||
# @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 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.