Class: GoodJob::CronEntry

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/good_job/cron_entry.rb

Overview

A CronEntry represents a single scheduled item’s properties.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ CronEntry

Returns a new instance of CronEntry.

Raises:

  • (ArgumentError)


51
52
53
54
55
56
# File 'app/models/good_job/cron_entry.rb', line 51

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

#paramsObject (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



16
17
18
19
# File 'app/models/good_job/cron_entry.rb', line 16

def self.all(configuration: nil)
  configuration ||= GoodJob.configuration
  configuration.cron_entries
end

.find(key, configuration: nil) ⇒ Object



45
46
47
48
49
# File 'app/models/good_job/cron_entry.rb', line 45

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/good_job/cron_entry.rb', line 21

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 = <<~SQL.squish
    CROSS JOIN LATERAL (
      SELECT * FROM good_jobs
      WHERE good_jobs.cron_key = cron_keys.cron_key
      ORDER BY cron_at DESC NULLS LAST
      LIMIT 1
    ) AS lateral_jobs
  SQL

  GoodJob::Job
    .select("lateral_jobs.*")
    .from(from_clause)
    .joins(join_clause)
    .index_by(&:cron_key)
end

Instance Method Details

#argsObject



73
74
75
# File 'app/models/good_job/cron_entry.rb', line 73

def args
  params[:args]
end

#descriptionObject



81
82
83
# File 'app/models/good_job/cron_entry.rb', line 81

def description
  params[:description]
end

#disableObject



119
120
121
# File 'app/models/good_job/cron_entry.rb', line 119

def disable
  GoodJob::Setting.cron_key_disable(key)
end

#display_propertiesObject



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/good_job/cron_entry.rb', line 139

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_scheduleObject



153
154
155
# File 'app/models/good_job/cron_entry.rb', line 153

def display_schedule
  cron_proc? ? display_property(cron) : fugit.original
end

#enableObject



115
116
117
# File 'app/models/good_job/cron_entry.rb', line 115

def enable
  GoodJob::Setting.cron_key_enable(key)
end

#enabled?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/good_job/cron_entry.rb', line 111

def enabled?
  GoodJob::Setting.cron_key_enabled?(key, default: enabled_by_default?)
end

#enabled_by_default?Boolean

Returns:

  • (Boolean)


171
172
173
174
# File 'app/models/good_job/cron_entry.rb', line 171

def enabled_by_default?
  value = params.fetch(:enabled_by_default, true)
  value.respond_to?(:call) ? value.call : value
end

#enqueue(cron_at = nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/good_job/cron_entry.rb', line 123

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 = job_class_value
      job_klass = job_klass.constantize if job_klass.is_a?(String)
      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_classObject



65
66
67
# File 'app/models/good_job/cron_entry.rb', line 65

def job_class
  params.fetch(:class)
end

#jobsObject



157
158
159
# File 'app/models/good_job/cron_entry.rb', line 157

def jobs
  GoodJob::Job.where(cron_key: key)
end

#keyObject Also known as: id, to_param



58
59
60
# File 'app/models/good_job/cron_entry.rb', line 58

def key
  params.fetch(:key)
end

#kwargsObject



77
78
79
# File 'app/models/good_job/cron_entry.rb', line 77

def kwargs
  params[:kwargs]
end

#last_jobObject



161
162
163
# File 'app/models/good_job/cron_entry.rb', line 161

def last_job
  jobs.order("cron_at DESC NULLS LAST").first
end

#last_job_atObject



165
166
167
168
169
# File 'app/models/good_job/cron_entry.rb', line 165

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



85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/good_job/cron_entry.rb', line 85

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

#setObject



69
70
71
# File 'app/models/good_job/cron_entry.rb', line 69

def set
  params[:set]
end

#within(period, previously_at: nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/good_job/cron_entry.rb', line 98

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