Class: SFRest::Task
- Inherits:
-
Object
- Object
- SFRest::Task
- 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
-
#delete_task(task_id) ⇒ Object
Deletes a specific task identified by its task id.
-
#find_task(task_id) ⇒ Hash
Retrieve a specific task by ID.
-
#find_task_ids(limit = nil, page = nil, group = nil, klass = nil, status = nil) ⇒ Array[Integer]
Find a set of task ids.
-
#find_tasks(datum = nil) ⇒ Object
Find a set of tasks.
-
#get_task_class_info(type = '') ⇒ Array
returns the classes that are either softpaused or softpause-for-update.
-
#get_task_id(name, group = nil, klass = nil, status = nil) ⇒ Object
Looks for a task with a specific name.
-
#get_task_logs(task_id) ⇒ Object
Get a specific task’s logs.
-
#get_wip_task_status(task_id) ⇒ Hash{"wip_task" => {"id" => Integer, "status" => Integer}, "time" => timestamp}
Get the status of a wip task by id.
-
#globally_paused? ⇒ Boolean
Gets the value of a vairable @TODO: this is missnamed becasue it does not check the global paused variable.
-
#initialize(conn) ⇒ Task
constructor
A new instance of Task.
-
#pause_all_tasks ⇒ Object
Pauses all tasks.
-
#pause_task(task_id, level = 'family') ⇒ Object
Pauses a specific task identified by its task id.
-
#resume_all_tasks ⇒ Object
Resumes all tasks.
-
#resume_task(task_id, level = 'family') ⇒ Object
Resumes a specific task identified by its task id.
-
#status_completed?(status) ⇒ Boolean
Returns true only if the status evaluates to completed.
-
#status_done?(status) ⇒ Boolean
Returns true if the status evaluates to a state that is considered done.
-
#status_error?(status) ⇒ Boolean
Returns true only if the status evaluates to errored.
-
#status_killed?(status) ⇒ Boolean
Returns true only if the status evaluates to killed.
-
#status_not_started?(status) ⇒ Boolean
Returns true only if the status evaluates to not started or restarted.
-
#status_running?(status) ⇒ Boolean
Returns true if the status evaluates to either waiting or in process.
-
#task_completed?(task_id) ⇒ Boolean
Returns true only if WIP reports the status as completed.
-
#task_done?(task_id) ⇒ Boolean
Returns true if WIP reports the status in a state that is considered done.
-
#task_errored?(task_id) ⇒ Boolean
Returns true only if WIP reports the status as errored.
-
#task_killed?(task_id) ⇒ Boolean
Returns true only if WIP reports the status as killed.
-
#task_not_started?(task_id) ⇒ Boolean
Returns true only if WIP reports the status as not started or restarted.
-
#task_running?(task_id) ⇒ Boolean
Returns true only if WIP reports the status as running.
-
#task_status(task_id) ⇒ Hash{"wip_task" => {"id" => Integer, "status" => Integer}, "time" => timestamp}
Get the status of a wip task by id.
-
#terminate_task(task_id) ⇒ Object
Terminates a specific task identified by its task id.
-
#wait_until_done(task_id, max_nap = 600) ⇒ Object
Blocks until a task is done.
-
#wait_until_state(task_id, state, max_nap) ⇒ Object
Blocks until a task reaches a specific status.
Constructor Details
#initialize(conn) ⇒ Task
Returns a new instance of Task.
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
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.
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
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.
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
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
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
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.
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
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. =~ /Variable not found/ end paused end |
#pause_all_tasks ⇒ Object
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
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_tasks ⇒ Object
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
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
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 |