titan 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## 0.2.0 (December 4, 2010)
2
+
3
+ - Features
4
+
5
+ * Use pid files for each thread instead of one global .titan file
6
+ * !!! You've to call Titan::Thread#run in order to start the created thread
7
+
8
+ - Bug fixes
9
+
10
+ * Synchronize threads when removing dead ones (Thanks to Tal Atlas)
11
+
1
12
  ## 0.1.1 (November 26, 2010)
2
13
 
3
14
  - Features
data/README.md CHANGED
@@ -15,31 +15,32 @@ and require it
15
15
 
16
16
  require "titan"
17
17
 
18
- Creating a new daemon thread using Titan is pretty easy:
18
+ Creating a new daemon thread using Titan is pretty easy. First, you've create a new Titan::Thread object and then call its #run method:
19
19
 
20
- Titan::Thread.new do
20
+ thread = Titan::Thread.new do
21
21
  # here comes the programm
22
22
  sleep(15)
23
23
  puts "I'm awake!"
24
24
  end
25
+ thread.run
25
26
 
26
27
  Furthermore you can pass an id to each created thread that can be used for identification in the future:
27
28
 
28
29
  Titan::Thread.new(:id => "my_new_thread") do
29
30
  sleep(15)
30
31
  puts "I'm awake!"
31
- end
32
+ end.run
32
33
 
33
- It's also possible to change the identifier after creation:
34
+ It's also possible to change the identifier after creation and while the thread is running:
34
35
 
35
36
  thread = Titan::Thread.new do
36
37
  1+1
37
- end
38
+ end.run
38
39
  thread.id = "my_new_thread"
39
40
 
40
41
  The identifier must be unique.
41
42
 
42
- Titan manages created threads and saves them in a special .titan file that can be found in your home folder.
43
+ Titan manages created threads and saves them in pid files inside of a .titan\_threads directory that can be found in your home folder.
43
44
 
44
45
  You can easily list all available threads:
45
46
 
data/lib/titan/thread.rb CHANGED
@@ -7,7 +7,7 @@ module Titan
7
7
  # that gets created automatically.
8
8
  #
9
9
  class Thread
10
- TITAN_FILE = "#{File.expand_path('~')}/.titan"
10
+ TITAN_DIRECTORY = File.expand_path('.titan_threads', '~')
11
11
 
12
12
  attr_accessor :id, :pid
13
13
 
@@ -17,18 +17,11 @@ module Titan
17
17
  # Creates a new daemonized thread
18
18
  #
19
19
  def initialize(options = {}, &block)
20
- @id = options[:id] || __id__
21
- @pid = Process.fork do
22
- # ignore interrupts
23
- Signal.trap('HUP', 'IGNORE')
24
- # execute the actual programm
25
- block.call
26
- # exit the forked process cleanly
27
- Kernel.exit!
28
- end
29
-
30
- Process.detach(@pid)
31
- Titan::Thread.add(self)
20
+ @id = options[:id] || __id__
21
+ @pid = -1
22
+ @programm = block
23
+ @@threads[@id] = self
24
+ self
32
25
  end
33
26
 
34
27
  #
@@ -48,6 +41,9 @@ module Titan
48
41
  false
49
42
  end
50
43
 
44
+ #
45
+ # Changes the id of the thread and starts synchronization
46
+ #
51
47
  def id=(id)
52
48
  @@threads.delete(@id)
53
49
  @id = id
@@ -55,14 +51,40 @@ module Titan
55
51
  Titan::Thread.save_threads
56
52
  end
57
53
 
58
- class << self
59
- def add(thread)
60
- load_threads
61
- @@threads[thread.id] = thread
62
- save_threads
63
- thread
54
+ #
55
+ # Returns the file where its pid gets saved
56
+ #
57
+ def pid_file
58
+ File.expand_path(@id.to_s + ".pid", Titan::Thread::TITAN_DIRECTORY)
59
+ end
60
+
61
+ #
62
+ # Opens the pid file and save its pid in it
63
+ #
64
+ def save
65
+ Titan::Thread.check_filesystem
66
+ File.open(pid_file, 'w') { |file| file.write(@pid) }
67
+ @@threads[@id] = self
68
+ end
69
+
70
+ #
71
+ # Executes the given programm
72
+ #
73
+ def run
74
+ @pid = Process.fork do
75
+ # ignore interrupts
76
+ Signal.trap('HUP', 'IGNORE')
77
+ # execute the actual programm
78
+ @programm.call
79
+ # exit the forked process cleanly
80
+ Kernel.exit!
64
81
  end
82
+ Process.detach(@pid)
83
+ save
84
+ self
85
+ end
65
86
 
87
+ class << self
66
88
  #
67
89
  # Returns a thread that has the given id
68
90
  #
@@ -84,25 +106,46 @@ module Titan
84
106
  end
85
107
 
86
108
  #
87
- # Loads threads from the TITAN_FILE
109
+ # Loads threads from pid files inside the TITAN_DIRECTORY
88
110
  #
89
111
  def load_threads
90
- return unless File.exists?(TITAN_FILE)
91
- @@threads = YAML::load(File.open(TITAN_FILE)) || {}
112
+ check_filesystem
113
+ pid_files.each{ |pid_file|
114
+ thread = Titan::Thread.new(:id => File.basename(pid_file, ".pid"))
115
+ thread.pid = File.read(File.expand_path(pid_file, TITAN_DIRECTORY))
116
+ @@threads[thread.id] = thread
117
+ }
92
118
  end
93
119
 
94
120
  #
95
- # Saves threads to the TITAN_FILE
121
+ # Saves threads to pid files inside the TITAN_DIRECTORY
96
122
  #
97
123
  def save_threads
98
- File.open(TITAN_FILE, 'w') { |file| file.write(YAML::dump(@@threads)) }
124
+ pid_files.each { |pid_file| File.delete (File.expand_path(pid_file, TITAN_DIRECTORY)) }
125
+ @@threads.each_value{ |thread| thread.save }
99
126
  end
100
127
 
101
128
  #
102
129
  # Removes threads that are not living anymore
103
130
  #
104
131
  def remove_dead_threads
105
- @@threads.each_value { |thread| @@threads.delete(thread.id) unless thread.alive? }
132
+ @@threads.delete_if { |thread_id,thread| !thread.alive? }
133
+ save_threads
134
+ @@threads
135
+ end
136
+
137
+ #
138
+ # Checks the file system for neccessary directories and permissions
139
+ #
140
+ def check_filesystem
141
+ Dir.mkdir(TITAN_DIRECTORY) unless File.directory?(TITAN_DIRECTORY)
142
+ end
143
+
144
+ #
145
+ # Returns a list of all pid files available in the TITAN_DIRECTORY
146
+ #
147
+ def pid_files
148
+ Dir.entries(TITAN_DIRECTORY) - [".", ".."]
106
149
  end
107
150
  end
108
151
  end
data/lib/titan/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Titan
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Stefan Sprenger
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-26 00:00:00 +01:00
17
+ date: 2010-12-04 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -26,10 +26,10 @@ dependencies:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
- - 1
30
29
  - 2
31
- - 9
32
- version: 1.2.9
30
+ - 0
31
+ - 0
32
+ version: 2.0.0
33
33
  type: :development
34
34
  version_requirements: *id001
35
35
  description: Helps you creating and managing daemon threads with Ruby.