lib/rocket_job/dirmon_entry.rb in rocketjob-5.1.1 vs lib/rocket_job/dirmon_entry.rb in rocketjob-5.2.0.beta1
- old
+ new
@@ -1,18 +1,18 @@
-require 'concurrent'
-require 'fileutils'
+require "concurrent"
+require "fileutils"
module RocketJob
class DirmonEntry
include Plugins::Document
include Plugins::StateMachine
# The default archive directory that is used when the job being queued does not respond
# to #upload, and does not have an `archive_directory` specified in this entry
class_attribute :default_archive_directory
- self.default_archive_directory = 'archive'.freeze
+ self.default_archive_directory = "archive".freeze
- store_in collection: 'rocket_job.dirmon_entries'
+ store_in collection: "rocket_job.dirmon_entries"
# User defined name used to identify this DirmonEntry in the Web Interface.
field :name, type: String
# Pattern for finding files
@@ -53,11 +53,11 @@
# If the file was in a sub-directory, the corresponding sub-directory will
# be created in the archive directory.
field :archive_directory, type: String, default: default_archive_directory
# If this DirmonEntry is in the failed state, exception contains the cause
- embeds_one :exception, class_name: 'RocketJob::JobException'
+ embeds_one :exception, class_name: "RocketJob::JobException"
# The maximum number of files that should ever match during a single poll of the pattern.
#
# Too many files could be as a result of an invalid pattern specification.
# Exceeding this number will result in an exception being logged in a failed Dirmon instance.
@@ -71,11 +71,11 @@
# Current state, as set by the state machine. Do not modify directly.
field :state, type: Symbol, default: :pending
# Unique index on pattern to help prevent two entries from scanning the same files
- index({pattern: 1}, background: true, unique: true, drop_dups: true)
+ index({pattern: 1}, background: true, unique: true)
before_validation :strip_whitespace
validates_presence_of :pattern, :job_class_name, :archive_directory
validate :job_is_a_rocket_job
validate :job_has_properties
@@ -173,12 +173,12 @@
# Example no dirmon entries:
# RocketJob::Job.counts_by_state
# # => {}
def self.counts_by_state
counts = {}
- collection.aggregate([{'$group' => {_id: '$state', count: {'$sum' => 1}}}]).each do |result|
- counts[result['_id'].to_sym] = result['count']
+ collection.aggregate([{"$group" => {_id: "$state", count: {"$sum" => 1}}}]).each do |result|
+ counts[result["_id"].to_sym] = result["count"]
end
counts
end
# Passes each filename [Pathname] found that matches the pattern into the supplied block
@@ -211,21 +211,22 @@
if exc_or_message.is_a?(Exception)
self.exception = JobException.from_exception(exc_or_message)
exception.worker_name = worker_name
else
build_exception(
- class_name: 'RocketJob::DirmonEntryException',
+ class_name: "RocketJob::DirmonEntryException",
message: exc_or_message,
backtrace: [],
worker_name: worker_name
)
end
end
# Returns the Job to be created.
def job_class
return if job_class_name.nil?
+
job_class_name.constantize
rescue NameError
nil
end
@@ -243,11 +244,11 @@
original_file_name: iopath.to_s,
job_id: job_id
)
logger.info(
- message: 'Created RocketJob::Jobs::UploadFileJob',
+ message: "Created RocketJob::Jobs::UploadFileJob",
payload: {
dirmon_entry_name: name,
upload_file_name: archive_path.to_s,
original_file_name: iopath.to_s,
job_class_name: job_class_name,
@@ -280,19 +281,21 @@
# Validates job_class is a Rocket Job
def job_is_a_rocket_job
klass = job_class
return if job_class_name.nil? || klass&.ancestors&.include?(RocketJob::Job)
+
errors.add(:job_class_name, "Job #{job_class_name} must be defined and inherit from RocketJob::Job")
end
# Does the job have all the supplied properties
def job_has_properties
klass = job_class
return unless klass
properties.each_pair do |k, _v|
next if klass.public_method_defined?("#{k}=".to_sym)
+
errors.add(:properties, "Unknown Property: Attempted to set a value for #{k.inspect} which is not allowed on the job #{job_class_name}")
end
end
end
end