Class: ForemanTasks::Triggering
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- ForemanTasks::Triggering
- Defined in:
- app/models/foreman_tasks/triggering.rb
Constant Summary collapse
- PARAMS =
[:start_at_raw, :start_before_raw, :max_iteration, :input_type, :cronline, :days, :days_of_week, :time, :end_time_limited, :end_time, :purpose].freeze
- ALLOWED_MODES =
[:immediate, :future, :recurring].freeze
- ALLOWED_INPUT_TYPES =
[:cronline, :monthly, :weekly, :daily, :hourly].freeze
- TIME_FORMAT =
'%Y-%m-%d %H:%M'.freeze
- TIME_REGEXP =
/\A\d{4}-\d{2}-\d{2} \d{2}:\d{2}\Z/.freeze
- DAYS_REGEXP =
/\A(\s*\d{1,2}\s*)(,\s*\d{1,2}\s*)*\Z/.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #delay_options ⇒ Object
- #future? ⇒ Boolean
- #immediate? ⇒ Boolean
- #mode ⇒ Object
- #mode=(mod) ⇒ Object
- #parse_start_at ⇒ Object
- #parse_start_at! ⇒ Object
- #parse_start_before! ⇒ Object
- #recurring? ⇒ Boolean
- #trigger(action, *args) ⇒ Object
Class Method Details
.new_from_params(params = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'app/models/foreman_tasks/triggering.rb', line 42 def self.new_from_params(params = {}) new(params.except(:mode, :start_at, :start_before)).tap do |triggering| triggering.mode = params.fetch(:mode, :immediate).to_sym triggering.input_type = params.fetch(:input_type, :daily).to_sym triggering.end_time_limited = params[:end_time_limited] == 'true' triggering.start_at_raw ||= Time.zone.now.strftime(TIME_FORMAT) triggering.recurring_logic = ::ForemanTasks::RecurringLogic.new_from_triggering(triggering) if triggering.recurring? end end |
Instance Method Details
#delay_options ⇒ Object
77 78 79 80 81 82 |
# File 'app/models/foreman_tasks/triggering.rb', line 77 def { :start_at => start_at.utc, :start_before => start_before.try(:utc), } end |
#future? ⇒ Boolean
84 85 86 |
# File 'app/models/foreman_tasks/triggering.rb', line 84 def future? mode == :future end |
#immediate? ⇒ Boolean
88 89 90 |
# File 'app/models/foreman_tasks/triggering.rb', line 88 def immediate? mode == :immediate end |
#mode ⇒ Object
52 53 54 |
# File 'app/models/foreman_tasks/triggering.rb', line 52 def mode super.to_sym end |
#mode=(mod) ⇒ Object
56 57 58 59 60 61 62 |
# File 'app/models/foreman_tasks/triggering.rb', line 56 def mode=(mod) if (mod.is_a?(String) || mod.is_a?(Symbol)) && ALLOWED_MODES.map(&:to_s).include?(mod.downcase.to_s) super(mod.downcase.to_sym) else raise ArgumentError, _('mode has to be one of %{allowed_modes}') % { :allowed_modes => ALLOWED_MODES.join(', ') } end end |
#parse_start_at ⇒ Object
100 101 102 103 104 |
# File 'app/models/foreman_tasks/triggering.rb', line 100 def parse_start_at self.start_at ||= Time.zone.parse(start_at_raw) if start_at_raw.present? rescue ArgumentError errors.add(:start_at, _('is not a valid format')) end |
#parse_start_at! ⇒ Object
96 97 98 |
# File 'app/models/foreman_tasks/triggering.rb', line 96 def parse_start_at! self.start_at ||= Time.zone.parse(start_at_raw) if start_at_raw.present? end |
#parse_start_before! ⇒ Object
106 107 108 |
# File 'app/models/foreman_tasks/triggering.rb', line 106 def parse_start_before! self.start_before ||= Time.zone.parse(start_before_raw) if start_before_raw.present? end |
#recurring? ⇒ Boolean
92 93 94 |
# File 'app/models/foreman_tasks/triggering.rb', line 92 def recurring? mode == :recurring end |
#trigger(action, *args) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/foreman_tasks/triggering.rb', line 64 def trigger(action, *args) case mode when :immediate ::ForemanTasks.async_task action, *args when :future ::ForemanTasks.delay action, , *args when :recurring recurring_logic.start_after(action, [:start_at] || Time.zone.now, *args) end end |