raemon 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007-2009 NuLayer Inc. All rights reserved.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,20 @@
1
+ Raemon
2
+ ======
3
+
4
+ Raemon is a Ruby framework for building daemons. It's designed for writing
5
+ master/worker pre-forking servers running on UNIX. More to come.
6
+
7
+
8
+ Credits
9
+ -------
10
+ By: Peter Kieltyka
11
+ Copyright (c) 2007-2009 NuLayer Inc. All rights reserved.
12
+
13
+
14
+ Thanks
15
+ ------
16
+ Raemon was influenced by the following projects:
17
+
18
+ servolux - http://github.com/TwP/servolux
19
+ daemon_kit - http://github.com/kennethkalmer/daemon-kit
20
+ rails - http://github.com/rails/rails
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "raemon"
8
+ gem.summary = "Raemon is a Ruby framework for building UNIX daemons."
9
+ gem.description = "Raemon is a Ruby framework for building UNIX daemons."
10
+ gem.email = "peter.kieltyka@nulayer.com"
11
+ gem.homepage = "http://github.com/pkieltyka/raemon"
12
+ gem.authors = ["Peter Kieltyka"]
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
17
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,51 @@
1
+ $:.unshift ::File.dirname(__FILE__) + '/../lib'
2
+
3
+ require 'rubygems'
4
+ require 'raemon'
5
+ require 'beanstalk-client'
6
+
7
+ class Test
8
+ include Raemon::Worker
9
+
10
+ def start
11
+ logger.info "=> Starting worker #{Process.pid}"
12
+
13
+ @beanstalk = Beanstalk::Pool.new(['localhost:11300'])
14
+ end
15
+
16
+ def stop
17
+ logger.info "=> Stopping worker #{Process.pid}"
18
+
19
+ @beanstalk.close
20
+ exit
21
+ end
22
+
23
+ def execute
24
+ loop do
25
+ stop if shutting_down?
26
+
27
+ begin
28
+ job = @beanstalk.reserve(2)
29
+ rescue Beanstalk::TimedOut
30
+ end
31
+
32
+ if job
33
+ logger.info "(#{Process.ppid}:#{Process.pid}) got job: #{job.inspect}"
34
+
35
+ # process job here ...
36
+ job.delete
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ ROOT_DIR = '/Users/peter/Desktop'
44
+
45
+ # Raemon::Master.startup 3, Test, {
46
+ # :detach => true,
47
+ # :logger => Logger.new("#{ROOT_DIR}/beanstalk.log"),
48
+ # :pid_file => "#{ROOT_DIR}/beanstalk.pid"
49
+ # }
50
+
51
+ Raemon::Master.startup 3, Test
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Setup environment
4
+ case ARGV.first
5
+ when nil
6
+ ENV['RAEMON_ENV'] = 'development'
7
+ when 'start'
8
+ ENV['RAEMON_ENV'] = 'production'
9
+ when 'stop'
10
+ @shutdown = true
11
+ end
12
+
13
+ # Load Raemon Server
14
+ require File.dirname(__FILE__) + '/../config/environment'
15
+
16
+ if @shutdown
17
+ Raemon::Server.shutdown!
18
+ else
19
+ Raemon::Server.startup!
20
+ end
@@ -0,0 +1,30 @@
1
+ # Don't change this file!
2
+ # Configure your daemon in config/environment.rb
3
+
4
+ RAEMON_ROOT = File.expand_path(File.dirname(__FILE__)+'/..') unless defined?(RAEMON_ROOT)
5
+
6
+ module Raemon
7
+ class << self
8
+ def boot!
9
+ return if defined? Raemon::Server
10
+
11
+ load_vendor_libs
12
+
13
+ require 'rubygems'
14
+ require 'raemon'
15
+
16
+ Raemon::Server.run
17
+ end
18
+
19
+ def load_vendor_libs
20
+ Dir.entries("#{RAEMON_ROOT}/vendor").each do |vendor|
21
+ vendor_lib = "#{RAEMON_ROOT}/vendor/#{vendor}/lib"
22
+ if File.directory?(vendor_lib) && vendor != '..'
23
+ $LOAD_PATH.unshift vendor_lib
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Raemon.boot!
@@ -0,0 +1,16 @@
1
+ # Be sure to restart your daemon when you modify this file
2
+
3
+ # Uncomment below to force your daemon into production mode
4
+ #ENV['DAEMON_ENV'] ||= 'production'
5
+
6
+ require File.join(File.dirname(__FILE__), 'boot')
7
+
8
+ require 'ruby-debug'
9
+
10
+ Raemon::Server.run do |config|
11
+ config.name = 'Sampled'
12
+ config.worker_klass = 'Sampled::Worker'
13
+ config.num_workers = 1
14
+
15
+ config.log_level = :info
16
+ end
@@ -0,0 +1,5 @@
1
+ config.detach = false
2
+ config.num_workers = 2
3
+ config.log_level = :debug
4
+
5
+ require 'ruby-debug'
@@ -0,0 +1,3 @@
1
+ config.detach = true
2
+ config.num_workers = 2
3
+ config.log_level = :warn
@@ -0,0 +1 @@
1
+ config.log_level = :debug
@@ -0,0 +1,2 @@
1
+ require 'yaml'
2
+ SETTINGS = YAML.load(File.read(RAEMON_ROOT + "/config/settings.yml"))[RAEMON_ENV]
@@ -0,0 +1,8 @@
1
+ development:
2
+ peter: true
3
+
4
+ production:
5
+ peter: false
6
+
7
+ test:
8
+ peter: true
@@ -0,0 +1,25 @@
1
+ module Sampled
2
+
3
+ class Worker
4
+ include Raemon::Worker
5
+
6
+ def start
7
+ logger.info "Start .. #{Process.ppid}:#{Process.pid}"
8
+ end
9
+
10
+ def stop
11
+ logger.info "=> Stopping worker #{Process.pid}"
12
+ exit
13
+ end
14
+
15
+ def execute
16
+ loop do
17
+ stop if shutting_down?
18
+
19
+ logger.warn "I'm executing .. #{Process.ppid}:#{Process.pid}"
20
+ sleep 2
21
+ end
22
+ end
23
+ end
24
+
25
+ end
data/examples/test.rb ADDED
@@ -0,0 +1,31 @@
1
+ $:.unshift ::File.dirname(__FILE__) + '/../lib'
2
+
3
+ require 'rubygems'
4
+ require 'raemon'
5
+
6
+ class Test
7
+ include Raemon::Worker
8
+
9
+ def start
10
+ logger.info "=> Starting worker #{Process.pid}"
11
+ end
12
+
13
+ def stop
14
+ logger.info "=> Stopping worker #{Process.pid}"
15
+ exit
16
+ end
17
+
18
+ def execute
19
+ loop do
20
+ stop if shutting_down?
21
+
22
+ logger.warn "I'm executing .. #{Process.ppid}:#{Process.pid}"
23
+ sleep 2
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ ROOT_DIR = '/Users/peter/Desktop'
30
+
31
+ Raemon::Master.startup 3, Test
@@ -0,0 +1,80 @@
1
+ module Raemon
2
+ class Master
3
+ attr_reader :worker_pids
4
+ attr_reader :worker_klass
5
+ attr_reader :logger
6
+
7
+ def self.startup(num_workers, worker_klass, opts={})
8
+ master = new(opts)
9
+ master.startup(num_workers, worker_klass)
10
+ end
11
+
12
+ def self.shutdown(pid_file)
13
+ pid = File.open(pid_file, 'r') {|f| f.gets }.to_i
14
+ Process.kill('TERM', pid) if pid > 0
15
+ File.unlink(pid_file)
16
+ rescue Errno::ESRCH
17
+ end
18
+
19
+ def initialize(opts={})
20
+ @detach = opts[:detach] || false
21
+ @logger = opts[:logger] || Logger.new(STDOUT)
22
+ @pid_file = opts[:pid_file]
23
+ @worker_pids = []
24
+
25
+ daemonize if @detach
26
+ end
27
+
28
+ def startup(num_workers, worker_klass)
29
+ logger.info "=> Starting Raemon::Master with #{num_workers} worker(s)"
30
+
31
+ @worker_klass = worker_klass
32
+
33
+ # Check if the worker implements our protocol
34
+ if !worker_klass.include?(Raemon::Worker)
35
+ logger.error "** Invalid Raemon worker"
36
+ exit
37
+ end
38
+
39
+ # Spawn workers
40
+ num_workers.times { worker_pids << worker_klass.start!(self) }
41
+
42
+ # Setup signals for the master process
43
+ setup_signals
44
+
45
+ # Wait for all the workers
46
+ Process.waitall
47
+
48
+ logger.close
49
+ end
50
+
51
+ def shutdown
52
+ @worker_pids.each { |wpid| worker_klass.stop!(wpid) }
53
+ end
54
+
55
+ def daemonize
56
+ exit if Kernel.fork
57
+
58
+ Process.setsid
59
+
60
+ Dir.chdir '/'
61
+ File.umask 0000
62
+
63
+ STDIN.reopen '/dev/null'
64
+ STDOUT.reopen '/dev/null', 'a'
65
+ STDERR.reopen '/dev/null', 'a'
66
+
67
+ File.open(@pid_file, 'w') { |f| f.puts(Process.pid) } if @pid_file
68
+ end
69
+
70
+ def setup_signals
71
+ shutdown_block = Proc.new { shutdown }
72
+
73
+ trap('INT', shutdown_block)
74
+ trap('TERM', shutdown_block)
75
+ end
76
+
77
+ def debugging?; @debug; end
78
+
79
+ end
80
+ end
@@ -0,0 +1,106 @@
1
+ RAEMON_ENV = (ENV['RAEMON_ENV'] || 'development').dup unless defined?(RAEMON_ENV)
2
+
3
+ module Raemon
4
+ module Server
5
+
6
+ class << self
7
+ attr_accessor :config
8
+
9
+ def run
10
+ @config = Configuration.new if config.nil?
11
+ yield config if block_given?
12
+ end
13
+
14
+ def startup!
15
+ load_environment
16
+ load_initializers
17
+ load_lib
18
+
19
+ initialize_logger
20
+
21
+ # Check if the server is already running
22
+ if running?
23
+ STDERR.puts "Error: #{server_name} is already running."
24
+ exit
25
+ end
26
+
27
+ # Start the master daemon
28
+ config.logger.info "=> Booting #{server_name} (#{RAEMON_ENV})"
29
+
30
+ worker_klass = instance_eval(config.worker_klass)
31
+
32
+ Raemon::Master.startup config.num_workers, worker_klass, {
33
+ :detach => config.detach,
34
+ :logger => config.logger,
35
+ :pid_file => pid_file
36
+ }
37
+ end
38
+
39
+ def shutdown!
40
+ Raemon::Master.shutdown pid_file
41
+ end
42
+
43
+ def initialize_logger
44
+ return if !config.logger.nil?
45
+
46
+ if config.detach
47
+ config.logger = Logger.new("#{RAEMON_ROOT}/log/#{server_name_key}.log")
48
+ else
49
+ config.logger = Logger.new(STDOUT)
50
+ end
51
+
52
+ # TODO: format the logger
53
+ # config.logger.format
54
+ end
55
+
56
+ def load_environment
57
+ environment_file = "#{RAEMON_ROOT}/config/environments/#{RAEMON_ENV}.rb"
58
+ eval IO.read(environment_file), binding
59
+ end
60
+
61
+ def load_initializers
62
+ load_folder("#{RAEMON_ROOT}/config/initializers")
63
+ end
64
+
65
+ def load_lib
66
+ load_folder("#{RAEMON_ROOT}/lib")
67
+ end
68
+
69
+ def load_folder(path)
70
+ Dir.entries(path).each do |lib_file|
71
+ if lib_file != '.' && lib_file != '..'
72
+ require "#{path}/#{lib_file}"
73
+ end
74
+ end
75
+ end
76
+
77
+ def server_name
78
+ @server_name = config.name || 'Raemon'
79
+ end
80
+
81
+ def server_name_key
82
+ server_name.downcase.gsub(' ', '_')
83
+ end
84
+
85
+ def pid_file
86
+ "#{RAEMON_ROOT}/tmp/pids/#{server_name_key}.pid"
87
+ end
88
+
89
+ def running?
90
+ # TODO
91
+ false
92
+ end
93
+ end
94
+
95
+ class Configuration
96
+ ATTRIBUTES = [ :name, :detach, :worker_klass, :num_workers, :log_level, :logger ]
97
+
98
+ attr_accessor *ATTRIBUTES
99
+
100
+ def [](key)
101
+ send key rescue nil
102
+ end
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,53 @@
1
+ module Raemon
2
+ module Worker
3
+
4
+ def self.included(base)
5
+ base.send :extend, ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def start!(master=nil)
11
+ child_pid = Kernel.fork do
12
+ # Child process
13
+ worker = new(master)
14
+ worker.execute
15
+ end
16
+
17
+ # Parent returns the worker's pid
18
+ return child_pid
19
+ end
20
+
21
+ def stop!(worker_pid)
22
+ Process.kill('QUIT', worker_pid) rescue nil
23
+ end
24
+ end
25
+
26
+ module InstanceMethods
27
+ attr_reader :logger
28
+
29
+ def initialize(master=nil)
30
+ @master = master
31
+ @logger = master.logger if master
32
+
33
+ setup_signals
34
+ start
35
+ end
36
+
37
+ def start; end
38
+ def stop; end
39
+ def shutting_down?; @shutting_down; end
40
+ def execute; raise "Abstract method"; end
41
+
42
+ def setup_signals
43
+ quit_block = Proc.new { @shutting_down = true }
44
+ force_quit_block = Proc.new { exit }
45
+
46
+ trap('QUIT', quit_block)
47
+ trap('TERM', force_quit_block)
48
+ trap('INT') {} # Reset INT signal handler
49
+ end
50
+ end
51
+
52
+ end
53
+ end
data/lib/raemon.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'socket'
2
+ require 'logger'
3
+
4
+ module Raemon
5
+ VERSION = '0.1'
6
+ end
7
+
8
+ require 'raemon/server'
9
+ require 'raemon/master'
10
+ require 'raemon/worker'
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: raemon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Kieltyka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-26 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Raemon is a Ruby framework for building UNIX daemons.
17
+ email: peter.kieltyka@nulayer.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - VERSION
31
+ - examples/beanstalk.rb
32
+ - examples/sampled/bin/sampled
33
+ - examples/sampled/config/boot.rb
34
+ - examples/sampled/config/environment.rb
35
+ - examples/sampled/config/environments/development.rb
36
+ - examples/sampled/config/environments/production.rb
37
+ - examples/sampled/config/environments/test.rb
38
+ - examples/sampled/config/initializers/settings.rb
39
+ - examples/sampled/config/settings.yml
40
+ - examples/sampled/lib/sampled.rb
41
+ - examples/test.rb
42
+ - lib/raemon.rb
43
+ - lib/raemon/master.rb
44
+ - lib/raemon/server.rb
45
+ - lib/raemon/worker.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/pkieltyka/raemon
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Raemon is a Ruby framework for building UNIX daemons.
74
+ test_files:
75
+ - examples/beanstalk.rb
76
+ - examples/sampled/config/boot.rb
77
+ - examples/sampled/config/environment.rb
78
+ - examples/sampled/config/environments/development.rb
79
+ - examples/sampled/config/environments/production.rb
80
+ - examples/sampled/config/environments/test.rb
81
+ - examples/sampled/config/initializers/settings.rb
82
+ - examples/sampled/lib/sampled.rb
83
+ - examples/test.rb