Module: Libis::Workflow::Base::WorkItem

Includes:
Enumerable, Logging, Status
Included in:
FileItem, Run, WorkItem
Defined in:
lib/libis/workflow/base/work_item.rb

Overview

Base module for all work items.

This module lacks the implementation for the data attributes. It functions as an interface that describes the common functionality regardless of the storage implementation. These attributes require some implementation:

  • status: [Symbol] the status field. Each task sets the status of the items it works on. Before starting processing

    the status is set to "#{task_name}Started". After successfull processing it is set to "#{task_name}Done" and if
    the task failed, it is set to "#{task_name}Failed". The status field can be used to perform real-time
    monitoring, reporting and error-recovery or restart of the ingest.
    The initial value for this attribute is :START.
    
  • parent: [Object|nil] a link to a parent work item. Work items can be organized in any hierarchy you think is

    relevant for your workflow (e.g. directory[/directory...]/file/line or library/section/book/page). Of course
    hierarchies are not mandatory.
    
  • items: [Array] a list of child work items. see above.

  • options: [Hash] a set of options for the task chain on how to deal with this work item. This attribute can be

    used to fine-tune the behaviour of tasks for a particular work item.
    
  • properties: [Hash] a set of properties, typically collected during the workflow processing and used to store

    final or intermediate resulst of tasks. The ::Lias::Ingester::FileItem module uses this attribute to store the
    properties (e.g. size, checksum, ...) of the file it represents.
    
  • log_history: [Array] a list of all logging messages collected for this work item. Whenever a task logs a message

    it will automatically be registered for the work item that it is processing or for the work item that was
    supplied as the first argument.
    
  • status_log: [Array] a list of all status changes the work item went through.

  • summary: [Hash] collected statistics about the ingest for the work item and its children. This structure will

    be filled in by the included task ::Lias::Ingester::Tasks::Analyzer wich is appended to the workflow by default.
    

The module is created so that it is possible to implement an ActiveRecord/Datamapper/… implementation easily. A simple in-memory implementation would require:

attr_accessor :parent attr_accessor :items attr_accessor :options, :properties attr_accessor :log_history, :status_log attr_accessor :summary

def initialize

self.parent = nil
self.items = []
self.options = {}
self.properties = {}
self.log_history = []
self.status_log = []
self.summary = {}

end

protected

## Methods below should be adapted to match the implementation of the log arrays

def add_log_entry(msg)

self.log_history << msg.merge(c_at: ::Time.now)

end

def add_status_log(message, tasklist = nil)

self.status_log << { c_at: ::Time.now, tasklist: tasklist, text: message }.cleanup

end

Constant Summary

Constants included from Status

Status::STATUS

Instance Method Summary collapse

Methods included from Logging

#<=, #add_log, #log_message

Methods included from Status

#check_status, #compare_status, #status, #status=, #status_label

Instance Method Details

#add_item(item) ⇒ Object Also known as: <<

Add a child work item

Parameters:

  • item (WorkItem)

    to be added to the child list :items



126
127
128
129
130
131
132
133
# File 'lib/libis/workflow/base/work_item.rb', line 126

def add_item(item)
  return self unless item and item.is_a?(Libis::Workflow::Base::WorkItem)
  self.items << item
  item.parent = self
  self.save!
  item.save!
  self
end

#each(&block) ⇒ Object

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



113
114
115
# File 'lib/libis/workflow/base/work_item.rb', line 113

def each(&block)
  self.items.each(&block)
end

#get_itemsObject

Get list of items.

This method should return a list of items that is safe to iterate over while it is being altered.



140
141
142
# File 'lib/libis/workflow/base/work_item.rb', line 140

def get_items
  self.items.dup
end

#get_parentLibis::Workflow::Base::WorkItem

Return item’s parent



146
147
148
# File 'lib/libis/workflow/base/work_item.rb', line 146

def get_parent
  self.parent
end

#get_rootLibis::Workflow::Base::WorkItem

go up the hierarchy and return the topmost work item



153
154
155
# File 'lib/libis/workflow/base/work_item.rb', line 153

def get_root
  self.get_parent && self.get_parent.is_a?(Libis::Workflow::Base::WorkItem) && self.get_parent.get_root || self
end

#get_runLibis::Workflow::Base::Run

Get the top



160
161
162
163
# File 'lib/libis/workflow/base/work_item.rb', line 160

def get_run
  return self if self.is_a?(Libis::Workflow::Base::Run)
  self.get_parent && self.get_parent.get_run || nil
end

#nameString

String representation of the identity of the work item.

You may want to overwrite this method as it tries the :name property or whatever #inspect returns if that failes. Typically this should return the key value, file name or id number. If that’s what your :name property contains, you’re fine.

Returns:

  • (String)

    string identification for this work item.



84
85
86
87
# File 'lib/libis/workflow/base/work_item.rb', line 84

def name
  # noinspection RubyResolve
  self.properties[:name] || self.inspect
end

#namepathObject



97
98
99
# File 'lib/libis/workflow/base/work_item.rb', line 97

def namepath;
  self.names.join('/');
end

#namesObject



93
94
95
# File 'lib/libis/workflow/base/work_item.rb', line 93

def names
  (self.parent.names rescue Array.new).push(name).compact
end

#saveObject

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.



168
169
# File 'lib/libis/workflow/base/work_item.rb', line 168

def save
end

#save!Object

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.



174
175
# File 'lib/libis/workflow/base/work_item.rb', line 174

def save!
end

#sizeObject Also known as: count



117
118
119
# File 'lib/libis/workflow/base/work_item.rb', line 117

def size
  self.items.size
end

#to_filenameString

File name safe version of the to_s 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. The default implementation URL-encodes (%xx) all characters except alphanumeric, ‘.’ and ‘-’.

Returns:

  • (String)

    file name



108
109
110
# File 'lib/libis/workflow/base/work_item.rb', line 108

def to_filename
  self.to_s.gsub(/[^\w.-]/) { |s| '%%%02x' % s.ord }
end

#to_sObject



89
90
91
# File 'lib/libis/workflow/base/work_item.rb', line 89

def to_s;
  self.name;
end