Class: GoodJob::CronEntry
- Inherits:
-
Object
- Object
- GoodJob::CronEntry
- Includes:
- ActiveModel::Model
- Defined in:
- app/models/good_job/cron_entry.rb
Overview
A CronEntry represents a single scheduled item's properties.
Constant Summary collapse
- VALUE_TYPES =
{ args: Array, kwargs: Hash, set: Hash, }.freeze
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
- .all(configuration: nil) ⇒ Object
- .find(key, configuration: nil) ⇒ Object
- .last_jobs_by_key(cron_entries) ⇒ Object
Instance Method Summary collapse
- #args ⇒ Object
- #description ⇒ Object
- #disable ⇒ Object
- #display_properties ⇒ Object
- #display_schedule ⇒ Object
- #enable ⇒ Object
- #enabled? ⇒ Boolean
- #enabled_by_default? ⇒ Boolean
- #enqueue(cron_at = nil) ⇒ Object
-
#initialize(params = {}) ⇒ CronEntry
constructor
A new instance of CronEntry.
- #job_class ⇒ Object
- #jobs ⇒ Object
- #key ⇒ Object (also: #id, #to_param)
- #kwargs ⇒ Object
- #last_job ⇒ Object
- #last_job_at ⇒ Object
- #next_at(previously_at: nil) ⇒ Object
- #set ⇒ Object
- #within(period, previously_at: nil) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ CronEntry
Returns a new instance of CronEntry.
60 61 62 63 64 65 |
# File 'app/models/good_job/cron_entry.rb', line 60 def initialize(params = {}) @params = params return if cron_proc? raise ArgumentError, "Invalid cron format: '#{cron}'" unless fugit.instance_of?(Fugit::Cron) end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
14 15 16 |
# File 'app/models/good_job/cron_entry.rb', line 14 def params @params end |
Class Method Details
.all(configuration: nil) ⇒ Object
25 26 27 28 |
# File 'app/models/good_job/cron_entry.rb', line 25 def self.all(configuration: nil) configuration ||= GoodJob.configuration configuration.cron_entries end |
.find(key, configuration: nil) ⇒ Object
54 55 56 57 58 |
# File 'app/models/good_job/cron_entry.rb', line 54 def self.find(key, configuration: nil) all(configuration: configuration).find { |entry| entry.key == key.to_sym }.tap do |cron_entry| raise ActiveRecord::RecordNotFound unless cron_entry end end |
.last_jobs_by_key(cron_entries) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/good_job/cron_entry.rb', line 30 def self.last_jobs_by_key(cron_entries) cron_keys = cron_entries.map { |entry| entry.key.to_s } return {} if cron_keys.empty? from_clause = GoodJob::Job.sanitize_sql_array( ["unnest(ARRAY[?]::text[]) AS cron_keys(cron_key)", cron_keys] ) join_clause = " CROSS JOIN LATERAL (\n SELECT * FROM \#{GoodJob::Job.quoted_table_name}\n WHERE \#{GoodJob::Job.quoted_table_name}.cron_key = cron_keys.cron_key\n ORDER BY cron_at DESC NULLS LAST\n LIMIT 1\n ) AS lateral_jobs\n SQL\n\n GoodJob::Job\n .select(\"lateral_jobs.*\")\n .from(from_clause)\n .joins(join_clause)\n .index_by(&:cron_key)\nend\n".squish |
Instance Method Details
#args ⇒ Object
82 83 84 |
# File 'app/models/good_job/cron_entry.rb', line 82 def args params[:args] end |
#description ⇒ Object
90 91 92 |
# File 'app/models/good_job/cron_entry.rb', line 90 def description params[:description] end |
#disable ⇒ Object
128 129 130 |
# File 'app/models/good_job/cron_entry.rb', line 128 def disable GoodJob::Setting.cron_key_disable(key) end |
#display_properties ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'app/models/good_job/cron_entry.rb', line 147 def display_properties { key: key, class: job_class, cron: display_schedule, set: display_property(set), description: display_property(description), }.tap do |properties| properties[:class] = display_property(job_class) if job_class.present? properties[:args] = display_property(args) if args.present? properties[:kwargs] = display_property(kwargs) if kwargs.present? end end |
#display_schedule ⇒ Object
161 162 163 |
# File 'app/models/good_job/cron_entry.rb', line 161 def display_schedule cron_proc? ? display_property(cron) : fugit.original end |
#enable ⇒ Object
124 125 126 |
# File 'app/models/good_job/cron_entry.rb', line 124 def enable GoodJob::Setting.cron_key_enable(key) end |
#enabled? ⇒ Boolean
120 121 122 |
# File 'app/models/good_job/cron_entry.rb', line 120 def enabled? GoodJob::Setting.cron_key_enabled?(key, default: enabled_by_default?) end |
#enabled_by_default? ⇒ Boolean
179 180 181 182 |
# File 'app/models/good_job/cron_entry.rb', line 179 def enabled_by_default? value = params.fetch(:enabled_by_default, true) value.respond_to?(:call) ? value.call : value end |
#enqueue(cron_at = nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'app/models/good_job/cron_entry.rb', line 132 def enqueue(cron_at = nil) GoodJob::CurrentThread.within do |current_thread| current_thread.cron_key = key current_thread.cron_at = cron_at I18n.with_locale(I18n.default_locale) do job_klass = resolve_job_class next unless job_klass.is_a?(Class) configured_job = job_klass.set(set_value) kwargs_value.present? ? configured_job.perform_later(*args_value, **kwargs_value) : configured_job.perform_later(*args_value) end end end |
#job_class ⇒ Object
74 75 76 |
# File 'app/models/good_job/cron_entry.rb', line 74 def job_class params.fetch(:class) end |
#jobs ⇒ Object
165 166 167 |
# File 'app/models/good_job/cron_entry.rb', line 165 def jobs GoodJob::Job.where(cron_key: key) end |
#key ⇒ Object Also known as: id, to_param
67 68 69 |
# File 'app/models/good_job/cron_entry.rb', line 67 def key params.fetch(:key) end |
#kwargs ⇒ Object
86 87 88 |
# File 'app/models/good_job/cron_entry.rb', line 86 def kwargs params[:kwargs] end |
#last_job ⇒ Object
169 170 171 |
# File 'app/models/good_job/cron_entry.rb', line 169 def last_job jobs.order("cron_at DESC NULLS LAST").first end |
#last_job_at ⇒ Object
173 174 175 176 177 |
# File 'app/models/good_job/cron_entry.rb', line 173 def last_job_at return if last_job.blank? (last_job.cron_at || last_job.created_at).localtime end |
#next_at(previously_at: nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/good_job/cron_entry.rb', line 94 def next_at(previously_at: nil) if cron_proc? result = Rails.application.executor.wrap { cron.call(previously_at || last_job_at) } if result.is_a?(String) Fugit.parse(result).next_time.to_t else result end else fugit.next_time.to_t end end |
#set ⇒ Object
78 79 80 |
# File 'app/models/good_job/cron_entry.rb', line 78 def set params[:set] end |
#within(period, previously_at: nil) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/models/good_job/cron_entry.rb', line 107 def within(period, previously_at: nil) if cron_proc? result = Rails.application.executor.wrap { cron.call(previously_at || last_job_at) } if result.is_a?(String) Fugit.parse(result).within(period).map(&:to_t) else result end else fugit.within(period).map(&:to_t) end end |