cicloid-backdrop 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-09-10
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2008 Jonathan Weiss, based on work taken
2
+ from Based on Highrise to LDAP Gateway http://svn.thoughtbot.com/highrise-ldap-proxy/
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ PostInstall.txt
5
+ README.txt
6
+ Rakefile
7
+ bin/backdrop-daemon
8
+ config/hoe.rb
9
+ config/requirements.rb
10
+ lib/backdrop.rb
11
+ lib/backdrop/formatter.rb
12
+ lib/backdrop/pid_file.rb
13
+ lib/backdrop/runner.rb
14
+ lib/backdrop/server.rb
15
+ lib/backdrop/version.rb
16
+ lib/daemon.rb
17
+ script/console
18
+ script/destroy
19
+ script/generate
20
+ script/txt2html
21
+ setup.rb
22
+ tasks/deployment.rake
23
+ tasks/environment.rake
24
+ tasks/website.rake
25
+ test/test_backdrop.rb
26
+ test/test_helper.rb
27
+ website/index.html
28
+ website/index.txt
29
+ website/javascripts/rounded_corners_lite.inc.js
30
+ website/stylesheets/screen.css
31
+ website/template.html.erb
@@ -0,0 +1,11 @@
1
+
2
+ For more information on backdrop, see http://backdrop.rubyforge.org
3
+
4
+ In order to run a backdrop daemon:
5
+
6
+ * Create an Backdrop::Runner implementation
7
+
8
+ * Create matching config.yml
9
+
10
+ See the README at http://github.com/peritor/backdrop/tree/master for details
11
+
@@ -0,0 +1,93 @@
1
+ = backdrop
2
+
3
+ http://labs.peritor.com/backdrop
4
+
5
+ == DESCRIPTION:
6
+
7
+ A Ruby daemon library that makes writing background daemons easy
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ Backdrop allows you to easily create Ruby background daemons. It utilizes the daemon gem to go
12
+ into the background and has some additional management utilities that allow you to control the
13
+ running backdrop.
14
+
15
+ == SYNOPSIS:
16
+
17
+ $ backdrop-daemon start config.yml
18
+
19
+ $ backdrop-daemon stop config.yml
20
+
21
+ $ backdrop-daemon restart config.yml
22
+
23
+
24
+ Where config.yml is:
25
+
26
+ ---
27
+ daemon_path: /path/to/my/daemon.rb
28
+ log_file: /path/to/mylog
29
+ pid_file: /path/to/store.pid
30
+ debug: false
31
+
32
+ The `daemon_path` config parameter refers to your Ruby library that will be required by backdrop.
33
+ This file needs to implement the actual processing task and can react to start/stop.
34
+
35
+ Example:
36
+
37
+ module Backdrop
38
+ class Runner
39
+ def self.run(logger)
40
+ logger.info "Running im my loop"
41
+ # .. doing the background operation once
42
+ # will be called in a loop
43
+ end
44
+
45
+ def self.start(logger)
46
+ # optional - react to starting the daemon
47
+ # e.g. do setup
48
+ end
49
+
50
+ def self.run(logger)
51
+ # optional - react to stopping the daemon
52
+ # e.g. do resource teardown
53
+ end
54
+ end
55
+ end
56
+
57
+ == REQUIREMENTS:
58
+
59
+ * ActiveSupport
60
+
61
+ == INSTALL:
62
+
63
+ * sudo gem install backdrop
64
+
65
+ * Create an Backdrop::Runner implementation
66
+
67
+ * Create matching config.yml
68
+
69
+ == LICENSE:
70
+
71
+ (The MIT License)
72
+
73
+ Copyright (c) 2008 Jonathan Weiss, based on work taken
74
+ from Based on Highrise to LDAP Gateway http://svn.thoughtbot.com/highrise-ldap-proxy/
75
+
76
+ Permission is hereby granted, free of charge, to any person obtaining
77
+ a copy of this software and associated documentation files (the
78
+ 'Software'), to deal in the Software without restriction, including
79
+ without limitation the rights to use, copy, modify, merge, publish,
80
+ distribute, sublicense, and/or sell copies of the Software, and to
81
+ permit persons to whom the Software is furnished to do so, subject to
82
+ the following conditions:
83
+
84
+ The above copyright notice and this permission notice shall be
85
+ included in all copies or substantial portions of the Software.
86
+
87
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
88
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
89
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
90
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
91
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
92
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
93
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ # require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 0
4
+ :minor: 1
@@ -0,0 +1,11 @@
1
+ #! /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
2
+
3
+ require 'rubygems'
4
+ require 'backdrop'
5
+
6
+ case ARGV[0]
7
+ when "start": Backdrop::Server.new(ARGV[1]).start
8
+ when "stop": Backdrop::Server.new(ARGV[1]).stop
9
+ when "restart": Backdrop::Server.new(ARGV[1]).restart
10
+ else puts "Usage: #{File.basename(__FILE__)} {start|stop|restart} [config file]"
11
+ end
@@ -0,0 +1,15 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Backdrop
5
+ end
6
+
7
+ %w( rubygems thread etc active_support yaml erb fileutils logger resolv-replace daemon tmpdir).each do |lib|
8
+ require lib
9
+ end
10
+
11
+ require "backdrop/version"
12
+ require "backdrop/pid_file"
13
+ require "backdrop/formatter"
14
+ require "backdrop/runner"
15
+ require "backdrop/server"
@@ -0,0 +1,7 @@
1
+ module Backdrop
2
+ class Formatter < Logger::Formatter
3
+ def call(severity, timestamp, progname, msg)
4
+ "#{timestamp.strftime("%H:%M:%S")}: #{severity} - #{msg}\n"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ require 'fileutils'
2
+ module Backdrop
3
+ class PidFile
4
+ attr_reader :file
5
+ def initialize(file)
6
+ @file = file
7
+ end
8
+
9
+ def pid
10
+ File.file?(@file) and IO.read(@file)
11
+ end
12
+
13
+ def remove
14
+ if self.pid
15
+ FileUtils.rm @file
16
+ end
17
+ end
18
+
19
+ def create
20
+ File.open(@file, "w") { |f| f.write($$) }
21
+ end
22
+
23
+ def ensure_empty!(msg = nil)
24
+ if self.pid
25
+ puts msg if msg
26
+ exit 1
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ module Backdrop
2
+ class Runner
3
+ def self.run(logger)
4
+ raise "please implement Backdrop::Runner.run yourself!"
5
+ end
6
+
7
+ def self.start(logger)
8
+ end
9
+
10
+ def self.stop(logger)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,100 @@
1
+ module Backdrop
2
+ class Server
3
+ attr_accessor :config, :logger, :pidfile
4
+ include Daemon
5
+
6
+ def initialize(config_file = nil)
7
+ options = YAML.load(ERB.new(File.read(config_file)).result).symbolize_keys
8
+
9
+ @config = {
10
+ :log_file => "#{Dir.tmpdir}/backdrop.log",
11
+ :pid_file => "#{Dir.tmpdir}/backdrop.pid",
12
+ :debug => false,
13
+ :daemon_path => nil # set to path pointing to you daemon implementation
14
+ }.update(options)
15
+
16
+ validate!
17
+
18
+ self.logger = Logger.new(config[:log_file])
19
+ self.logger.level = config[:debug] ? Logger::DEBUG : Logger::INFO
20
+ self.logger.formatter = Backdrop::Formatter.new
21
+ self.logger.info ""
22
+
23
+ @pidfile = Backdrop::PidFile.new(config[:pid_file])
24
+ end
25
+
26
+ def validate!
27
+ raise ArgumentError, "Need a main daemon implementation class, please set :daemon_path" if config[:daemon_path].blank?
28
+ end
29
+
30
+ def become_user(username = 'nobody', chroot = false)
31
+ user = Etc::getpwnam(username)
32
+
33
+ Dir.chroot(user.dir) and Dir.chdir('/') if chroot
34
+
35
+ Process::initgroups(username, user.gid)
36
+ Process::Sys::setegid(user.gid)
37
+ Process::Sys::setgid(user.gid)
38
+ Process::Sys::setuid(user.uid)
39
+ end
40
+
41
+ def start
42
+ pidfile.ensure_empty! "ERROR: It looks like I'm already running. Not starting."
43
+
44
+ logger.info "Starting server"
45
+ logger.info "logging to #{config[:log_file]}"
46
+ logger.info "storing PID at #{config[:pid_file]}"
47
+ logger.info "requiring main implementation from #{config[:daemon_path]}"
48
+
49
+ require config[:daemon_path]
50
+
51
+ daemonize(logger)
52
+
53
+ logger.info "Became daemon with process id: #{$$}"
54
+ begin
55
+ pidfile.create
56
+ rescue Exception => e
57
+ logger.info "Exception caught while creating pidfile: #{e}"
58
+ exit
59
+ end
60
+
61
+ trap("TERM") do
62
+ logger.info("Received signal TERM - Shutting down") if logger
63
+ pidfile.remove if pidfile
64
+ exit
65
+ end
66
+
67
+ Backdrop::Runner.start(logger)
68
+
69
+ # main loop run
70
+ begin
71
+ loop do
72
+ Backdrop::Runner.run(logger)
73
+ end
74
+ rescue Object => e
75
+ logger.error "Exception in runner: #{e.message}\n#{e.backtrace.join("\n")}"
76
+ stop
77
+ end
78
+ end
79
+
80
+ def stop
81
+ if @pidfile.pid
82
+
83
+ Backdrop::Runner.stop(logger)
84
+
85
+ puts "Sending signal TERM to process #{pidfile.pid}" if config[:debug]
86
+ logger.info("Killing server with PID #{pidfile.pid}")
87
+ Process.kill("TERM", pidfile.pid.to_i)
88
+ @pidfile.remove
89
+ else
90
+ puts "PID cannot be found. Server not running?"
91
+ end
92
+ end
93
+
94
+ def restart
95
+ stop
96
+ sleep 5
97
+ start
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,9 @@
1
+ module Backdrop
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ class Logger
4
+ attr_accessor :logdev
5
+ end
6
+
7
+ module Daemon
8
+ def daemonize(logger = nil)
9
+ # This causes the grandchild process to be orphaned,
10
+ # so the init process is responsible for cleaning it up.
11
+ Kernel.fork and Kernel.exit
12
+ Process.setsid
13
+ Kernel.fork and Kernel.exit
14
+
15
+ File.umask 0
16
+ Dir.chdir '/'
17
+
18
+ ObjectSpace.each_object(IO) do |io|
19
+ unless (logger and logger.logdev.dev == io)
20
+ io.close rescue nil
21
+ end
22
+ end
23
+
24
+ STDIN.reopen( '/dev/null')
25
+ STDOUT.reopen('/dev/null', 'a')
26
+ STDERR.reopen('/dev/null', 'a')
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ ---
2
+ daemon_path: <%= File.expand_path(File.dirname(__FILE__) + "/test/test_impl.rb") %>
3
+ log_file: /tmp/foo.log
4
+ pid_file: /tmp/foo.pid
5
+ debug: true
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestBackdrop < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_calling_runner
9
+ # Daemon.expects(:daemonize).returns(true)
10
+ # Backdrop::Runner.expects(:run).raises(RuntimeError)
11
+ # Backdrop::Runner.expects(:start).returns(nil)
12
+ # Backdrop::Runner.expects(:stop).returns(nil)
13
+ #
14
+ # server = Backdrop::Server.new(File.dirname(__FILE__) + '/test.yml')
15
+ # server.pidfile.expects(:ensure_empty!).returns(true)
16
+ # server.start
17
+ end
18
+
19
+ end
@@ -0,0 +1,4 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'mocha'
4
+ require File.dirname(__FILE__) + '/../lib/backdrop'
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'backdrop'
5
+
6
+ module Backdrop
7
+ class Runner
8
+ def self.run(logger)
9
+ logger.info "Running im my loop"
10
+ system("echo 'c' >> /tmp/c")
11
+ end
12
+ end
13
+ end
14
+
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cicloid-backdrop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Gustavo Barr\xC3\xB3n"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-14 00:00:00 -07:00
13
+ default_executable: backdrop-daemon
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: gustavo@foobarra.com
18
+ executables:
19
+ - backdrop-daemon
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ files:
25
+ - History.txt
26
+ - License.txt
27
+ - Manifest.txt
28
+ - PostInstall.txt
29
+ - README.txt
30
+ - Rakefile
31
+ - VERSION.yml
32
+ - bin/backdrop-daemon
33
+ - lib/backdrop.rb
34
+ - lib/backdrop/formatter.rb
35
+ - lib/backdrop/pid_file.rb
36
+ - lib/backdrop/runner.rb
37
+ - lib/backdrop/server.rb
38
+ - lib/backdrop/version.rb
39
+ - lib/daemon.rb
40
+ - test/test.yml
41
+ - test/test_backdrop.rb
42
+ - test/test_helper.rb
43
+ - test/test_impl.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/cicloid/backdrop
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.2.0
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: TODO
70
+ test_files:
71
+ - test/test_backdrop.rb
72
+ - test/test_helper.rb
73
+ - test/test_impl.rb