Class: Relay::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/relay/task.rb

Overview

Task represents a Rake task that can be run in a separate process. The process becomes a group leader, so that if it spawns any child processes, they will be in the same process group.

A task provides its status through a channel, which can be either “success” or “error” depending on whether the task completed successfully or not.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ void

Parameters:

  • task (String)

    The name of the Rake task to run



27
28
29
30
31
# File 'lib/relay/task.rb', line 27

def initialize(task)
  @task = task
  @ch = xchan(:pure)
  @pid = nil
end

Instance Attribute Details

#chChan::UNIXSocket (readonly)

Returns:

  • (Chan::UNIXSocket)


17
18
19
# File 'lib/relay/task.rb', line 17

def ch
  @ch
end

#pidInteger (readonly)

Returns:

  • (Integer)


21
22
23
# File 'lib/relay/task.rb', line 21

def pid
  @pid
end

Instance Method Details

#callInteger

Call the task in a separate process

Returns:

  • (Integer)

    The PID of the child process



37
38
39
40
41
42
43
44
45
# File 'lib/relay/task.rb', line 37

def call
  @pid = fork do
    become_group_leader
    invoke_task
    record_success
  rescue
    record_error
  end
end

#statusString

Read the status of a task (non-blocking)

Returns:

  • (String)

    Either “success” or “error”

Raises:

  • (Chan::WaitReadable)

    When the channel is not ready to be read



53
54
55
56
# File 'lib/relay/task.rb', line 53

def status
  return @status if defined?(@status)
  @status = ch.recv_nonblock
end