titan 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +3 -0
- data/LICENSE +22 -0
- data/README.md +89 -0
- data/lib/titan.rb +6 -0
- data/lib/titan/manager.rb +61 -0
- data/lib/titan/thread.rb +45 -0
- data/lib/titan/version.rb +3 -0
- metadata +89 -0
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2010 Stefan Sprenger
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
Titan
|
2
|
+
======
|
3
|
+
|
4
|
+
Titan helps you creating daemon threads, that can be accessed later on. On the one side it creates daemons that run in the background and don't stop if the current process exits. On the other
|
5
|
+
side it manages created daemon threads and provides functionality for accessing them later on by a custom id.
|
6
|
+
|
7
|
+
Usage
|
8
|
+
======
|
9
|
+
|
10
|
+
Creating a new daemon thread using Titan is pretty easy:
|
11
|
+
|
12
|
+
Titan::Thread.new do
|
13
|
+
# here comes the programm
|
14
|
+
sleep(15)
|
15
|
+
puts "I'm awake!"
|
16
|
+
end
|
17
|
+
|
18
|
+
Furthermore you can pass an id to each created thread that can be used for identification in the future:
|
19
|
+
|
20
|
+
Titan::Thread.new(:id => "my_new_thread") do
|
21
|
+
sleep(15)
|
22
|
+
puts "I'm awake!"
|
23
|
+
end
|
24
|
+
|
25
|
+
It's also possible to change the identifier after creation:
|
26
|
+
|
27
|
+
thread = Titan::Thread.new do
|
28
|
+
1+1
|
29
|
+
end
|
30
|
+
thread.id = "my_new_thread"
|
31
|
+
|
32
|
+
The identifier must be unique.
|
33
|
+
|
34
|
+
If you want to access the thread in the future, Titan::Manager is the tool of your choice:
|
35
|
+
|
36
|
+
thread = Titan::Thread.new(:id => "my_new_thread") do
|
37
|
+
sleep(15)
|
38
|
+
puts "I'm awake!"
|
39
|
+
end
|
40
|
+
Titan::Manager.add(thread)
|
41
|
+
|
42
|
+
It manages threads and saves them in a special .titan file that can be found in your home folder.
|
43
|
+
|
44
|
+
You can easily list all available threads:
|
45
|
+
|
46
|
+
Titan::Manager.all
|
47
|
+
|
48
|
+
By using the manager you can find currently running threads using their identifier:
|
49
|
+
|
50
|
+
thread = Titan::Manager.find("my_new_thread")
|
51
|
+
|
52
|
+
A thread can be forced to exit:
|
53
|
+
|
54
|
+
thread.kill if thread.alive?
|
55
|
+
|
56
|
+
If you want to remove threads from the manager, that aren't running any longer, you can do this by:
|
57
|
+
|
58
|
+
Titan::Manager.remove_dead_threads
|
59
|
+
|
60
|
+
Furthermore, you can check if a single thread is alive:
|
61
|
+
|
62
|
+
thread = Titan::Manager.find("my_new_thread")
|
63
|
+
thread.alive? # returns true or false
|
64
|
+
|
65
|
+
Requirements
|
66
|
+
======
|
67
|
+
|
68
|
+
* Linux or Mac OS X
|
69
|
+
|
70
|
+
Bugs
|
71
|
+
======
|
72
|
+
|
73
|
+
Please report bugs at http://github.com/flippingbits/titan/issues.
|
74
|
+
|
75
|
+
Note on Patches/Pull Requests
|
76
|
+
======
|
77
|
+
|
78
|
+
* Fork the project from http://github.com/flippingbits/titan.
|
79
|
+
* Make your feature addition or bug fix.
|
80
|
+
* Add tests for it. This is important so I don't break it in a
|
81
|
+
future version unintentionally.
|
82
|
+
* Commit, do not mess with rakefile, version, or history.
|
83
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
84
|
+
* Send me a pull request. Bonus points for topic branches.
|
85
|
+
|
86
|
+
Copyright
|
87
|
+
======
|
88
|
+
|
89
|
+
Copyright (c) 2010 Stefan Sprenger. See LICENSE for details.
|
data/lib/titan.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module Titan
|
2
|
+
#
|
3
|
+
# Titan::Manager provides access to created threads.
|
4
|
+
# It serializes and deserializes them using YAML.
|
5
|
+
#
|
6
|
+
class Manager
|
7
|
+
TITAN_FILE = "#{File.expand_path('~')}/.titan"
|
8
|
+
|
9
|
+
@@threads = {}
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def add(thread)
|
13
|
+
load_threads
|
14
|
+
@@threads[thread.id] = thread
|
15
|
+
save_threads
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Returns a thread that has the given id
|
20
|
+
#
|
21
|
+
def find(id)
|
22
|
+
load_threads
|
23
|
+
@@threads[id]
|
24
|
+
end
|
25
|
+
|
26
|
+
def kill(id)
|
27
|
+
find(id).kill
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Returns all Titan-managed threads
|
32
|
+
#
|
33
|
+
def all_threads
|
34
|
+
load_threads
|
35
|
+
@@threads
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# Loads threads from the TITAN_FILE
|
40
|
+
#
|
41
|
+
def load_threads
|
42
|
+
return unless File.exists?(TITAN_FILE)
|
43
|
+
@@threads = YAML::load(File.open(TITAN_FILE)) || {}
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# Saves threads to the TITAN_FILE
|
48
|
+
#
|
49
|
+
def save_threads
|
50
|
+
File.open(TITAN_FILE, 'w') { |file| file.write(YAML::dump(@@threads)) }
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# Removes threads that are not living anymore
|
55
|
+
#
|
56
|
+
def remove_dead_threads
|
57
|
+
@@threads.each_value { |thread| @@threads.delete(thread.id) unless thread.alive? }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/titan/thread.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Titan
|
2
|
+
#
|
3
|
+
# Titan::Thread helps you creating daemon threads that are independent from your application.
|
4
|
+
# Each Titan::Thread is identified by an id that you can either pass on initialization or
|
5
|
+
# that gets created automatically.
|
6
|
+
#
|
7
|
+
class Thread
|
8
|
+
attr_accessor :id, :pid
|
9
|
+
|
10
|
+
#
|
11
|
+
# Creates a new daemonized thread
|
12
|
+
#
|
13
|
+
def initialize(options = {}, &block)
|
14
|
+
@id = options[:id] || __id__
|
15
|
+
@pid = Process.fork do
|
16
|
+
# ignore interrupts
|
17
|
+
Signal.trap('HUP', 'IGNORE')
|
18
|
+
# execute the actual programm
|
19
|
+
block.call
|
20
|
+
# exit the forked process cleanly
|
21
|
+
Kernel.exit!
|
22
|
+
end
|
23
|
+
|
24
|
+
Process.detach(@pid)
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# Kills the daemonized thread
|
30
|
+
#
|
31
|
+
def kill
|
32
|
+
Process.kill('KILL', @pid)
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Returns whether the thread is alive or not
|
37
|
+
#
|
38
|
+
def alive?
|
39
|
+
Process.getpgid(@pid)
|
40
|
+
true
|
41
|
+
rescue Errno::ESRCH
|
42
|
+
false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: titan
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Stefan Sprenger
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-24 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 9
|
34
|
+
version: 1.2.9
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Helps you creating and managing daemon threads with Ruby.
|
38
|
+
email: info@stefan-sprenger.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- LICENSE
|
45
|
+
- README.md
|
46
|
+
files:
|
47
|
+
- lib/titan/manager.rb
|
48
|
+
- lib/titan/thread.rb
|
49
|
+
- lib/titan/version.rb
|
50
|
+
- lib/titan.rb
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- CHANGELOG.md
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/flippingbits/titan
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --charset=UTF-8
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Helps you creating and managing daemon threads with Ruby.
|
88
|
+
test_files: []
|
89
|
+
|