Module: Libis::Workflow::Status

Included in:
Base::WorkItem
Defined in:
lib/libis/workflow/status.rb

Constant Summary collapse

STATUS =
{
    NOT_STARTED: 0,
    STARTED: 1,
    DONE: 2,
    ASYNC_WAIT: 3,
    ASYNC_HALT: 4,
    FAILED: 5
}
STATUS_TEXT =
[
    'not started',
    'started',
    'done',
    'waiting for running async process',
    'waiting for halted async process',
    'failed'
]

Instance Method Summary collapse

Instance Method Details

#check_status(state, task = nil) ⇒ Boolean

Check status of the object.

Parameters:

  • state (Symbol)

    status to look for

  • task (String) (defaults to: nil)

    name of task whose status to check

Returns:

  • (Boolean)

    true if the object status matches



75
76
77
# File 'lib/libis/workflow/status.rb', line 75

def check_status(state, task = nil)
  self.status(task) == state
end

#compare_status(state, task = nil) ⇒ Integer

Compare status with current status of the object.

Parameters:

  • state (Symbol)

Returns:

  • (Integer)

    1, 0 or -1 depnding on which



83
84
85
# File 'lib/libis/workflow/status.rb', line 83

def compare_status(state, task = nil)
  STATUS[self.status(task)] <=> STATUS[state]
end

#set_status(task, status) ⇒ Object

Changes the status of the object. The status changed is logged in the status_log with the current timestamp.

Parameters:

  • task (String)

    namepath of the task

  • status (Symbol)

    status to set



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/libis/workflow/status.rb', line 27

def set_status(task, status)
  task = task.namepath if task.is_a?(Libis::Workflow::Task)
  log_entry = self.status_entry(task)
  case status
    when :STARTED
      unless status(task) == :ASYNC_WAIT
        log_entry = self.add_status_log('task' => task, 'status' => status, 'created' => DateTime.now)
      end
    else
      log_entry ||= self.add_status_log('task' => task, 'status' => status, 'created' => DateTime.now)
  end
  log_entry['status'] = status
  log_entry['updated'] = DateTime.now
  self.save!
end

#status(task = nil) ⇒ Symbol

Get last known status symbol for a given task

Parameters:

  • task (String) (defaults to: nil)

    task name to check item status for

Returns:

  • (Symbol)

    the status code



47
48
49
50
# File 'lib/libis/workflow/status.rb', line 47

def status(task = nil)
  entry = self.status_entry(task)
  status_symbol(entry['status']) rescue :NOT_STARTED
end

#status_label(task = nil) ⇒ String

Gets the last known status label of the object.

Parameters:

  • task (String) (defaults to: nil)

    name of task to get the status for

Returns:

  • (String)

    status label ( = task name + status )



65
66
67
68
# File 'lib/libis/workflow/status.rb', line 65

def status_label(task = nil)
  entry = self.status_entry(task)
  "#{entry['task'] rescue nil}#{entry['status'].capitalize rescue nil}"
end

#status_progress(task, progress, max = nil) ⇒ Object

Update the progress of the working task

Parameters:

  • task (String)

    namepath of the task

  • progress (Integer)

    progress indicator (as <progress> of <max> or as % if <max> not set). Default: 0

  • max (Integer) (defaults to: nil)

    max count.



91
92
93
94
95
96
97
98
# File 'lib/libis/workflow/status.rb', line 91

def status_progress(task, progress, max = nil)
  log_entry = self.status_entry(task)
  log_entry ||= self.add_status_log('task' => task, 'status' => :STARTED, 'created' => DateTime.now)
  log_entry['progress'] = progress ? progress : (log_entry['progress'] || 0) + 1
  log_entry['max'] = max if max
  log_entry['updated'] = DateTime.now
  self.save!
end

#status_text(task = nil) ⇒ Symbol

Get last known status text for a given task

Parameters:

  • task (String) (defaults to: nil)

    task name to check item status for

Returns:

  • (Symbol)

    the status code



56
57
58
59
# File 'lib/libis/workflow/status.rb', line 56

def status_text(task = nil)
  entry = self.status_entry(task)
  status_string(entry['status']) rescue STATUS_TEXT.first
end