Class: Kuroko2::JobInstance

Inherits:
ApplicationRecord show all
Includes:
TableNameCustomizable
Defined in:
app/models/kuroko2/job_instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#log_messageObject

Returns the value of attribute log_message.



6
7
8
# File 'app/models/kuroko2/job_instance.rb', line 6

def log_message
  @log_message
end

Instance Method Details

#cancel(by:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/kuroko2/job_instance.rb', line 49

def cancel(by:)
  self.tokens.destroy(*self.tokens)
  self.executions.destroy(*self.executions)
  self.touch(:canceled_at)

  message = "This job was canceled by #{by}."
  self.logs.warn(message)
  Kuroko2.logger.warn(message)

  Kuroko2::Workflow::Notifier.notify(:cancellation, self) if job_definition.hipchat_notify_finished?
end

#cancelable?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/kuroko2/job_instance.rb', line 45

def cancelable?
  tokens.first.try(:cancelable?)
end

#error?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/kuroko2/job_instance.rb', line 37

def error?
  working? && error_at?
end

#execution_minutesObject



74
75
76
# File 'app/models/kuroko2/job_instance.rb', line 74

def execution_minutes
  (((error_at || canceled_at || finished_at || Time.current) - created_at).to_f / 60).round(2)
end

#log_memory_consumption(value) ⇒ Object

Log given value if it is greater than stored one. This logging is not so important that we can ignore race condition, so we use ‘#update` and `#create_association` without bang here.

Parameters:

  • value (Intger)


65
66
67
68
69
70
71
72
# File 'app/models/kuroko2/job_instance.rb', line 65

def log_memory_consumption(value)
  if memory_consumption_log
    max = [value, memory_consumption_log.value].max
    memory_consumption_log.update(value: max)
  else
    create_memory_consumption_log(value: value)
  end
end

#statusObject



78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/kuroko2/job_instance.rb', line 78

def status
  if finished_at?
    'success'
  elsif canceled_at?
    'canceled'
  elsif error_at?
    'error'
  else
    'working'
  end
end

#working?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/kuroko2/job_instance.rb', line 41

def working?
  !finished_at? && !canceled_at?
end