class LIBIS::Workflow::WorkItem

Base class for all work items.

This class contains some basic attributes required for making the workflow and tasks behave properly:

The class is created so that it is possible to derive an ActiveRecord/Datamapper/Mongoid/… implementation easily.

Constants

SEV_LABEL

Attributes

items[R]
log_history[RW]
options[RW]
parent[R]
properties[RW]
status[R]
status_log[RW]
summary[RW]
workflow[RW]

Public Class Methods

new() click to toggle source

The initializer takes care of properly setting the correct default values for the attributes. A derived class that wishes to define it's own initializer should take care to call 'super' or make sure it overwrites the attribute definitions itself. (e.g. in a ActiveRecord implementation)

# File lib/libis/workflow/workitems/work_item.rb, line 52
def initialize
  self.status = :START
  self.options = {}
  self.properties = {}
  self.log_history = []
  self.status_log = []
  self.parent = nil
  self.items = []
  self.summary = {}
  self.workflow = nil
end

Public Instance Methods

<<(item) click to toggle source

Add a child work item

@param [WorkItem] item to be added to the child list :items

# File lib/libis/workflow/workitems/work_item.rb, line 129
def <<(item)
  return unless item
  self.items << item
  item.parent = self if item.respond_to? :parent=
  self.save
  item.save
end
add_log(message = {}) click to toggle source

Helper function for the Tasks to add a log entry to the log_history.

The supplied message structure is expected to contain the following fields:

  • :severity : ::Logger::Severity value

  • :id : optional message id

  • :test : message text

  • :names : list of tasks names (task hierarchy) that submits the message

@param [Hash] message

# File lib/libis/workflow/workitems/work_item.rb, line 113
def add_log(message = {})
  msg = message_struct(message)
  self.log_history << msg
  self.save
end
each(&block) click to toggle source

Iterates over the work item clients and invokes code on each of them.

@param [Proc] block procedure that will be invoked for each item. The block gets the item as parameter.

# File lib/libis/workflow/workitems/work_item.rb, line 122
def each(&block)
  self.items.each(&block)
end
failed?() click to toggle source

Check ingest status of the object. The status is checked to see if it ends in 'Failed'.

@return [Boolean] true if the object failed, false otherwise

# File lib/libis/workflow/workitems/work_item.rb, line 100
def failed?
  self.status.to_s =~ /Failed$/ ? true : false
end
save() click to toggle source

Dummy method. It is a placeholder for DB backed implementations. Wherever appropriate WorkItem#save will be called to save the current item's state. If state needs to persisted, you should override this method or make sure your persistence layer implements it in your class.

# File lib/libis/workflow/workitems/work_item.rb, line 140
def save
end
set_status(status) click to toggle source

Changes the status of the object. As a side effect the status is also logged in the status_log with the current timestamp.

@param [Symbol] status

# File lib/libis/workflow/workitems/work_item.rb, line 86
def set_status(status)
  if status != self.status
    self.status_log << {
        timestamp: ::Time.now,
        text: status
    }
    self.status = status
    self.save
  end
end
to_filename() click to toggle source

File name save version of the to_string output. The output should be safe to use as a file name to store work item data. Typical use is when extra file items are created by a task and need to be stored on disk.

@return [String] file name

# File lib/libis/workflow/workitems/work_item.rb, line 78
def to_filename
  self.to_string.gsub(/[^\w.-]/) { |s| '%%%02x' % s.ord }
end
to_string() click to toggle source

String representation of the identity of the work item.

This method should be overwritten in subclass as the default value is rather worthless. Typically this should return the key value, file name or id number.

@return [String] string identification for this work item.

# File lib/libis/workflow/workitems/work_item.rb, line 70
def to_string
  self.inspect
end

Protected Instance Methods

message_struct(opts = {}) click to toggle source

create and return a proper message structure

# File lib/libis/workflow/workitems/work_item.rb, line 148
def message_struct(opts = {})
  opts.reverse_merge!(severity: ::Logger::INFO, id: 0, text: '')
  {
      timestamp: ::Time.now,
      severity: SEV_LABEL[opts[:severity]],
      task_list: opts[:names],
      message: opts[:text]
  }
end
root() click to toggle source

go up the hierarchy and return the topmost work item

@return [WorkItem] the root work item

# File lib/libis/workflow/workitems/work_item.rb, line 161
def root
  root = self
  root = root.parent while root.parent
  root
end

Private Instance Methods

items=(i) click to toggle source
# File lib/libis/workflow/workitems/work_item.rb, line 178
def items=(i)
  @items = i;
end
parent=(p) click to toggle source
# File lib/libis/workflow/workitems/work_item.rb, line 174
def parent=(p)
  @parent = p
end
status=(status) click to toggle source

status setter

# File lib/libis/workflow/workitems/work_item.rb, line 170
def status=(status)
  @status = status
end