Class: SFRest::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrest/task.rb

Overview

Deal with tasks, find them, pause them…

Constant Summary collapse

STATUS_NOT_STARTED =

Task has been added to queue but not picked up

1
STATUS_RESTARTED =

Task has been re-added to the queue but not picked up

2
STATUS_TO_BE_RUN =

Restarted + not started

3
STATUS_IN_PROCESS =

A wip process is actively processing a task

4
STATUS_WAITING =

Task has been released from active processing

8
STATUS_RUNNING =

Running = in process + waiting to be processed

12
STATUS_COMPLETED =

Task is successfully processed (exited with success)

16
STATUS_ERROR =

Task unsccuessfully completed (exited with failure)

32
STATUS_KILLED =

Task is terminated

64
STATUS_WARNING =

Completed bit + 128 bit(warning).

144
STATUS_DONE =

Completed + error + killed + 128 bit(warning)

240

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ Task

Returns a new instance of Task.

Parameters:



19
20
21
# File 'lib/sfrest/task.rb', line 19

def initialize(conn)
  @conn = conn
end

Instance Method Details

#delete_task(task_id) ⇒ Object

Deletes a specific task identified by its task id. This will delete the task and its children

Parameters:

  • task_id (Integer)


273
274
275
276
# File 'lib/sfrest/task.rb', line 273

def delete_task(task_id)
  current_path = "/api/v1/tasks/#{task_id}"
  @conn.delete(current_path)
end

#find_task(task_id) ⇒ Hash

Retrieve a specific task by ID.

Parameters:

  • task_id (Integer)

    The ID of the task to retrieve.

Returns:

  • (Hash)

    The task information.



180
181
182
183
# File 'lib/sfrest/task.rb', line 180

def find_task(task_id)
  current_path = "/api/v1/tasks/#{task_id}"
  @conn.get(current_path)
end

#find_task_ids(limit = nil, page = nil, group = nil, klass = nil, status = nil) ⇒ Array[Integer]

Find a set of task ids. rubocop:disable Metrics/ParameterLists

Parameters:

  • limit (Integer) (defaults to: nil)

    max amount of results to return per request

  • page (Integer) (defaults to: nil)

    page of request

  • group (String) (defaults to: nil)

    task group

  • klass (String) (defaults to: nil)

    task class

  • status (Integer) (defaults to: nil)

    Integerish the status of the task see SFRest::Task::STATUS_*

Returns:

  • (Array[Integer])


151
152
153
154
155
156
157
158
159
160
161
# File 'lib/sfrest/task.rb', line 151

def find_task_ids(limit = nil, page = nil, group = nil, klass = nil, status = nil)
  # rubocop:enable Metrics/ParameterLists
  res = find_tasks limit: limit, page: page, group: group, klass: klass, status: status
  task_ids = []
  i = 0
  res.each do |task|
    task_ids[i] = task['id']
    i += 1
  end
  task_ids
end

#find_tasks(datum = nil) ⇒ Object

Find a set of tasks.

Parameters:

  • datum (Hash) (defaults to: nil)

    Hash of filters

Options Hash (datum):

  • :limit (Integer)

    max amount of results to return per request

  • :page (Integer)

    page of request

  • :group (String)

    task group

  • :class (String)

    task class

  • :status (Integer)

    Integerish the status of the task see SFRest::Task::STATUS_*



171
172
173
174
175
# File 'lib/sfrest/task.rb', line 171

def find_tasks(datum = nil)
  current_path = '/api/v1/tasks'
  pb = SFRest::Pathbuilder.new
  @conn.get URI.parse(pb.build_url_query(current_path, datum)).to_s
end

#get_task_class_info(type = '') ⇒ Array

returns the classes that are either softpaused or softpause-for-update

Parameters:

  • type (String) (defaults to: '')

    softpaused | softpause-for-update

Returns:

  • (Array)

    Array of wip classes



281
282
283
284
# File 'lib/sfrest/task.rb', line 281

def get_task_class_info(type = '')
  current_path = "/api/v1/classes/#{type}"
  @conn.get(current_path)
end

#get_task_id(name, group = nil, klass = nil, status = nil) ⇒ Object

Looks for a task with a specific name

Parameters:

  • name (String)

    display name of the task

  • group (String) (defaults to: nil)

    task group filter

  • klass (String) (defaults to: nil)

    task class filter

  • status (String) (defaults to: nil)

    task status filter



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/sfrest/task.rb', line 190

def get_task_id(name, group = nil, klass = nil, status = nil)
  page_size = 100
  page = 0
  loop do
    tasks = find_tasks(limit: page_size, page: page, group: group, class: klass, status: status)
    tasks.each do |task|
      return task['id'].to_i if task['name'] =~ /#{name}/

      page += 1
    end
    break if tasks.size < page_size
  end
  nil
end

#get_task_logs(task_id) ⇒ Object

Get a specific task’s logs

Parameters:

  • task_id (Integer)


221
222
223
224
# File 'lib/sfrest/task.rb', line 221

def get_task_logs(task_id)
  current_path = "/api/v1/tasks/#{task_id}/logs"
  @conn.get(current_path)
end

#get_wip_task_status(task_id) ⇒ Hash{"wip_task" => {"id" => Integer, "status" => Integer}, "time" => timestamp}

Get the status of a wip task by id.

Parameters:

  • task_id (Integer)

Returns:

  • (Hash{"wip_task" => {"id" => Integer, "status" => Integer}, "time" => timestamp})


289
290
291
292
# File 'lib/sfrest/task.rb', line 289

def get_wip_task_status(task_id)
  current_path = "/api/v1/wip/task/#{task_id}/status"
  @conn.get(current_path)
end

#globally_paused?Boolean

Gets the value of a vairable @TODO: this is missnamed becasue it does not check the global paused variable

Returns:

  • (Boolean)


229
230
231
232
233
234
235
236
237
238
239
# File 'lib/sfrest/task.rb', line 229

def globally_paused?
  paused = false
  current_path = '/api/v1/variables?name=wip_pause_global'
  begin
    res = @conn.get(current_path)
    paused = res['wip_pause_global']
  rescue SFRest::SFError => e
    paused = false if e.message =~ /Variable not found/
  end
  paused
end

#pause_all_tasksObject

Pauses all tasks.



206
207
208
209
210
# File 'lib/sfrest/task.rb', line 206

def pause_all_tasks
  current_path = '/api/v1/pause'
  payload = { 'paused' => true }.to_json
  @conn.post(current_path, payload)
end

#pause_task(task_id, level = 'family') ⇒ Object

Pauses a specific task identified by its task id. This can pause either the task and it’s children or just the task

Parameters:

  • task_id (Integer)
  • level (String) (defaults to: 'family')

    family|task



245
246
247
248
249
# File 'lib/sfrest/task.rb', line 245

def pause_task(task_id, level = 'family')
  current_path = "/api/v1/pause/#{task_id}"
  payload = { 'paused' => true, 'level' => level }.to_json
  @conn.post(current_path, payload)
end

#resume_all_tasksObject

Resumes all tasks.



213
214
215
216
217
# File 'lib/sfrest/task.rb', line 213

def resume_all_tasks
  current_path = '/api/v1/pause'
  payload = { 'paused' => false }.to_json
  @conn.post(current_path, payload)
end

#resume_task(task_id, level = 'family') ⇒ Object

Resumes a specific task identified by its task id. This can resume either the task and it’s children or just the task

Parameters:

  • task_id (Integer)
  • level (String) (defaults to: 'family')

    family|task



255
256
257
258
259
# File 'lib/sfrest/task.rb', line 255

def resume_task(task_id, level = 'family')
  current_path = "/api/v1/pause/#{task_id}"
  payload = { 'paused' => false, 'level' => level }.to_json
  @conn.post(current_path, payload)
end

#status_completed?(status) ⇒ Boolean

Returns true only if the status evaluates to completed

Parameters:

  • status (Integer)

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/sfrest/task.rb', line 36

def status_completed?(status)
  return true if status.to_i == STATUS_COMPLETED

  false
end

#status_done?(status) ⇒ Boolean

Returns true if the status evaluates to a state that is considered done

Parameters:

  • status (Integer)

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/sfrest/task.rb', line 74

def status_done?(status)
  return true if (status.to_i & STATUS_DONE).positive?

  false
end

#status_error?(status) ⇒ Boolean

Returns true only if the status evaluates to errored

Parameters:

  • status (Integer)

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/sfrest/task.rb', line 55

def status_error?(status)
  return true if status.to_i == STATUS_ERROR

  false
end

#status_killed?(status) ⇒ Boolean

Returns true only if the status evaluates to killed

Parameters:

  • status (Integer)

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/sfrest/task.rb', line 64

def status_killed?(status)
  return true if status.to_i == STATUS_KILLED

  false
end

#status_not_started?(status) ⇒ Boolean

Returns true only if the status evaluates to not started or restarted

Parameters:

  • status (Integer)

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/sfrest/task.rb', line 27

def status_not_started?(status)
  return true if (status.to_i & STATUS_TO_BE_RUN).positive?

  false
end

#status_running?(status) ⇒ Boolean

Returns true if the status evaluates to either waiting or in process

Parameters:

  • status (Integer)

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/sfrest/task.rb', line 46

def status_running?(status)
  return true if (status.to_i & STATUS_RUNNING).positive?

  false
end

#task_completed?(task_id) ⇒ Boolean

Returns true only if WIP reports the status as completed

Parameters:

  • task_id (Integer)

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/sfrest/task.rb', line 112

def task_completed?(task_id)
  status = task_status task_id
  status_completed?(status)
end

#task_done?(task_id) ⇒ Boolean

Returns true if WIP reports the status in a state that is considered done

Parameters:

  • task_id (Integer)

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/sfrest/task.rb', line 121

def task_done?(task_id)
  status = task_status task_id
  status_done?(status)
end

#task_errored?(task_id) ⇒ Boolean

Returns true only if WIP reports the status as errored

Parameters:

  • task_id (Integer)

Returns:

  • (Boolean)


137
138
139
140
# File 'lib/sfrest/task.rb', line 137

def task_errored?(task_id)
  status = task_status task_id
  status_error?(status)
end

#task_killed?(task_id) ⇒ Boolean

Returns true only if WIP reports the status as killed

Parameters:

  • task_id (Integer)

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/sfrest/task.rb', line 129

def task_killed?(task_id)
  status = task_status task_id
  status_killed?(status)
end

#task_not_started?(task_id) ⇒ Boolean

Returns true only if WIP reports the status as not started or restarted

Parameters:

  • task_id (Integer)

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/sfrest/task.rb', line 96

def task_not_started?(task_id)
  status = task_status task_id
  status_not_started?(status)
end

#task_running?(task_id) ⇒ Boolean

Returns true only if WIP reports the status as running

Parameters:

  • task_id (Integer)

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/sfrest/task.rb', line 104

def task_running?(task_id)
  status = task_status task_id
  status_running?(status)
end

#task_status(task_id) ⇒ Hash{"wip_task" => {"id" => Integer, "status" => Integer}, "time" => timestamp}

Get the status of a wip task by id.

Parameters:

  • task_id (Integer)

Returns:

  • (Hash{"wip_task" => {"id" => Integer, "status" => Integer}, "time" => timestamp})

Raises:



83
84
85
86
87
88
89
90
# File 'lib/sfrest/task.rb', line 83

def task_status(task_id)
  current_path = "/api/v1/wip/task/#{task_id}/status"
  res = @conn.get(current_path)
  raise InvalidDataError, "No wip task returned for task id #{task_id}" if res['wip_task'].nil?
  raise InvalidDataError, "No task status returned for task id #{task_id}" if res['wip_task']['status'].nil?

  res['wip_task']['status']
end

#terminate_task(task_id) ⇒ Object

Terminates a specific task identified by its task id. This will terminate the task and its children

Parameters:

  • task_id (Integer)


264
265
266
267
268
# File 'lib/sfrest/task.rb', line 264

def terminate_task(task_id)
  current_path = "/api/v1/tasks/#{task_id}/terminate"
  payload = nil
  @conn.put(current_path, payload)
end

#wait_until_done(task_id, max_nap = 600) ⇒ Object

Blocks until a task is done

Parameters:

  • task_id (Integer)
  • max_nap (Integer) (defaults to: 600)

    seconds to try before giving up



297
298
299
# File 'lib/sfrest/task.rb', line 297

def wait_until_done(task_id, max_nap = 600)
  wait_until_state(task_id, 'done', max_nap)
end

#wait_until_state(task_id, state, max_nap) ⇒ Object

Blocks until a task reaches a specific status

Parameters:

  • task_id (Integer)
  • state (String)

    state to reach

  • max_nap (Integer)

    seconds to try before giving up



305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/sfrest/task.rb', line 305

def wait_until_state(task_id, state, max_nap)
  blink_time = 5 # wake up and scan
  nap_start = Time.now
  state_method = method(:"task_#{state}?")
  loop do
    break if state_method.call(task_id)
    raise TaskNotDoneError, "Task: #{task_id} has taken too long to complete!" if Time.new > (nap_start + max_nap)

    sleep blink_time
  end
  task_id
end