Class: ForemanTasks::Triggering
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- 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]
- ALLOWED_MODES =
[:immediate, :future, :recurring]
- ALLOWED_INPUT_TYPES =
[:cronline, :monthly, :weekly, :daily, :hourly]
- TIME_FORMAT =
"%Y-%m-%d %H:%M"
- TIME_REGEXP =
/\A\d{4}-\d{2}-\d{2} \d{2}:\d{2}\Z/
- DAYS_REGEXP =
/\A(\s*\d{1,2}\s*)(,\s*\d{1,2}\s*)*\Z/
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_before! ⇒ Object
- #recurring? ⇒ Boolean
- #trigger(action, *args) ⇒ Object
Class Method Details
.new_from_params(params = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'app/models/foreman_tasks/triggering.rb', line 40 def self.new_from_params(params = {}) self.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
75 76 77 78 79 80 |
# File 'app/models/foreman_tasks/triggering.rb', line 75 def { :start_at => start_at.utc, :start_before => start_before.try(:utc) } end |
#future? ⇒ Boolean
82 83 84 |
# File 'app/models/foreman_tasks/triggering.rb', line 82 def future? mode == :future end |
#immediate? ⇒ Boolean
86 87 88 |
# File 'app/models/foreman_tasks/triggering.rb', line 86 def immediate? mode == :immediate end |
#mode ⇒ Object
50 51 52 |
# File 'app/models/foreman_tasks/triggering.rb', line 50 def mode super.to_sym end |
#mode=(mod) ⇒ Object
54 55 56 57 58 59 60 |
# File 'app/models/foreman_tasks/triggering.rb', line 54 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
94 95 96 |
# File 'app/models/foreman_tasks/triggering.rb', line 94 def parse_start_at! self.start_at ||= Time.zone.parse(start_at_raw) end |
#parse_start_before! ⇒ Object
98 99 100 |
# File 'app/models/foreman_tasks/triggering.rb', line 98 def parse_start_before! self.start_before ||= Time.zone.parse(start_before_raw) unless start_before_raw.blank? end |
#recurring? ⇒ Boolean
90 91 92 |
# File 'app/models/foreman_tasks/triggering.rb', line 90 def recurring? mode == :recurring end |
#trigger(action, *args) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/foreman_tasks/triggering.rb', line 62 def trigger(action, *args) case mode when :immediate ::ForemanTasks.async_task action, *args when :future ::ForemanTasks.delay action, , *args when :recurring recurring_logic.start(action, *args) end end |