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

Examples:

Defining a type of Task

class MyDeleteTask
  include Task::Task
  # This task has the item_id data field, representing the item to delete
  data_attr_reader :item_id
end

Creating a task

my_task = MyDeleteTask.build(:id => 'my_id', :task_list => "#{service.id}-delete", :item_id => '123')

Serializing and deserializing a task


Saving a task


Fetching a single task that has been saved


Fetching all tasks for a task list


Completing a task, so that it is not longer fetchable


Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

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



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

.interfaceTask::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_hashHash

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

#completeNilClass

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

Raises:

  • (NotImplementedError)


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

def execute(options = {})
  raise NotImplementedError.new('execute method not implemented')
end

#saveNilClass

Saves this task to the data store.



149
150
151
152
# File 'lib/task/task.rb', line 149

def save
  Task.interface.store(self)
  nil
end