Class: Titan::Thread
- Inherits:
-
Object
- Object
- Titan::Thread
- Defined in:
- lib/titan/thread.rb
Overview
Titan::Thread helps you creating daemon threads that are independent from your application. Each Titan::Thread is identified by an id that you can either pass on initialization or that gets created automatically.
Constant Summary collapse
- @@threads =
{}
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#pid ⇒ Object
Returns the value of attribute pid.
Class Method Summary collapse
-
.all ⇒ Object
Returns all Titan-managed threads.
-
.find(id) ⇒ Object
Returns a thread that has the given id.
- .kill(id) ⇒ Object
-
.load_threads ⇒ Object
Loads threads from pid files inside the TITAN_DIRECTORY.
-
.remove_dead_threads ⇒ Object
Removes threads that are not living anymore.
-
.save_threads ⇒ Object
Saves threads to pid files inside the TITAN_DIRECTORY.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Returns whether the thread is alive or not.
-
#initialize(options = {}, &block) ⇒ Thread
constructor
Creates a new daemonized thread.
-
#kill ⇒ Object
Kills the daemonized thread.
-
#pid_file ⇒ Object
Returns the file where its pid gets saved.
-
#run ⇒ Object
Executes the given programm.
-
#save ⇒ Object
Opens the pid file and save its pid in it.
-
#used_cpu ⇒ Object
Returns the used CPU.
-
#used_memory ⇒ Object
Returns the used memory.
Constructor Details
#initialize(options = {}, &block) ⇒ Thread
Creates a new daemonized thread
15 16 17 18 19 20 21 |
# File 'lib/titan/thread.rb', line 15 def initialize( = {}, &block) @id = [:id] || __id__ @pid = -1 @programm = block @@threads[@id] = self self end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
8 9 10 |
# File 'lib/titan/thread.rb', line 8 def id @id end |
#pid ⇒ Object
Returns the value of attribute pid.
8 9 10 |
# File 'lib/titan/thread.rb', line 8 def pid @pid end |
Class Method Details
.all ⇒ Object
Returns all Titan-managed threads
115 116 117 118 |
# File 'lib/titan/thread.rb', line 115 def all load_threads @@threads end |
.find(id) ⇒ Object
Returns a thread that has the given id
103 104 105 106 |
# File 'lib/titan/thread.rb', line 103 def find(id) load_threads @@threads[id] end |
.kill(id) ⇒ Object
108 109 110 |
# File 'lib/titan/thread.rb', line 108 def kill(id) find(id).kill end |
.load_threads ⇒ Object
Loads threads from pid files inside the TITAN_DIRECTORY
123 124 125 126 127 128 129 130 |
# File 'lib/titan/thread.rb', line 123 def load_threads Titan::System.check_filesystem Titan::System.pid_files.each{ |pid_file| thread = Titan::Thread.new(:id => File.basename(pid_file, ".pid")) thread.pid = File.read(File.(pid_file, TITAN_DIRECTORY)).to_i @@threads[thread.id] = thread } end |
.remove_dead_threads ⇒ Object
Removes threads that are not living anymore
143 144 145 146 147 |
# File 'lib/titan/thread.rb', line 143 def remove_dead_threads @@threads.delete_if { |thread_id,thread| !thread.alive? } save_threads @@threads end |
.save_threads ⇒ Object
Saves threads to pid files inside the TITAN_DIRECTORY
135 136 137 138 |
# File 'lib/titan/thread.rb', line 135 def save_threads Titan::System.pid_files.each { |pid_file| File.delete(File.(pid_file, TITAN_DIRECTORY)) } @@threads.each_value { |thread| thread.save } end |
Instance Method Details
#alive? ⇒ Boolean
Returns whether the thread is alive or not
33 34 35 36 37 38 |
# File 'lib/titan/thread.rb', line 33 def alive? Process.getpgid(@pid) true rescue Errno::ESRCH false end |
#kill ⇒ Object
Kills the daemonized thread
26 27 28 |
# File 'lib/titan/thread.rb', line 26 def kill Process.kill('KILL', @pid) end |
#pid_file ⇒ Object
Returns the file where its pid gets saved
53 54 55 |
# File 'lib/titan/thread.rb', line 53 def pid_file File.(@id.to_s + ".pid", TITAN_DIRECTORY) end |
#run ⇒ Object
Executes the given programm
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/titan/thread.rb', line 85 def run @pid = Process.fork do # ignore interrupts Signal.trap('HUP', 'IGNORE') # execute the actual programm @programm.call # exit the forked process cleanly Kernel.exit! end Process.detach(@pid) save self end |
#save ⇒ Object
Opens the pid file and save its pid in it
76 77 78 79 80 |
# File 'lib/titan/thread.rb', line 76 def save Titan::System.check_filesystem File.open(pid_file, 'w') { |file| file.write(@pid) } @@threads[@id] = self end |