lib/rocket_job/plugins/job/model.rb in rocketjob-5.1.1 vs lib/rocket_job/plugins/job/model.rb in rocketjob-5.2.0.beta1

- old
+ new

@@ -1,6 +1,6 @@ -require 'active_support/concern' +require "active_support/concern" module RocketJob module Plugins module Job # Prevent more than one instance of this job class from running at a time @@ -85,11 +85,11 @@ # Allow a job to updates its estimated progress # Any integer from 0 to 100 field :percent_complete, type: Integer, default: 0 # Store the last exception for this job - embeds_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 @@ -108,11 +108,11 @@ # Example: # job = DataStudyJob.new # job.underscore_name # # => "data_study" def underscore_name - @underscore_name ||= name.sub(/Job$/, '').underscore + @underscore_name ||= name.sub(/Job$/, "").underscore end # Allow the collective name for this job class to be overridden def underscore_name=(underscore_name) @underscore_name = underscore_name @@ -123,11 +123,11 @@ # Example: # job = DataStudyJob.new # job.human_name # # => "Data Study" def human_name - @human_name ||= name.sub(/Job$/, '').titleize + @human_name ||= name.sub(/Job$/, "").titleize end # Allow the human readable job name for this job class to be overridden def human_name=(human_name) @human_name = human_name @@ -138,11 +138,11 @@ # Example: # job = DataStudyJob.new # job.collective_name # # => "data_studies" def collective_name - @collective_name ||= name.sub(/Job$/, '').pluralize.underscore + @collective_name ||= name.sub(/Job$/, "").pluralize.underscore end # Allow the collective name for this job class to be overridden def collective_name=(collective_name) @collective_name = collective_name @@ -153,12 +153,18 @@ queued.where(:run_at.gt => Time.now) end # Scope for queued jobs that can run now # I.e. Queued jobs excluding scheduled jobs - def queued_now - queued.or({run_at: nil}, :run_at.lte => Time.now) + if Mongoid::VERSION.to_f >= 7.1 + def queued_now + queued.and(RocketJob::Job.where(run_at: nil).or(:run_at.lte => Time.now)) + end + else + def queued_now + queued.or({run_at: nil}, :run_at.lte => Time.now) + end end # Defines all the fields that are accessible on the Document # For each field that is defined, a getter and setter will be # added as an instance method to the Document. @@ -191,11 +197,11 @@ super(name, options) end # DEPRECATED def rocket_job - warn 'Replace calls to .rocket_job with calls to set class instance variables. For example: self.priority = 50' + warn "Replace calls to .rocket_job with calls to set class instance variables. For example: self.priority = 50" yield(self) end # DEPRECATED def public_rocket_job_properties(*args) @@ -274,46 +280,46 @@ end # Returns [Hash] status of this job def as_json attrs = serializable_hash(methods: %i[seconds duration]) - attrs.delete('result') unless collect_output? - attrs.delete('failure_count') unless failure_count.positive? + attrs.delete("result") unless collect_output? + attrs.delete("failure_count") unless failure_count.positive? if queued? - attrs.delete('started_at') - attrs.delete('completed_at') - attrs.delete('result') + attrs.delete("started_at") + attrs.delete("completed_at") + attrs.delete("result") attrs elsif running? - attrs.delete('completed_at') - attrs.delete('result') + attrs.delete("completed_at") + attrs.delete("result") attrs elsif completed? - attrs.delete('percent_complete') + attrs.delete("percent_complete") attrs elsif paused? - attrs.delete('completed_at') - attrs.delete('result') + attrs.delete("completed_at") + attrs.delete("result") # Ensure 'paused_at' appears first in the hash - {'paused_at' => completed_at}.merge(attrs) + {"paused_at" => completed_at}.merge(attrs) elsif aborted? - attrs.delete('completed_at') - attrs.delete('result') - {'aborted_at' => completed_at}.merge(attrs) + attrs.delete("completed_at") + attrs.delete("result") + {"aborted_at" => completed_at}.merge(attrs) elsif failed? - attrs.delete('completed_at') - attrs.delete('result') - {'failed_at' => completed_at}.merge(attrs) + attrs.delete("completed_at") + attrs.delete("result") + {"failed_at" => completed_at}.merge(attrs) else attrs end end # Returns [Hash] the status of this job - def status(time_zone = 'Eastern Time (US & Canada)') + def status(time_zone = "Eastern Time (US & Canada)") h = as_json - h.delete('seconds') + h.delete("seconds") h.dup.each_pair do |k, v| if v.is_a?(Time) h[k] = v.in_time_zone(time_zone).to_s elsif v.is_a?(BSON::ObjectId) h[k] = v.to_s @@ -323,9 +329,10 @@ end # Returns [Boolean] whether the worker runs on a particular server. def worker_on_server?(server_name) return false unless worker_name.present? && server_name.present? + worker_name.start_with?(server_name) end end end end