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)


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

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

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

Find a set of task ids.

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])


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

def find_task_ids(limit = nil, page = nil, group = nil, klass = nil, status = nil)
  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_*



169
170
171
172
173
# File 'lib/sfrest/task.rb', line 169

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



271
272
273
274
# File 'lib/sfrest/task.rb', line 271

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



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sfrest/task.rb', line 180

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)


211
212
213
214
# File 'lib/sfrest/task.rb', line 211

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})


279
280
281
282
# File 'lib/sfrest/task.rb', line 279

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)


219
220
221
222
223
224
225
226
227
228
229
# File 'lib/sfrest/task.rb', line 219

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.



196
197
198
199
200
# File 'lib/sfrest/task.rb', line 196

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



235
236
237
238
239
# File 'lib/sfrest/task.rb', line 235

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.



203
204
205
206
207
# File 'lib/sfrest/task.rb', line 203

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



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

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)


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

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



287
288
289
# File 'lib/sfrest/task.rb', line 287

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



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/sfrest/task.rb', line 295

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}?".to_sym)
  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