rork 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7eacb09d72f31f9327a6e786cdb651cf3ab99c8
4
- data.tar.gz: c142b8c488861893a3219e377e56e244bedf80c3
3
+ metadata.gz: cdad2aad4fc9aca53fbc6619b7c5c3a3e914981a
4
+ data.tar.gz: 40b76f3d98f26464851765723a727b4f5998ce94
5
5
  SHA512:
6
- metadata.gz: 478bf81286d188da8902f5237602a103a0127b785464760bcc8e2185aa8f183d0e9b89479b41e964157ca103319f8fd220f4794f555b64e6140a7157db42b482
7
- data.tar.gz: add361aa7fa173370eab77826a81aa36eb6f066761352104866b1074e7fa8082cc38fd3f6af83de438f00f79fbfe405c182d51d963e1cd33d24a64ef90d81441
6
+ metadata.gz: 455e44f3b6cc920626391e9de2e0a25cc1d84afb749a73f4d21bd3123bc1a70efa2c2ce0b067d852b923e66378233c6360818851291d3263f965a5475b7db793
7
+ data.tar.gz: fde646b547ed0df780c6f8b072c238c65c7af0675af9b009cac21f42fa89a634bc1125d99d5c8a07751c2847c0a5453a05f9d3cd24b4a483d73f146adc52fa14
data/bin/rork CHANGED
@@ -4,7 +4,7 @@ require 'rork'
4
4
 
5
5
  begin
6
6
  Parser.parse
7
- Controller.run ARGV[0]
7
+ Controller.run ARGV[0], ARGV[1..-1]
8
8
  rescue Exception => e
9
9
  puts e.to_s
10
10
  end
@@ -1,47 +1,44 @@
1
1
  module Controller
2
- private
3
-
4
- def self.module_attr *names
5
- names.each do |name|
6
- instance_eval %{
7
- def #{name}
8
- @#{name}
9
- end
10
-
11
- def #{name}= arg
12
- @#{name} = arg
13
- end
14
- }
2
+ class Application
3
+ attr_accessor :pid, :log, :err, :cmd
4
+
5
+ def initialize
6
+ @err = @log = '/dev/null'
15
7
  end
16
8
  end
17
9
 
18
- public
19
-
20
10
  module_function
21
11
 
22
- @err = @log = '/dev/null'
23
-
24
- module_attr :pid, :log, :err, :cmd
12
+ @apps = Hash.new
13
+ def [] key
14
+ @apps[key] = Application.new unless @apps.key? key
15
+ @apps[key]
16
+ end
25
17
 
26
- def start
18
+ def start args = []
27
19
  begin
28
- puts 'Starting daemon'
20
+ args[0] = 'default' if args[0].nil?
21
+ @app = @apps[args[0]]
22
+ raise "Daemon #{args[0]} not found!" if @app.nil?
29
23
 
30
- raise 'Daemon already running!' if File.exist? @pid
24
+ puts "Starting daemon #{args[0]}..."
25
+ raise "Pid file must be given for #{args[0]}!" if @app.pid.nil?
26
+ raise "Command must be given for #{args[0]}!" if @app.cmd.nil?
27
+ raise "Daemon #{args[0]} already running!" if File.exist? @app.pid
31
28
 
32
29
  id = Process.fork do
33
30
  Process.setsid
34
31
  STDIN.reopen '/dev/null', 'r'
35
- STDOUT.reopen @log, 'a'
36
- STDERR.reopen @err, 'a'
32
+ STDOUT.reopen @app.log, 'a'
33
+ STDERR.reopen @app.err, 'a'
37
34
  STDOUT.sync = true
38
35
  STDERR.sync = true
39
- exec @cmd
36
+ exec @app.cmd
40
37
  end
41
38
 
42
39
  raise 'Fork failed!' if id.nil?
43
40
 
44
- File.open @pid, 'w' do |file|
41
+ File.open @app.pid, 'w' do |file|
45
42
  file << id
46
43
  end
47
44
  rescue Exception => e
@@ -53,17 +50,22 @@ module Controller
53
50
  puts '[Success]'
54
51
  end
55
52
 
56
- def stop
53
+ def stop args = []
57
54
  begin
58
- puts 'Stopping daemon'
55
+ args[0] = 'default' if args[0].nil?
56
+ @app = @apps[args[0]]
57
+ raise "Daemon #{args[0]} not found!" if @app.nil?
59
58
 
60
- raise 'Daemon not running!' unless File.exist? @pid
59
+ puts "Stopping daemon #{args[0]}..."
60
+ raise "Pid file must be given for #{args[0]}!" if @app.pid.nil?
61
+ raise "Daemon #{args[0]}not running!" unless File.exist? @app.pid
61
62
 
62
- id = File.read @pid
63
+ id = File.read @app.pid
64
+ id = id.to_i
63
65
 
64
- Process.kill 'TERM', id.to_i
66
+ Process.kill 'TERM', id
65
67
 
66
- File.delete @pid
68
+ File.delete @app.pid
67
69
  rescue Exception => e
68
70
  puts '[Failed]'
69
71
  puts e.to_s
@@ -73,7 +75,7 @@ module Controller
73
75
  puts '[Success]'
74
76
  end
75
77
 
76
- def run cmd
78
+ def run cmd, args = []
77
79
  cmds = {
78
80
  'start' => :start,
79
81
  'stop' => :stop
@@ -81,6 +83,6 @@ module Controller
81
83
 
82
84
  raise "Unknown command #{cmd}" if cmds[cmd].nil?
83
85
 
84
- send cmds[cmd]
86
+ send cmds[cmd], args
85
87
  end
86
88
  end
@@ -3,19 +3,25 @@ module Parser
3
3
  module_function
4
4
 
5
5
  def pid path
6
- Controller.pid = path
6
+ @app.pid = path
7
7
  end
8
8
 
9
9
  def log path
10
- Controller.log = path
10
+ @app.log = path
11
11
  end
12
12
 
13
13
  def err path
14
- Controller.err = path
14
+ @app.err = path
15
15
  end
16
16
 
17
17
  def run cmd
18
- Controller.cmd = cmd
18
+ @app.cmd = cmd
19
+ end
20
+
21
+ def app name = 'default'
22
+ @app = Controller[name.to_s]
23
+ yield
24
+ @app = Controller['default']
19
25
  end
20
26
  end
21
27
 
@@ -24,6 +30,8 @@ module Parser
24
30
  def parse
25
31
  raise 'Forkfile not found' unless File.exist? 'Forkfile'
26
32
  forkfile = File.read 'Forkfile'
27
- Inside.instance_eval forkfile
33
+ Inside.app do
34
+ Inside.instance_eval forkfile
35
+ end
28
36
  end
29
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gyf1214
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-21 00:00:00.000000000 Z
11
+ date: 2014-06-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Run applications as daemon with ruby.
14
14
  email: gyf1214@gmail.com