Class: Lato::Operation

Inherits:
ApplicationRecord show all
Defined in:
app/models/lato/operation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate(active_job_name, active_job_input = {}, user_id = nil) ⇒ Object

Class



88
89
90
91
92
93
94
# File 'app/models/lato/operation.rb', line 88

def self.generate(active_job_name, active_job_input = {}, user_id = nil)
  Operation.create(
    active_job_name: active_job_name,
    active_job_input: active_job_input,
    lato_user_id: user_id
  )
end

Instance Method Details

#completed(message = nil) ⇒ Object



77
78
79
80
81
82
83
# File 'app/models/lato/operation.rb', line 77

def completed(message = nil)
  update(
    status: :completed,
    closed_at: Time.now,
    active_job_output: message ? active_job_output.merge(_message: message) : active_job_output
  )
end

#failed(error = nil) ⇒ Object



69
70
71
72
73
74
75
# File 'app/models/lato/operation.rb', line 69

def failed(error = nil)
  update(
    status: :failed,
    closed_at: Time.now,
    active_job_output: error ? active_job_output.merge(_error: error) : active_job_output
  )
end

#finished?Boolean

Questions

Returns:

  • (Boolean)


25
26
27
# File 'app/models/lato/operation.rb', line 25

def finished?
  completed_status? || failed_status?
end

#output_errorObject

Helpers



44
45
46
# File 'app/models/lato/operation.rb', line 44

def output_error
  active_job_output['_error']
end

#output_error?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/lato/operation.rb', line 33

def output_error?
  active_job_output && !active_job_output['_error'].blank?
end

#output_file?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/lato/operation.rb', line 37

def output_file?
  output_file.attached?
end

#output_messageObject



48
49
50
# File 'app/models/lato/operation.rb', line 48

def output_message
  active_job_output['_message']
end

#output_message?Boolean

Returns:

  • (Boolean)


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

def output_message?
  active_job_output && !active_job_output['_message'].blank?
end

#runningObject



65
66
67
# File 'app/models/lato/operation.rb', line 65

def running
  update(status: :running)
end

#startObject

Operations



54
55
56
57
58
59
60
61
62
63
# File 'app/models/lato/operation.rb', line 54

def start
  begin
    active_job_name.constantize.perform_later(active_job_input.merge(_operation_id: id))
  rescue StandardError
    errors.add(:base, 'Impossibile eseguire il job')
    return false
  end

  true
end