rork 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c7eacb09d72f31f9327a6e786cdb651cf3ab99c8
4
+ data.tar.gz: c142b8c488861893a3219e377e56e244bedf80c3
5
+ SHA512:
6
+ metadata.gz: 478bf81286d188da8902f5237602a103a0127b785464760bcc8e2185aa8f183d0e9b89479b41e964157ca103319f8fd220f4794f555b64e6140a7157db42b482
7
+ data.tar.gz: add361aa7fa173370eab77826a81aa36eb6f066761352104866b1074e7fa8082cc38fd3f6af83de438f00f79fbfe405c182d51d963e1cd33d24a64ef90d81441
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 gyf1214
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ Ruby Fork
2
+ ==============
3
+
4
+ Use `ruby` to run applications as daemon.
5
+
6
+ Usage
7
+ ----------------
8
+
9
+ Write the Forkfile and simply use `rork start` to start daemon and `rork stop` to stop!
10
+
11
+ Forkfile
12
+ -----------
13
+
14
+ run <cmdline>
15
+
16
+ (Required) Determine the command line of the daemon.
17
+
18
+ pid <pidfile>
19
+
20
+ (Required) Determine the file to store pid.
21
+
22
+ log <logfile>
23
+
24
+ Determine the path of `STDOUT` of the daemon. The default value is `/dev/null`.
25
+
26
+ err <errfile>
27
+
28
+ Determine the path of `STDERR` of the daemon. The default value is `/dev/null`
29
+
30
+ License
31
+ ----------
32
+
33
+ The MIT License (MIT).
34
+
35
+ See `LICENSE` for details.
36
+
37
+ Author
38
+ ----------
39
+
40
+ gyf1214
data/bin/rork ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rork'
4
+
5
+ begin
6
+ Parser.parse
7
+ Controller.run ARGV[0]
8
+ rescue Exception => e
9
+ puts e.to_s
10
+ end
@@ -0,0 +1,86 @@
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
+ }
15
+ end
16
+ end
17
+
18
+ public
19
+
20
+ module_function
21
+
22
+ @err = @log = '/dev/null'
23
+
24
+ module_attr :pid, :log, :err, :cmd
25
+
26
+ def start
27
+ begin
28
+ puts 'Starting daemon'
29
+
30
+ raise 'Daemon already running!' if File.exist? @pid
31
+
32
+ id = Process.fork do
33
+ Process.setsid
34
+ STDIN.reopen '/dev/null', 'r'
35
+ STDOUT.reopen @log, 'a'
36
+ STDERR.reopen @err, 'a'
37
+ STDOUT.sync = true
38
+ STDERR.sync = true
39
+ exec @cmd
40
+ end
41
+
42
+ raise 'Fork failed!' if id.nil?
43
+
44
+ File.open @pid, 'w' do |file|
45
+ file << id
46
+ end
47
+ rescue Exception => e
48
+ puts '[Failed]'
49
+ puts e.to_s
50
+ exit
51
+ end
52
+
53
+ puts '[Success]'
54
+ end
55
+
56
+ def stop
57
+ begin
58
+ puts 'Stopping daemon'
59
+
60
+ raise 'Daemon not running!' unless File.exist? @pid
61
+
62
+ id = File.read @pid
63
+
64
+ Process.kill 'TERM', id.to_i
65
+
66
+ File.delete @pid
67
+ rescue Exception => e
68
+ puts '[Failed]'
69
+ puts e.to_s
70
+ exit
71
+ end
72
+
73
+ puts '[Success]'
74
+ end
75
+
76
+ def run cmd
77
+ cmds = {
78
+ 'start' => :start,
79
+ 'stop' => :stop
80
+ }
81
+
82
+ raise "Unknown command #{cmd}" if cmds[cmd].nil?
83
+
84
+ send cmds[cmd]
85
+ end
86
+ end
@@ -0,0 +1,29 @@
1
+ module Parser
2
+ module Inside
3
+ module_function
4
+
5
+ def pid path
6
+ Controller.pid = path
7
+ end
8
+
9
+ def log path
10
+ Controller.log = path
11
+ end
12
+
13
+ def err path
14
+ Controller.err = path
15
+ end
16
+
17
+ def run cmd
18
+ Controller.cmd = cmd
19
+ end
20
+ end
21
+
22
+ module_function
23
+
24
+ def parse
25
+ raise 'Forkfile not found' unless File.exist? 'Forkfile'
26
+ forkfile = File.read 'Forkfile'
27
+ Inside.instance_eval forkfile
28
+ end
29
+ end
data/lib/rork.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'rork/parser'
2
+ require 'rork/controller'
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rork
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - gyf1214
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Run applications as daemon with ruby.
14
+ email: gyf1214@gmail.com
15
+ executables:
16
+ - rork
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.md
20
+ - LICENSE
21
+ files:
22
+ - README.md
23
+ - LICENSE
24
+ - bin/rork
25
+ - lib/rork.rb
26
+ - lib/rork/parser.rb
27
+ - lib/rork/controller.rb
28
+ homepage: https://github.com/gyf1214/rork
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --main
35
+ - README.md
36
+ - --encoding
37
+ - UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.0.14
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: ruby fork
56
+ test_files: []