titan 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +4 -0
- data/README.md +7 -15
- data/lib/titan.rb +0 -1
- data/lib/titan/thread.rb +58 -1
- data/lib/titan/version.rb +1 -1
- metadata +3 -8
- data/lib/titan/manager.rb +0 -63
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -39,35 +39,27 @@ It's also possible to change the identifier after creation:
|
|
39
39
|
|
40
40
|
The identifier must be unique.
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
thread = Titan::Thread.new(:id => "my_new_thread") do
|
45
|
-
sleep(15)
|
46
|
-
puts "I'm awake!"
|
47
|
-
end
|
48
|
-
Titan::Manager.add(thread)
|
49
|
-
|
50
|
-
It manages threads and saves them in a special .titan file that can be found in your home folder.
|
42
|
+
Titan manages created threads and saves them in a special .titan file that can be found in your home folder.
|
51
43
|
|
52
44
|
You can easily list all available threads:
|
53
45
|
|
54
|
-
Titan::
|
46
|
+
Titan::Thread.all
|
55
47
|
|
56
|
-
By using the
|
48
|
+
By using the Thread class you can find currently managed threads by using their identifier:
|
57
49
|
|
58
|
-
thread = Titan::
|
50
|
+
thread = Titan::Thread.find("my_new_thread")
|
59
51
|
|
60
52
|
A thread can be forced to exit:
|
61
53
|
|
62
54
|
thread.kill if thread.alive?
|
63
55
|
|
64
|
-
If you want to remove threads from
|
56
|
+
If you want to remove threads from Titan, that aren't running any longer, you can do this by:
|
65
57
|
|
66
|
-
Titan::
|
58
|
+
Titan::Thread.remove_dead_threads
|
67
59
|
|
68
60
|
Furthermore, you can check if a single thread is alive:
|
69
61
|
|
70
|
-
thread = Titan::
|
62
|
+
thread = Titan::Thread.find("my_new_thread")
|
71
63
|
thread.alive? # returns true or false
|
72
64
|
|
73
65
|
Requirements
|
data/lib/titan.rb
CHANGED
data/lib/titan/thread.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "yaml"
|
2
|
+
|
1
3
|
module Titan
|
2
4
|
#
|
3
5
|
# Titan::Thread helps you creating daemon threads that are independent from your application.
|
@@ -5,8 +7,12 @@ module Titan
|
|
5
7
|
# that gets created automatically.
|
6
8
|
#
|
7
9
|
class Thread
|
10
|
+
TITAN_FILE = "#{File.expand_path('~')}/.titan"
|
11
|
+
|
8
12
|
attr_accessor :id, :pid
|
9
13
|
|
14
|
+
@@threads = {}
|
15
|
+
|
10
16
|
#
|
11
17
|
# Creates a new daemonized thread
|
12
18
|
#
|
@@ -22,7 +28,7 @@ module Titan
|
|
22
28
|
end
|
23
29
|
|
24
30
|
Process.detach(@pid)
|
25
|
-
self
|
31
|
+
Thread.add(self)
|
26
32
|
end
|
27
33
|
|
28
34
|
#
|
@@ -41,5 +47,56 @@ module Titan
|
|
41
47
|
rescue Errno::ESRCH
|
42
48
|
false
|
43
49
|
end
|
50
|
+
|
51
|
+
class << self
|
52
|
+
def add(thread)
|
53
|
+
load_threads
|
54
|
+
@@threads[thread.id] = thread
|
55
|
+
save_threads
|
56
|
+
thread
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# Returns a thread that has the given id
|
61
|
+
#
|
62
|
+
def find(id)
|
63
|
+
load_threads
|
64
|
+
@@threads[id]
|
65
|
+
end
|
66
|
+
|
67
|
+
def kill(id)
|
68
|
+
find(id).kill
|
69
|
+
end
|
70
|
+
|
71
|
+
#
|
72
|
+
# Returns all Titan-managed threads
|
73
|
+
#
|
74
|
+
def all
|
75
|
+
load_threads
|
76
|
+
@@threads
|
77
|
+
end
|
78
|
+
|
79
|
+
#
|
80
|
+
# Loads threads from the TITAN_FILE
|
81
|
+
#
|
82
|
+
def load_threads
|
83
|
+
return unless File.exists?(TITAN_FILE)
|
84
|
+
@@threads = YAML::load(File.open(TITAN_FILE)) || {}
|
85
|
+
end
|
86
|
+
|
87
|
+
#
|
88
|
+
# Saves threads to the TITAN_FILE
|
89
|
+
#
|
90
|
+
def save_threads
|
91
|
+
File.open(TITAN_FILE, 'w') { |file| file.write(YAML::dump(@@threads)) }
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# Removes threads that are not living anymore
|
96
|
+
#
|
97
|
+
def remove_dead_threads
|
98
|
+
@@threads.each_value { |thread| @@threads.delete(thread.id) unless thread.alive? }
|
99
|
+
end
|
100
|
+
end
|
44
101
|
end
|
45
102
|
end
|
data/lib/titan/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: titan
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 25
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
7
|
+
- 1
|
8
8
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.3
|
9
|
+
version: 0.1.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Stefan Sprenger
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-11-
|
17
|
+
date: 2010-11-26 00:00:00 +01:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,7 +25,6 @@ dependencies:
|
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 13
|
30
28
|
segments:
|
31
29
|
- 1
|
32
30
|
- 2
|
@@ -44,7 +42,6 @@ extra_rdoc_files:
|
|
44
42
|
- LICENSE
|
45
43
|
- README.md
|
46
44
|
files:
|
47
|
-
- lib/titan/manager.rb
|
48
45
|
- lib/titan/thread.rb
|
49
46
|
- lib/titan/version.rb
|
50
47
|
- lib/titan.rb
|
@@ -65,7 +62,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
62
|
requirements:
|
66
63
|
- - ">="
|
67
64
|
- !ruby/object:Gem::Version
|
68
|
-
hash: 3
|
69
65
|
segments:
|
70
66
|
- 0
|
71
67
|
version: "0"
|
@@ -74,7 +70,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
70
|
requirements:
|
75
71
|
- - ">="
|
76
72
|
- !ruby/object:Gem::Version
|
77
|
-
hash: 3
|
78
73
|
segments:
|
79
74
|
- 0
|
80
75
|
version: "0"
|
data/lib/titan/manager.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
require "yaml"
|
2
|
-
|
3
|
-
module Titan
|
4
|
-
#
|
5
|
-
# Titan::Manager provides access to created threads.
|
6
|
-
# It serializes and deserializes them using YAML.
|
7
|
-
#
|
8
|
-
class Manager
|
9
|
-
TITAN_FILE = "#{File.expand_path('~')}/.titan"
|
10
|
-
|
11
|
-
@@threads = {}
|
12
|
-
|
13
|
-
class << self
|
14
|
-
def add(thread)
|
15
|
-
load_threads
|
16
|
-
@@threads[thread.id] = thread
|
17
|
-
save_threads
|
18
|
-
end
|
19
|
-
|
20
|
-
#
|
21
|
-
# Returns a thread that has the given id
|
22
|
-
#
|
23
|
-
def find(id)
|
24
|
-
load_threads
|
25
|
-
@@threads[id]
|
26
|
-
end
|
27
|
-
|
28
|
-
def kill(id)
|
29
|
-
find(id).kill
|
30
|
-
end
|
31
|
-
|
32
|
-
#
|
33
|
-
# Returns all Titan-managed threads
|
34
|
-
#
|
35
|
-
def all_threads
|
36
|
-
load_threads
|
37
|
-
@@threads
|
38
|
-
end
|
39
|
-
|
40
|
-
#
|
41
|
-
# Loads threads from the TITAN_FILE
|
42
|
-
#
|
43
|
-
def load_threads
|
44
|
-
return unless File.exists?(TITAN_FILE)
|
45
|
-
@@threads = YAML::load(File.open(TITAN_FILE)) || {}
|
46
|
-
end
|
47
|
-
|
48
|
-
#
|
49
|
-
# Saves threads to the TITAN_FILE
|
50
|
-
#
|
51
|
-
def save_threads
|
52
|
-
File.open(TITAN_FILE, 'w') { |file| file.write(YAML::dump(@@threads)) }
|
53
|
-
end
|
54
|
-
|
55
|
-
#
|
56
|
-
# Removes threads that are not living anymore
|
57
|
-
#
|
58
|
-
def remove_dead_threads
|
59
|
-
@@threads.each_value { |thread| @@threads.delete(thread.id) unless thread.alive? }
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|