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, file = nil) ⇒ Object

Class



94
95
96
97
98
99
100
101
102
103
# File 'app/models/lato/operation.rb', line 94

def self.generate(active_job_name, active_job_input = {}, user_id = nil, file = nil)
  operation_params = {
    active_job_name: active_job_name,
    active_job_input: active_job_input,
    lato_user_id: user_id
  }
  operation_params[:input_file] = file unless file.nil?

  Lato::Operation.create(operation_params)
end

Instance Method Details

#completed(message = nil) ⇒ Object



83
84
85
86
87
88
89
# File 'app/models/lato/operation.rb', line 83

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



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

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)


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

def finished?
  completed_status? || failed_status?
end

#output_errorObject

Helpers



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

def output_error
  active_job_output['_error']
end

#output_error?Boolean

Returns:

  • (Boolean)


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

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

#output_file?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/lato/operation.rb', line 42

def output_file?
  output_file.attached?
end

#output_messageObject



53
54
55
# File 'app/models/lato/operation.rb', line 53

def output_message
  active_job_output['_message']
end

#output_message?Boolean

Returns:

  • (Boolean)


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

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

#runningObject



71
72
73
# File 'app/models/lato/operation.rb', line 71

def running
  update(status: :running)
end

#startObject

Operations



60
61
62
63
64
65
66
67
68
69
# File 'app/models/lato/operation.rb', line 60

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