Class: Asyncapi::Client::UpdateJob

Inherits:
Object
  • Object
show all
Defined in:
app/services/asyncapi/client/update_job.rb

Class Method Summary collapse

Class Method Details

.event_for(status) ⇒ Object



23
24
25
26
27
28
# File 'app/services/asyncapi/client/update_job.rb', line 23

def self.event_for(status)
  case status
  when "success"; :succeed
  when "error"; :fail
  end
end

.execute(job:, params:) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/asyncapi/client/update_job.rb', line 4

def self.execute(job:, params:)
  sanitized_params = params.reject { |key, value| key.to_sym == :status }
  job.assign_attributes(sanitized_params)
  status = params[:status]

  if may_transition?(job, to: status)
    transition(job, to: status)
    if job.status_changed? && job.save
      JobStatusWorker.perform_async(job.id)
    else
      job.save
    end
  elsif !may_transition?(job, to: status) && job.status == status
    job.save
  end
end

.may_transition?(job, to:) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/services/asyncapi/client/update_job.rb', line 30

def self.may_transition?(job, to:)
  job.send(:"may_#{event_for(to)}?")
end

.transition(job, to:) ⇒ Object



34
35
36
# File 'app/services/asyncapi/client/update_job.rb', line 34

def self.transition(job, to:)
  job.send(event_for(to)) if may_transition?(job, to: to)
end