module Task::Task

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.

@example 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

@example Creating a task

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

@example Serializing and deserializing a task # 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)

@example Saving a task my_task.save

@example Fetching a single task that has been saved Task::DataInterface::Interface.new.find(task_list, task_id)

@example Fetching all tasks for a task list Task::DataInterface::Interface.new.all(task_list)

@example Completing a task, so that it is not longer fetchable my_task.complete

Public Class Methods

all(task_list) click to toggle source

Returns all tasks for the provided task list @param [String] task_list @return [Enumerator::Lazy<Task::Task>]

# File lib/task/task.rb, line 125
def self.all(task_list)
  interface.all(task_list)
end
find(task_list, id) click to toggle source

Returns the task with the given id @param [String] task_list @param [String] task_id @return [Task::Task|NilClass]

# File lib/task/task.rb, line 118
def self.find(task_list, id)
  interface.find(task_list, id)
end
from_hash(task_hash) click to toggle source

@param task_hash [Hash] Should contain the :task_list, :id, :type, and :data fields @return [Task::Task] The task object.

# 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() click to toggle source

The Data Interface used @return [Task::DataInterface::Interface]

# File lib/task/task.rb, line 131
def self.interface
  DataInterface::Interface.new
end

Public Instance Methods

as_hash() click to toggle source

Serialized this Task as a hash @return [Hash]

# File lib/task/task.rb, line 143
def as_hash
  attributes.merge(type: self.class.to_s)
end
complete() click to toggle source

Marks this task as complete, removing it from the datastore @return [NilClass]

# File lib/task/task.rb, line 157
def complete
  Task.interface.delete(task_list, id)
  nil
end
execute(options = {}) click to toggle source

Executes this task @param options [Hash] Options specific to the execution of this task

# File lib/task/task.rb, line 137
def execute(options = {})
  raise NotImplementedError.new('execute method not implemented')
end
save() click to toggle source

Saves this task to the data store. @return [NilClass]

# File lib/task/task.rb, line 149
def save
  Task.interface.store(self)
  nil
end