- Signal process(es) when some event happens.
Advantages:
- Simple concept.
- Simple implementation.
- Like an observer except no data is passed, just the occurrence of the event
itself.
Disadvantages:
- Signaller process has to define the event and set up the signal. (You could
use XMLRPC to set up the signal.)
- Terminology reversed from Observer/Observable. In this case, the
"Observable" is the "RemoteSignaller".
Example use:
- Let one process know another (non-child process) has terminated.
Add a process to be signalled.
- sig
- signal to be sent
- pid
- pid of process to be signalled
# File util/remotesignaller.rb, line 61
def add_signalled(sig, pid)
@observers = Hash.new unless defined? @observers
@observers[pid] = sig
end
|
signal_waiting(signum = "ALL")
|
Send signal to all waiting processes.
- signum
- Signal processes waiting on this signal, or all waiting processes if
signum is "ALL". signum is a string naming the
signal desired, or the string "ALL".
- Returns:
- Array of processes which could not be signalled.
# File util/remotesignaller.rb, line 74
def signal_waiting(signum = "ALL")
dead = []
if defined? @observers
@observers.each_pair {
|pid, sig|
if signum == "ALL" || signum == sig
begin
Process.kill(sig, pid)
rescue
dead << pid
end
end
}
end
dead
end
Delete a waiting process.
- pid
- Delete process with pid == pid.
# File util/remotesignaller.rb, line 94
def del_signalled(pid)
@observers.delete(pid) if defined? @observers
end
|
del_all_signalled(signum = "ALL")
|
Delete all waiting processes.
- signum
- Delete processes waiting on this signal, or all waiting processes if
signum is "ALL". signum is a string naming the
signal desired, or the string "ALL".
# File util/remotesignaller.rb, line 104
def del_all_signalled(signum = "ALL")
if defined? @observers
@observers.delete_if {
|pid, sig|
signum == "ALL" || signum == sig
}
end
end
|
count_signalled(signum = "ALL")
|
Count processes waiting on signals.
- signum
- Count processes waiting on this signal, or all waiting processes if
signum is "ALL". signum is a string naming the
signal desired, or the string "ALL".
# File util/remotesignaller.rb, line 119
def count_signalled(signum = "ALL")
if defined? @observers
@observers.find_all {
|pid, sig|
signum == "ALL" || signum == sig
}.size
else
0
end
end