Class: Rearview::Job

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ConstantsModuleMaker, Ext::StateMachine
Defined in:
app/models/rearview/job.rb

Instance Method Summary collapse

Methods included from Ext::StateMachine

#fire_event

Methods included from ConstantsModuleMaker

included

Instance Method Details

#create_associated_event(transition) ⇒ Object



141
142
143
144
145
146
147
# File 'app/models/rearview/job.rb', line 141

def create_associated_event(transition)
  logger.debug "#{self}#create_associated_event for transition #{transition.inspect}"
  job_error_attrs = {}
  job_error_attrs.merge!(event_data[:job_error]) if event_data.try(:[],:job_error)
  job_error = job_errors.create(job_error_attrs)
  job_error.fire_event(translate_associated_event(transition),event_data)
end

#delayObject

The number of seconds to delay before the next time this job should run



95
96
97
98
99
100
101
# File 'app/models/rearview/job.rb', line 95

def delay
  if cron_expr == "0 * * * * ?"
    60.0
  else
    Rearview::CronHelper.next_valid_time_after(cron_expr)
  end
end

#resetObject



84
85
86
87
88
89
90
91
92
# File 'app/models/rearview/job.rb', line 84

def reset
  self.unschedule if active
  self.job_data.destroy unless(self.job_data.nil?)
  self.job_errors.clear
  self.status = nil
  self.save!
  self.reload
  self.schedule if active
end

#scheduleObject



76
77
78
# File 'app/models/rearview/job.rb', line 76

def schedule
  Rearview.monitor_service.schedule(self)
end

#sync_monitor_serviceObject

This doesn’t fit nicely as a callback – the monitor itself needs to update the job which triggers a fun cycle of events =)



105
106
107
108
109
110
111
# File 'app/models/rearview/job.rb', line 105

def sync_monitor_service
  if active
    schedule
  else
    unschedule
  end
end

#unscheduleObject



80
81
82
# File 'app/models/rearview/job.rb', line 80

def unschedule
  Rearview.monitor_service.unschedule(self)
end

#update_associated_event(transition) ⇒ Object



149
150
151
152
153
154
155
# File 'app/models/rearview/job.rb', line 149

def update_associated_event(transition)
  logger.debug "#{self}#update_associated_event for transition #{transition.inspect}"
  job_error = Rearview::JobError.latest_entry(self)
  if job_error.present?
    job_error.fire_event(translate_associated_event(transition),event_data)
  end
end

#valid_alert_keysObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/rearview/job.rb', line 119

def valid_alert_keys
  if alert_keys.present?
    schemes = Rearview::Alerts.registry.keys
    alert_keys.each do |key|
      begin
        uri = URI(key)
        scheme = uri.scheme
        unless scheme.present? && schemes.include?(scheme)
          errors.add(:alert_keys,"unsupported scheme")
        else
          scheme_class = Rearview::Alerts.registry[scheme]
          unless scheme_class.key?(key)
            errors.add(:alert_keys,"#{scheme} URI is invalid")
          end
        end
      rescue URI::InvalidURIError
        errors.add(:alert_keys,"invalid URI")
      end
    end
  end
end

#valid_cron_expressionObject



113
114
115
116
117
# File 'app/models/rearview/job.rb', line 113

def valid_cron_expression
  if cron_expr.present? && !Rearview::CronHelper.valid_expression?(cron_expr)
    errors.add(:cron_expr, "not a valid cron expression")
  end
end