Module: Task::Task
- Extended by:
- ActiveSupport::Concern
- Included in:
- Task::Tasks::CompositeTask
- Defined in:
- lib/task/task.rb
Overview
A Task represents a task to be performed. The task can be serialized as JSON to be passed to other processes to complete, and can be stored in Cassandra as a means of ensuring “at-least-once” task completion for this task. So, an example lifecycle of a task would be:
1) A task is generated in a "master" job
2) The master job saves the task, providing a record that the task was generated.
3) The master job passes the task to another job to complete the task.
4) The worker job completes task, removing the record for that task.
5) If the worker job fails, the task is not completed and so the record for it persists.
6) A subsequent job can fetch the list of tasks, returning the tasks that failed to complete.
This job could either serialize them to be completed by other jobs, or complete them directly.
A task has a unique id, belongs to a task list that group similar tasks together, and has data associated with it.
# In the process that generated the task serialized = my_task.as_hash
# In the process that acts on the hash my_task_copy = Task::Task.from_hash(serialized)
my_task.save
Task::DataInterface::Interface.new.find(task_list, task_id)
Task::DataInterface::Interface.new.all(task_list)
my_task.complete
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.all(task_list) ⇒ Enumerator::Lazy<Task::Task>
Returns all tasks for the provided task list.
-
.find(task_list, id) ⇒ Task::Task|NilClass
Returns the task with the given id.
-
.from_hash(task_hash) ⇒ Task::Task
The task object.
-
.interface ⇒ Task::DataInterface::Interface
The Data Interface used.
Instance Method Summary collapse
-
#as_hash ⇒ Hash
Serialized this Task as a hash.
-
#complete ⇒ NilClass
Marks this task as complete, removing it from the datastore.
-
#execute(options = {}) ⇒ Object
Executes this task.
-
#save ⇒ NilClass
Saves this task to the data store.
Class Method Details
.all(task_list) ⇒ Enumerator::Lazy<Task::Task>
Returns all tasks for the provided task list
125 126 127 |
# File 'lib/task/task.rb', line 125 def self.all(task_list) interface.all(task_list) end |
.find(task_list, id) ⇒ Task::Task|NilClass
Returns the task with the given id
118 119 120 |
# File 'lib/task/task.rb', line 118 def self.find(task_list, id) interface.find(task_list, id) end |
.from_hash(task_hash) ⇒ Task::Task
Returns The task object.
108 109 110 111 112 |
# File 'lib/task/task.rb', line 108 def self.from_hash(task_hash) task_hash = task_hash.dup type = task_hash.delete(:type) type.constantize.new(task_hash) end |
.interface ⇒ Task::DataInterface::Interface
The Data Interface used
131 132 133 |
# File 'lib/task/task.rb', line 131 def self.interface DataInterface::Interface.new end |
Instance Method Details
#as_hash ⇒ Hash
Serialized this Task as a hash
143 144 145 |
# File 'lib/task/task.rb', line 143 def as_hash attributes.merge(type: self.class.to_s) end |
#complete ⇒ NilClass
Marks this task as complete, removing it from the datastore
157 158 159 160 |
# File 'lib/task/task.rb', line 157 def complete Task.interface.delete(task_list, id) nil end |
#execute(options = {}) ⇒ Object
Executes this task
137 138 139 |
# File 'lib/task/task.rb', line 137 def execute( = {}) raise NotImplementedError.new('execute method not implemented') end |