Class: Task

Inherits:
Object
  • Object
show all
Extended by:
Observable
Includes:
GlobalID::Identification, Mongoid::Document, Mongoid::Uuid, MongoidEnumerable
Defined in:
app/models/task.rb

Instance Method Summary collapse

Methods included from Observable

add_observer, extended, observer_instances_for, remove_observer

Instance Method Details

#calculate_millisecond_span(start, finish) ⇒ Object



100
101
102
# File 'app/models/task.rb', line 100

def calculate_millisecond_span(start, finish)
  ((finish - start) * 1.day.in_milliseconds).to_i if finish.present? && start.present?
end

#force_retry!Object



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

def force_retry!
  update(try_count: 0)
  retry!
  RunTasksJob.perform_later(execution_type: execution_type)
end

#generate_runner_idObject



118
119
120
121
122
123
124
# File 'app/models/task.rb', line 118

def generate_runner_id
  prefix = name.gsub("_", "-").parameterize
  random_suffix = SecureRandom.alphanumeric(8).downcase
  max_prefix_size = Constants::Runner::MAX_NAME_SIZE - random_suffix.length - 1

  "#{prefix.truncate(max_prefix_size, omission: "")}-#{random_suffix}"
end

#get_logsObject



54
55
56
57
58
59
60
# File 'app/models/task.rb', line 54

def get_logs
  if started?
    slot.node.runner_service(:fetch_logs).perform(task: self)
  else
    logs.try(:data)
  end
end

#mark_as_retry(error: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/task.rb', line 68

def mark_as_retry(error: nil)
  update!(error: error)

  if try_count < Settings.task_retry_count
    update(try_count: try_count + 1, slot: nil, runner_id: nil)
    retry!
    RunTasksJob.perform_later(execution_type: execution_type)
  else
    failed!
  end
end

#mark_as_started!(runner_id:, slot:) ⇒ Object



62
63
64
65
66
# File 'app/models/task.rb', line 62

def mark_as_started!(runner_id:, slot:)
  update!(started_at: Time.zone.now, runner_id: runner_id, slot: slot)

  started!
end

#milliseconds_runningObject



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

def milliseconds_running
  if completed? || failed?
    calculate_millisecond_span(started_at, finished_at)
  elsif started?
    calculate_millisecond_span(started_at, Time.zone.now.to_datetime)
  end
end

#milliseconds_waitingObject



80
81
82
83
84
85
86
# File 'app/models/task.rb', line 80

def milliseconds_waiting
  if started? || completed? || failed?
    calculate_millisecond_span(created_at, started_at)
  else
    calculate_millisecond_span(created_at, Time.zone.now.to_datetime)
  end
end

#normalize_tagsObject



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

def normalize_tags
  tags.transform_values!(&:to_s)
end

#request_idObject



126
127
128
# File 'app/models/task.rb', line 126

def request_id
  tags&.dig("request_id")
end

#seconds_runningObject



96
97
98
# File 'app/models/task.rb', line 96

def seconds_running
  milliseconds_running&.div(1000)
end

#set_logs(logs) ⇒ Object



50
51
52
# File 'app/models/task.rb', line 50

def set_logs(logs)
  self.logs = BSON::Binary.new(logs.dup, :generic)
end

#to_sObject



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

def to_s
  "Task #{name} #{uuid} (#{status} runner_id: #{runner_id}) request_id=#{request_id}"
end