Class: Cron::JobTab

Inherits:
Tab
  • Object
show all
Defined in:
lib/app/jobs/cron/job_tab.rb

Overview

Value object for a cron tab entry

Constant Summary collapse

FRAMEWORK_CLASSES =
%w[job trim_collection command server tab job_tab].freeze

Constants inherited from Tab

Tab::COMMA_DELIM, Tab::SLASH_DEMO, Tab::TIME_UNITS, Tab::WILDCARD

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tab

#time_to_run?

Methods included from SearchAble

#after_search_text, #before_search_text, included, #method_missing, #respond_to?, #respond_to_missing?, #search_fields, #sort_fields, #update_search_and_sort_text, #update_text

Methods included from StandardModel

#audit_action, #auto_strip_attributes, #capture_user_info, #clear_cache, #created_by_display_name, #delete_and_log, #destroy_and_log, included, #last_modified_by_display_name, #log_change, #log_deletion, #remove_blank_secure_fields, #save_and_log, #save_and_log!, #secure_fields, #update, #update!, #update_and_log, #update_and_log!

Methods included from App47Logger

clean_params, #clean_params, delete_parameter_keys, #log_controller_error, log_debug, #log_debug, log_error, #log_error, log_exception, #log_message, log_message, #log_warn, log_warn, mask_parameter_keys, #update_flash_messages

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SearchAble

Class Method Details

.ensure_cron_tabsObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/app/jobs/cron/job_tab.rb', line 27

def ensure_cron_tabs
  job_names.each do |job_name|
    klass = "cron/#{job_name}".camelize.constantize
    tab = JobTab.find_or_initialize_by name: job_name
    next if tab.persisted?

    configure_cron_tab(tab, klass.cron_tab)
  end
  purge_cron_tabs
end

.from_string(name, value) ⇒ Object

Pull form system configuration



19
20
21
22
23
24
25
# File 'lib/app/jobs/cron/job_tab.rb', line 19

def from_string(name, value)
  tab = JobTab.find_or_initialize_by name: name
  return unless tab.valid?

  tab.update_from_string(value)
  tab
end

.job_namesObject



38
39
40
41
42
43
44
45
# File 'lib/app/jobs/cron/job_tab.rb', line 38

def job_names
  @job_names ||= all_jobs.collect do |job|
    job_name = job.to_s
    next if FRAMEWORK_CLASSES.include?(job_name) || job_name.end_with?('_test') || job_name.start_with?('base_')

    job_name.underscore
  end.compact
end

Instance Method Details

#cron_job_classObject

Return the class associated with this job cron tab



114
115
116
# File 'lib/app/jobs/cron/job_tab.rb', line 114

def cron_job_class
  @cron_job_class ||= "cron/#{name}".camelize.constantize
end

#runObject

Run this job cron tab



97
98
99
100
101
102
# File 'lib/app/jobs/cron/job_tab.rb', line 97

def run
  return unless valid_environment?

  cron_job_class.perform_later
  super
end

#to_stringObject

Convert back to a standard string value



121
122
123
# File 'lib/app/jobs/cron/job_tab.rb', line 121

def to_string
  [min, hour, mday, month, wday].join(' ')
end

#update_from_string(value) ⇒ Object

Update our values based on the value



85
86
87
88
89
90
91
92
# File 'lib/app/jobs/cron/job_tab.rb', line 85

def update_from_string(value)
  values = value.split(' ')
  self.min = values[0]
  self.hour = values[1]
  self.mday = values[2]
  self.month = values[3]
  self.wday = values[4]
end

#valid_environment?Boolean

Is this enabled and a valid environment

Returns:

  • (Boolean)


107
108
109
# File 'lib/app/jobs/cron/job_tab.rb', line 107

def valid_environment?
  enabled? && cron_job_class.valid_environment?
end

#valid_nameObject

Test the name is the list of jobs discovered



128
129
130
# File 'lib/app/jobs/cron/job_tab.rb', line 128

def valid_name
  errors.add(:name, 'Invalid Tab') unless JobTab.job_names.include?(name)
end