simonmenke-shuttle 0.1.08 → 0.1.09

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
 
3
+ autoload :URI, 'uri'
3
4
  autoload :Etc, 'etc'
4
5
  autoload :DRb, 'drb'
5
6
  autoload :TSort, 'tsort'
@@ -15,6 +16,7 @@ module Shuttle
15
16
  autoload :Server, (base+'/shuttle/server')
16
17
  autoload :Client, (base+'/shuttle/client')
17
18
  autoload :System, (base+'/shuttle/system')
19
+ autoload :Daemon, (base+'/shuttle/daemon')
18
20
  autoload :JobQueue, (base+'/shuttle/job_queue')
19
21
  autoload :Satellite, (base+'/shuttle/satellite')
20
22
  autoload :AppRunner, (base+'/shuttle/app_runner')
@@ -50,7 +52,6 @@ module Shuttle
50
52
 
51
53
  THOR_VERSION = '>= 0.9.9'
52
54
  RUBIGEN_VERSION = '>= 1.5.2'
53
- SIMPLE_DEAMON_VERSION = '>= 0.1.2'
54
55
 
55
56
  Shuttle::ExceptionHandler.setup
56
57
  extend ExceptionHandler
@@ -8,7 +8,6 @@ module Shuttle
8
8
  desc "start", 'start the server'
9
9
  method_options :foreground => :boolean, :config => :optional
10
10
  def start
11
- Shuttle.runtime_gem('simple-daemon', Shuttle::SIMPLE_DEAMON_VERSION)
12
11
  Shuttle.server? true
13
12
  Shuttle::System.load!(options[:root_path])
14
13
 
@@ -24,11 +23,9 @@ module Shuttle
24
23
  exit(1)
25
24
  end
26
25
 
27
- SimpleDaemon.const_set 'WORKING_DIRECTORY', Shuttle.system.root
28
26
  if options[:foreground]
29
27
  Shuttle::Server.start
30
28
  else
31
- ARGV.clear and ARGV.concat(%w( start ))
32
29
  Shuttle::Server.daemonize
33
30
  end
34
31
  end
@@ -0,0 +1,71 @@
1
+
2
+ module Shuttle
3
+ module Daemon
4
+ class Base
5
+
6
+ def self.daemonize(&block)
7
+ Shuttle::Daemon::Controller.start(self,&block)
8
+ end
9
+
10
+ end
11
+
12
+ module PidFile
13
+ def self.store(pid)
14
+ File.open(self.pid_file, 'w') {|f| f << pid}
15
+ end
16
+
17
+ def self.recall
18
+ IO.read(self.pid_file).to_i rescue nil
19
+ end
20
+
21
+ def self.destroy
22
+ FileUtils.rm(self.pid_file) if self.exist?
23
+ end
24
+
25
+ def self.pid_file
26
+ Shuttle.system.path("Server.pid")
27
+ end
28
+
29
+ def self.exist?
30
+ File.file?(self.pid_file)
31
+ end
32
+ end
33
+
34
+ module Controller
35
+
36
+ def self.start(daemon, &block)
37
+ fork do
38
+ Process.setsid
39
+ exit if fork
40
+ if PidFile.exist?
41
+ puts "Pid file #{PidFile.pid_file} already exists. Not starting."
42
+ exit 1
43
+ end
44
+ Shuttle::Daemon::PidFile.store(Process.pid)
45
+ Dir.chdir Shuttle.system.root
46
+ File.umask 0000
47
+
48
+ Shuttle::ExceptionHandler.redirect_std
49
+
50
+ trap("TERM") { daemon.stop; exit }
51
+ at_exit { Shuttle::Daemon::PidFile.destroy if $master }
52
+ at_exit(&block) if block
53
+ daemon.start
54
+ end
55
+ puts "Daemon started."
56
+ end
57
+
58
+ def self.stop
59
+ if !Shuttle::Daemon::PidFile.exist?
60
+ puts "Pid file not found. Is the daemon started?"
61
+ exit
62
+ end
63
+ pid = Shuttle::Daemon::PidFile.recall
64
+ pid && Process.kill("TERM", pid)
65
+ rescue Errno::ESRCH
66
+ puts "Pid file found, but process was not running. The daemon may have died."
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -4,15 +4,21 @@ module Shuttle
4
4
 
5
5
  def self.setup(out=STDOUT, err=STDERR)
6
6
  if String === out
7
- @out = Logger.new(out, 'daily')
7
+ @out = Logger.new(out, 'daily')
8
+ @stdout = @out.instance_variable_get('@logdev').instance_variable_get('@dev')
8
9
  else
9
- @out = Logger.new(out)
10
+ @out = Logger.new(out)
11
+ @stdout = out
10
12
  end
13
+
11
14
  if String === err
12
15
  @err = Logger.new(err, 'daily')
16
+ @stderr = @err.instance_variable_get('@logdev').instance_variable_get('@dev')
13
17
  else
14
- @err = Logger.new(err)
18
+ @err = Logger.new(err)
19
+ @stderr = err
15
20
  end
21
+
16
22
  @out.level = Logger::DEBUG
17
23
  @err.level = Logger::DEBUG
18
24
  end
@@ -25,6 +31,26 @@ module Shuttle
25
31
  @out
26
32
  end
27
33
 
34
+ def self.stderr
35
+ @stderr
36
+ end
37
+
38
+ def self.stdout
39
+ @stdout
40
+ end
41
+
42
+ def self.redirect_std
43
+ if STDOUT != self.stdout
44
+ STDOUT.reopen self.stdout
45
+ end
46
+
47
+ if STDERR != self.stderr
48
+ STDERR.reopen self.stderr
49
+ end
50
+
51
+ STDIN.reopen "/dev/null"
52
+ end
53
+
28
54
  def logger
29
55
  Shuttle::ExceptionHandler
30
56
  end
@@ -1,9 +1,8 @@
1
- Shuttle.runtime_gem('simple-daemon', Shuttle::SIMPLE_DEAMON_VERSION)
2
1
 
3
2
  module Shuttle
4
3
 
5
4
  # the Server is supposed to be a simple DRb interface to the System.
6
- class Server < SimpleDaemon::Base
5
+ class Server < Shuttle::Daemon::Base
7
6
  include DRbUndumped
8
7
 
9
8
  autoload :Proxy, File.dirname(__FILE__)+'/server/proxy'
@@ -23,17 +22,20 @@ module Shuttle
23
22
 
24
23
  def stop_server
25
24
  Shuttle.log "stopping the server..."
26
- Thread.new { sleep(1) and exit(Shuttle::STOP_STATUS) }
25
+ $exitstatus = Shuttle::STOP_STATUS
26
+ DRb.stop_service
27
27
  end
28
28
 
29
29
  def restart_server
30
30
  Shuttle.log "restarting the server..."
31
- Thread.new { sleep(1) and exit(Shuttle::RESTART_STATUS) }
31
+ $exitstatus = Shuttle::RESTART_STATUS
32
+ DRb.stop_service
32
33
  end
33
34
 
34
35
  def reload_server
35
36
  Shuttle.log "reloading the server..."
36
- Thread.new { sleep(1) and exit(Shuttle::RELOAD_STATUS) }
37
+ $exitstatus = Shuttle::RELOAD_STATUS
38
+ DRb.stop_service
37
39
  end
38
40
 
39
41
  def server_version
@@ -1,4 +1,3 @@
1
- require 'uri'
2
1
 
3
2
  module Shuttle
4
3
  class Server
@@ -19,7 +18,7 @@ module Shuttle
19
18
  uri.to_s
20
19
  end
21
20
 
22
- # stop teh server
21
+ # stop the server
23
22
  def stop
24
23
  Shuttle.client.stop_server
25
24
  end
@@ -31,6 +30,7 @@ module Shuttle
31
30
 
32
31
  # start the failsafe runner.
33
32
  def start_with_failsafe
33
+ $master = true
34
34
  stop_server = false
35
35
  wait_before_start = 0
36
36
  retries = 0
@@ -41,6 +41,7 @@ module Shuttle
41
41
  end
42
42
 
43
43
  pid = Process.fork { self.run_server }
44
+ return unless pid
44
45
  Process.waitpid(pid, 0)
45
46
  case $?.exitstatus
46
47
  when Shuttle::STOP_STATUS
@@ -52,19 +53,18 @@ module Shuttle
52
53
  when Shuttle::RELOAD_STATUS
53
54
  stop_server = true
54
55
  retries = 0
55
- Shuttle.system.run(%{sleep 2 ; #{Shuttle::BIN_PATH} #{ORIGINAL_ARGV.join(' ')}})
56
+ Shuttle::Daemon::PidFile.destroy
57
+ Shuttle.system.run %{#{Shuttle::BIN_PATH} #{ORIGINAL_ARGV.join(' ')}}
56
58
  else
57
59
  retries += 1
58
60
  stop_server = true if retries >= 3
59
61
  end
60
62
  end
61
-
62
- path = Shuttle.system.path('Server.pid')
63
- File.unlink(path) if File.file?(path)
64
63
  end
65
64
 
66
65
  # start the actual DRb server
67
66
  def run_server
67
+ $master = false
68
68
  Dir.chdir(Shuttle.system.root)
69
69
 
70
70
  Shuttle.log "Server started"
@@ -74,13 +74,12 @@ module Shuttle
74
74
  make_client_cert_public!
75
75
 
76
76
  at_exit do
77
- unless $task_child
78
- Shuttle.system.queue.stop!
79
- Shuttle.log "Server stopped"
80
- end
77
+ Shuttle.system.queue.stop!
78
+ Shuttle.log "Server stopped"
81
79
  end
82
80
 
83
81
  DRb.thread.join
82
+ exit($exitstatus || Shuttle::STOP_STATUS)
84
83
  end
85
84
 
86
85
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simonmenke-shuttle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.08
4
+ version: 0.1.09
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Menke
@@ -42,6 +42,7 @@ files:
42
42
  - lib/shuttle/apps/server.rb
43
43
  - lib/shuttle/client/auth_token.rb
44
44
  - lib/shuttle/client.rb
45
+ - lib/shuttle/daemon.rb
45
46
  - lib/shuttle/exception_handler.rb
46
47
  - lib/shuttle/extentions/rubygems_plugin.rb
47
48
  - lib/shuttle/extentions/thor_extentions.rb