Class: Titan::Thread

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(options = {}, &block)
  @id       = options[:id] || __id__
  @pid      = -1
  @programm = block
  @@threads[@id] = self
  self
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/titan/thread.rb', line 8

def id
  @id
end

#pidObject

Returns the value of attribute pid.



8
9
10
# File 'lib/titan/thread.rb', line 8

def pid
  @pid
end

Class Method Details

.allObject

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_threadsObject

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.expand_path(pid_file, TITAN_DIRECTORY)).to_i
    @@threads[thread.id] = thread
  }
end

.remove_dead_threadsObject

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_threadsObject

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.expand_path(pid_file, TITAN_DIRECTORY)) }
  @@threads.each_value { |thread| thread.save }
end

Instance Method Details

#alive?Boolean

Returns whether the thread is alive or not

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/titan/thread.rb', line 33

def alive?
  Process.getpgid(@pid)
  true
rescue Errno::ESRCH
  false
end

#killObject

Kills the daemonized thread



26
27
28
# File 'lib/titan/thread.rb', line 26

def kill
  Process.kill('KILL', @pid)
end

#pid_fileObject

Returns the file where its pid gets saved



53
54
55
# File 'lib/titan/thread.rb', line 53

def pid_file
  File.expand_path(@id.to_s + ".pid", TITAN_DIRECTORY)
end

#runObject

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

#saveObject

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

#used_cpuObject

Returns the used CPU



68
69
70
71
# File 'lib/titan/thread.rb', line 68

def used_cpu
  used_cpu = Titan::System.ps('%cpu', @pid)
  used_cpu ? used_cpu.to_f : nil
end

#used_memoryObject

Returns the used memory



60
61
62
63
# File 'lib/titan/thread.rb', line 60

def used_memory
  used_memory = Titan::System.ps('rss', @pid)
  used_memory ? used_memory.to_i : nil
end