Module: Chore

Defined in:
lib/chore.rb,
lib/chore/store.rb,
lib/chore/constants.rb,
lib/chore/time_help.rb

Overview

Client module to access the server. Basic usage is something like:

Chore.monitor('task') do
  # ...
end

Refer to the various methods for additional options

Defined Under Namespace

Modules: Constants, Store, TimeHelp

Class Method Summary collapse

Class Method Details

.fail(task, opts = {}) ⇒ Object

Manually indicate that a task has failed.

:error

optional error message



61
62
63
64
# File 'lib/chore.rb', line 61

def self.fail task, opts={}
  opts[:fail_time] ||= Time.now().to_i
  send( [:fail, task, opts] )
end

.finish(task, opts = {}) ⇒ Object

Manually indicate that a task has finished



48
49
50
51
# File 'lib/chore.rb', line 48

def self.finish task, opts={}
  opts[:finish_time] ||= Time.now().to_i
  send( [:finish, task, opts] )
end

.monitor(task, opts = {}, &code) ⇒ Object

Automatically run Chore.start, execute a code block, and automatically run Chore.finish (or Chore.fail in the case of an exception) when the block finishes.

All options from .start, .finish, and .fail may be passed in as options.

In addition to normal opts, :pop => true will automatically remove the task from the store



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chore.rb', line 75

def self.monitor task, opts={}, &code
  pop = false
  if opts[:pop]
    pop = true
    opts.delete(:pop)
  end
  
  Chore.start(task, opts)
  begin
    code.call()
    if pop
      Chore.pop(task)
    else
      Chore.finish(task)
    end
  rescue Exception => ex
    msg = [ex.class, ex.message]
    msg <<  ([''] + ex.backtrace[0..4]) if ex.backtrace.length > 0
    msg << "..." if ex.backtrace.length > 5
    msg = msg.join("\n")

    Chore.fail(task, :error => "#{msg}")
    raise
  end
end

.pop(task, opts = {}) ⇒ Object

Remove a task from monitoring.



54
55
56
# File 'lib/chore.rb', line 54

def self.pop task, opts={}
  send( [:pop, task, opts] )
end

.set_server(ip, port) ⇒ Object

Override the default server settings



15
16
17
18
# File 'lib/chore.rb', line 15

def self.set_server ip, port
  @@server_ip = ip
  @@server_port = port
end

.start(task, opts = {}) ⇒ Object

Let the server know that you’ve started a task. Options you can include are:

:do_every

Indicate that the task should run every X seconds. If this does not happen, show task status in RED.

:grace_period

Allow a grace period for the above option. If we are late but withing the grace period, show task status in YELLOW.

:finish_in

Indicate that the task should finish in X seconds. If we haven’t received a finish message by then, show the task in RED.

:expire_in

Remove the task after X seconds. This may be useful to keep the task list clean.



36
37
38
39
# File 'lib/chore.rb', line 36

def self.start task, opts={}
  opts[:start_time] ||= Time.now().to_i
  send( [:start, task, opts] )
end

.status(task, message) ⇒ Object

Provide an optional status message that can be updated. Only the last status message is retained.



43
44
45
# File 'lib/chore.rb', line 43

def self.status task, message
  send( [:status_update, task, { :status_note => message}] )
end