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)


27
28
29
30
31
32
# File 'app/models/good_job/cron_entry.rb', line 27

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



21
22
23
24
25
# File 'app/models/good_job/cron_entry.rb', line 21

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

Instance Method Details

#argsObject



49
50
51
# File 'app/models/good_job/cron_entry.rb', line 49

def args
  params[:args]
end

#descriptionObject



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

def description
  params[:description]
end

#disableObject



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

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

#display_propertiesObject



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

def display_properties
  {
    key: key,
    class: job_class,
    cron: display_schedule,
    set: display_property(set),
    description: display_property(description),
  }.tap do |properties|
    properties[:args] = display_property(args) if args.present?
    properties[:kwargs] = display_property(kwargs) if kwargs.present?
  end
end

#display_scheduleObject



113
114
115
# File 'app/models/good_job/cron_entry.rb', line 113

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

#enableObject



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

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

#enabled?Boolean

Returns:

  • (Boolean)


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

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

#enqueue(cron_at = nil) ⇒ Object



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

def enqueue(cron_at = nil)
  GoodJob::CurrentThread.within do |current_thread|
    current_thread.cron_key = key
    current_thread.cron_at = cron_at

    configured_job = job_class.constantize.set(set_value)
    I18n.with_locale(I18n.default_locale) do
      kwargs_value.present? ? configured_job.perform_later(*args_value, **kwargs_value) : configured_job.perform_later(*args_value)
    end
  end
rescue ActiveRecord::RecordNotUnique
  false
end

#job_classObject



41
42
43
# File 'app/models/good_job/cron_entry.rb', line 41

def job_class
  params.fetch(:class)
end

#jobsObject



117
118
119
# File 'app/models/good_job/cron_entry.rb', line 117

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

#keyObject Also known as: id, to_param



34
35
36
# File 'app/models/good_job/cron_entry.rb', line 34

def key
  params.fetch(:key)
end

#kwargsObject



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

def kwargs
  params[:kwargs]
end

#last_jobObject



121
122
123
# File 'app/models/good_job/cron_entry.rb', line 121

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

#last_job_atObject



125
126
127
128
129
# File 'app/models/good_job/cron_entry.rb', line 125

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



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/good_job/cron_entry.rb', line 61

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



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

def set
  params[:set]
end