lib/rocket_job/plugins/job/model.rb in rocketjob-2.1.3 vs lib/rocket_job/plugins/job/model.rb in rocketjob-3.0.0.alpha
- old
+ new
@@ -11,14 +11,13 @@
included do
#
# User definable attributes
#
# The following attributes are set when the job is created
- # @formatter:off
# Description for this job instance
- key :description, String
+ field :description, type: String, class_attribute: true, user_editable: true
# Priority of this job as it relates to other jobs [1..100]
# 1: Highest Priority
# 50: Default Priority
# 100: Lowest Priority
@@ -27,173 +26,146 @@
# A job with a priority of 40 will execute before a job with priority 50
#
# In RocketJob Pro, if a SlicedJob is running and a higher priority job
# arrives, then the current job will complete the current slices and process
# the new higher priority job
- key :priority, Integer, default: 50
+ field :priority, type: Integer, default: 50, class_attribute: true, user_editable: true
- # Run this job no earlier than this time
- key :run_at, Time
-
- # If a job has not started by this time, destroy it
- key :expires_at, Time
-
# When the job completes destroy it from both the database and the UI
- key :destroy_on_complete, Boolean, default: true
+ field :destroy_on_complete, type: Boolean, default: true, class_attribute: true
- # Any user supplied arguments for the method invocation
- # All keys must be UTF-8 strings. The values can be any valid BSON type:
- # Integer
- # Float
- # Time (UTC)
- # String (UTF-8)
- # Array
- # Hash
- # True
- # False
- # Symbol
- # nil
- # Regular Expression
- #
- # Note: Date is not supported, convert it to a UTC time
- key :arguments, Array
-
# Whether to store the results from this job
- key :collect_output, Boolean, default: false
+ field :collect_output, type: Boolean, default: false, class_attribute: true
+ # Run this job no earlier than this time
+ field :run_at, type: Time
+
+ # If a job has not started by this time, destroy it
+ field :expires_at, type: Time
+
# Raise or lower the log level when calling the job
# Can be used to reduce log noise, especially during high volume calls
# For debugging a single job can be logged at a low level such as :trace
# Levels supported: :trace, :debug, :info, :warn, :error, :fatal
- key :log_level, Symbol
+ field :log_level, type: Symbol, user_editable: true
#
# Read-only attributes
#
# Current state, as set by the state machine. Do not modify this value directly.
- key :state, Symbol, default: :queued
+ field :state, type: Symbol, default: :queued
# When the job was created
- key :created_at, Time, default: -> { Time.now }
+ field :created_at, type: Time, default: -> { Time.now }
# When processing started on this job
- key :started_at, Time
+ field :started_at, type: Time
# When the job completed processing
- key :completed_at, Time
+ field :completed_at, type: Time
# Number of times that this job has failed to process
- key :failure_count, Integer, default: 0
+ field :failure_count, type: Integer, default: 0
# This name of the worker that this job is being processed by, or was processed by
- key :worker_name, String
+ field :worker_name, type: String
#
# Values that jobs can update during processing
#
# Allow a job to updates its estimated progress
# Any integer from 0 to 100
- key :percent_complete, Integer, default: 0
+ field :percent_complete, type: Integer, default: 0
# Store the last exception for this job
- one :exception, class_name: 'RocketJob::JobException'
+ embeds_one :exception, class_name: 'RocketJob::JobException'
# Store the Hash result from this job if collect_output is true,
# and the job returned actually returned a Hash, otherwise nil
# Not applicable to SlicedJob jobs, since its output is stored in a
# separate collection
- key :result, Hash
+ field :result, type: Hash
- # @formatter:on
+ index({state: 1, priority: 1, _id: 1}, background: true)
- # Store all job types in this collection
- set_collection_name 'rocket_job.jobs'
-
validates_presence_of :state, :failure_count, :created_at
validates :priority, inclusion: 1..100
validates :log_level, inclusion: SemanticLogger::LEVELS + [nil]
+ end
+ module ClassMethods
# Returns [String] the singular name for this job class
#
# Example:
# job = DataStudyJob.new
# job.underscore_name
# # => "data_study"
- def self.underscore_name
+ def underscore_name
@underscore_name ||= name.sub(/Job$/, '').underscore
end
# Allow the collective name for this job class to be overridden
- def self.underscore_name=(underscore_name)
+ def underscore_name=(underscore_name)
@underscore_name = underscore_name
end
# Returns [String] the human readable name for this job class
#
# Example:
# job = DataStudyJob.new
# job.human_name
# # => "Data Study"
- def self.human_name
+ def human_name
@human_name ||= name.sub(/Job$/, '').titleize
end
# Allow the human readable job name for this job class to be overridden
- def self.human_name=(human_name)
+ def human_name=(human_name)
@human_name = human_name
end
# Returns [String] the collective name for this job class
#
# Example:
# job = DataStudyJob.new
# job.collective_name
# # => "data_studies"
- def self.collective_name
+ def collective_name
@collective_name ||= name.sub(/Job$/, '').pluralize.underscore
end
# Allow the collective name for this job class to be overridden
- def self.collective_name=(collective_name)
+ def collective_name=(collective_name)
@collective_name = collective_name
end
# Scope for jobs scheduled to run in the future
- def self.scheduled
- queued.where(run_at: {'$gt' => Time.now})
+ def scheduled
+ queued.where(:run_at.gt => Time.now)
end
# Scope for queued jobs that can run now
# I.e. Queued jobs excluding scheduled jobs
- def self.queued_now
- queued.where(
- '$or' => [
- {run_at: {'$exists' => false}},
- {run_at: {'$lte' => Time.now}}
- ]
+ def queued_now
+ queued.or(
+ {:run_at.exists => false},
+ {:run_at.lte => Time.now}
)
end
- # Returns the number of required arguments for this job
- def self.rocket_job_argument_count
- instance_method(:perform).arity
+ # DEPRECATED
+ def rocket_job
+ warn 'Replace calls to .rocket_job with calls to set class instance variables. For example: self.priority = 50'
+ yield(self)
end
- # User definable properties in Dirmon Entry
- def self.rocket_job_properties
- @rocket_job_properties ||= (self == RocketJob::Job ? [] : superclass.rocket_job_properties)
+ # DEPRECATED
+ def public_rocket_job_properties(*args)
+ warn "Replace calls to .public_rocket_job_properties by adding `user_editable: true` option to the field declaration in #{name} for: #{args.inspect}"
+ self.user_editable_fields += args.collect(&:to_sym)
end
-
- # Add to user definable properties in Dirmon Entry
- def self.public_rocket_job_properties(*properties)
- properties.each { |property| raise("Invalid public_rocket_job_property: #{property.inspect}") unless key?(property)}
- rocket_job_properties.concat(properties).uniq!
- end
-
- # User definable properties in Dirmon Entry
- public_rocket_job_properties :description, :priority, :log_level, :arguments
end
# Returns [true|false] whether to collect nil results from running this batch
def collect_nil_output?
collect_output? ? (collect_nil_output == true) : false