Class: Action

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/action.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.ignored_exceptionsObject

Returns the value of attribute ignored_exceptions.



29
30
31
# File 'app/models/action.rb', line 29

def ignored_exceptions
  @ignored_exceptions
end

Class Method Details

.run!(action_name, params, trigger) ⇒ Object



43
44
45
46
# File 'app/models/action.rb', line 43

def run!(action_name, params, trigger)
  action = create!(name: action_name, trigger: trigger, params: params)
  action.run!
end

.runningObject



35
36
37
# File 'app/models/action.rb', line 35

def running
  where.not(started_at: nil).where(finished_at: nil)
end

.started_before(time) ⇒ Object



31
32
33
# File 'app/models/action.rb', line 31

def started_before(time)
  where arel_table[:started_at].lteq time
end

.unqueuedObject



39
40
41
# File 'app/models/action.rb', line 39

def unqueued
  where(started_at: nil)
end

Instance Method Details

#durationObject



105
106
107
108
# File 'app/models/action.rb', line 105

def duration
  return nil unless finished?
  finished_at - started_at
end

#exception=(exception) ⇒ Object



97
98
99
# File 'app/models/action.rb', line 97

def exception=(exception)
  self.error = Error.find_or_create_for_exception(exception) if exception
end

#finish!(exception) ⇒ Object



101
102
103
# File 'app/models/action.rb', line 101

def finish!(exception)
  update_attributes! finished_at: Time.now, succeeded: exception.nil?, exception: exception
end

#finished?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'app/models/action.rb', line 114

def finished?
  finished_at.present?
end

#in_progress?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'app/models/action.rb', line 118

def in_progress?
  started_at.present? && finished_at.nil?
end

#retry!Object



90
91
92
93
94
95
# File 'app/models/action.rb', line 90

def retry!
  update_attributes! started_at: Time.now, finished_at: nil, succeeded: nil, exception: nil
  Houston.async do
    run!
  end
end

#run!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/action.rb', line 51

def run!
  exception = nil

  Houston.reconnect do
    touch :started_at
    Houston.actions.fetch(name).execute(params)
  end

rescue *::Action.ignored_exceptions

  # Note that the action failed, but do not report _these_ exceptions
  exception = $!

rescue Exception # rescues StandardError by default; but we want to rescue and report all errors

  # Report all other exceptions
  exception = $!
  Houston.report_exception($!, parameters: {
    action_id: id,
    action_name: name,
    trigger: trigger,
    params: params
  })

ensure
  begin
    Houston.reconnect do
      finish! exception
    end
  rescue Exception # rescues StandardError by default; but we want to rescue and report all errors
    Houston.report_exception($!, parameters: {
      action_id: id,
      action_name: name,
      trigger: trigger,
      params: params
    })
  end
end

#started?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'app/models/action.rb', line 110

def started?
  started_at.present?
end