rspawn 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 808e145c459114cf4a24056ca188b10c2e3de2a8
4
+ data.tar.gz: e5e0518f35b59263f63c1d9ad2e9c725662247c9
5
+ SHA512:
6
+ metadata.gz: 069428bf3aad0405cfff6a652bae5b28ee2f004a4734030e6ce67076313634c37cb2b57d966a0f1502ce1eea9535a3924cab5efd453833f0300b2c02505ebb3b
7
+ data.tar.gz: 97cfeb88d60b15aeb23296a8db8c10cea0e823e706644b12d1020799bbf6e4908942a79525f020c2fc60d678429119db5077cf626a9b41532f10ccf5b726f9a9
@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.md
3
+ LICENSE.txt
@@ -0,0 +1,10 @@
1
+ Gemfile.lock
2
+ doc/
3
+ pkg/
4
+ **/*.gem
5
+ .yardoc/
6
+ .bundle/
7
+ vendor/bundle/
8
+ .tags
9
+ *.pid
10
+ *.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1
7
+ - ruby-head
8
+
9
+ os:
10
+ - linux
11
+ - osx
12
+
13
+ gemfile:
14
+ - Gemfile
15
+
16
+ script: 'bundle exec rake spec'
@@ -0,0 +1 @@
1
+ --markup markdown --title "rspawn Documentation" --protected
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2015-02-07
2
+
3
+ * Initial release:
4
+
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'kramdown'
7
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Hiroshi Toyama
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.
@@ -0,0 +1,45 @@
1
+ # rspawn [![Build Status](https://secure.travis-ci.org/toyama0919/rspawn.png?branch=master)](http://travis-ci.org/toyama0919/rspawn)
2
+
3
+ TODO: Summary
4
+
5
+ TODO: Description
6
+
7
+ ## Examples
8
+
9
+ $ rspawn sample
10
+ #=> hoge
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'rspawn'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install rspawn
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new [Pull Request](../../pull/new/master)
33
+
34
+ ## Information
35
+
36
+ * [Homepage](https://github.com/toyama0919/rspawn)
37
+ * [Issues](https://github.com/toyama0919/rspawn/issues)
38
+ * [Documentation](http://rubydoc.info/gems/rspawn/frames)
39
+ * [Email](mailto:toyama0919@gmail.com)
40
+
41
+ ## Copyright
42
+
43
+ Copyright (c) 2015 Hiroshi Toyama
44
+
45
+ See [LICENSE.txt](../LICENSE.txt) for details.
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ begin
6
+ require 'bundler'
7
+ rescue LoadError => e
8
+ warn e.message
9
+ warn "Run `gem install bundler` to install Bundler."
10
+ exit -1
11
+ end
12
+
13
+ begin
14
+ Bundler.setup(:development)
15
+ rescue Bundler::BundlerError => e
16
+ warn e.message
17
+ warn "Run `bundle install` to install missing gems."
18
+ exit e.status_code
19
+ end
20
+
21
+ require 'rake'
22
+
23
+ require 'rubygems/tasks'
24
+ Gem::Tasks.new
25
+
26
+ require 'rspec/core/rake_task'
27
+ RSpec::Core::RakeTask.new
28
+
29
+ task :test => :spec
30
+ task :default => :spec
31
+
32
+ require 'yard'
33
+ YARD::Rake::YardocTask.new
34
+ task :doc => :yard
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'rspawn'
5
+
6
+ Rspawn::CLI.start
@@ -0,0 +1,5 @@
1
+ require 'rspawn/version'
2
+ require 'rspawn/worker'
3
+ require 'rspawn/core'
4
+ require 'rspawn/config'
5
+ require 'rspawn/cli'
@@ -0,0 +1,100 @@
1
+ require 'thor'
2
+ require 'json'
3
+
4
+ module Rspawn
5
+ class CLI < Thor
6
+
7
+ include Thor::Actions
8
+
9
+ class_option :root, type: :string, default: ENV['HOME'] + '/.rspawn', desc: 'rspawn root'
10
+ class_option :processes, aliases: '-p', type: :numeric, default: nil, desc: 'command'
11
+ class_option :working_dir, type: :string, default: nil, desc: 'command'
12
+ class_option :log_file, type: :string, default: nil, desc: 'command'
13
+ class_option :pid_file, type: :string, default: nil, desc: 'pid_file'
14
+ class_option :timeout, type: :numeric, default: nil, desc: 'timeout'
15
+ class_option :tail, type: :boolean, default: false, desc: 'sync_log'
16
+
17
+ def initialize(args = [], options = {}, config = {})
18
+ super(args, options, config)
19
+ @class_options = config[:shell].base.options
20
+ @config = get_config(args.first)
21
+ end
22
+
23
+ desc "start", "start daemon"
24
+ option :command, aliases: '-c', type: :string, default: nil, desc: 'command'
25
+ def start(key)
26
+ start_action(options['command'], 'start')
27
+ end
28
+
29
+ desc "restart", "restart daemon"
30
+ option :command, aliases: '-c', type: :string, default: nil, desc: 'command'
31
+ def restart(key)
32
+ start_action(options['command'], 'restart')
33
+ end
34
+
35
+ desc "stop", "stop daemon"
36
+ def stop(key)
37
+ option, command = @config.option
38
+ RspawnWorker.spawn!(option, ["stop"])
39
+ end
40
+
41
+ desc "status", "staus check daemon"
42
+ def status(key = nil)
43
+ keys = key.nil? ? Dir.glob(@class_options['root'] + "/*") : [key]
44
+ keys = keys.map { |file| File.basename(file) }
45
+ result = {}
46
+ keys.each do |proc_name|
47
+ option, command = get_config(proc_name).option
48
+ option[:status] = capture_stdout do
49
+ RspawnWorker.spawn!(option, ["status"])
50
+ end
51
+ option[:command] = command
52
+ result[proc_name] = option
53
+ end
54
+ puts JSON.pretty_generate(result)
55
+ end
56
+
57
+ desc 'remove', 'remove'
58
+ def rm(key)
59
+ @config.remove
60
+ end
61
+
62
+ desc 'tail', 'remove'
63
+ def tail(key)
64
+ option, command = @config.option(nil)
65
+ tail_log(option[:log_file])
66
+ end
67
+
68
+ desc 'version', 'show version'
69
+ def version
70
+ puts VERSION
71
+ end
72
+
73
+ private
74
+ def start_action(command, action)
75
+ option, command = @config.option(command)
76
+ @config.save(option, command)
77
+ RspawnWorker.spawn!(option, [action, command])
78
+ tail_log(option[:log_file]) if @class_options['tail']
79
+ end
80
+
81
+ def tail_log(log_file)
82
+ sleep 1
83
+ system("tail -F #{log_file}*")
84
+ rescue Exception => e
85
+ end
86
+
87
+ def get_config(key)
88
+ Config.new(@class_options['root'], key, @class_options)
89
+ end
90
+
91
+ def capture_stdout
92
+ out = StringIO.new
93
+ $stdout = out
94
+ yield
95
+ return out.string
96
+ ensure
97
+ $stdout = STDOUT
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,49 @@
1
+ module Rspawn
2
+ class Config
3
+
4
+ def initialize(root, key, cli_options)
5
+ @root = root
6
+ @key = key
7
+ @cli_options = cli_options
8
+ end
9
+
10
+ def config_path
11
+ "#{@root}/#{@key}/config.yml"
12
+ end
13
+
14
+ def config_dir
15
+ File.dirname(config_path)
16
+ end
17
+
18
+ def load
19
+ File.exist?(config_path) ? JSON.parse(File.read(config_path)) : {}
20
+ end
21
+
22
+ def save(option, command)
23
+ FileUtils.mkdir_p(config_dir)
24
+ output = option.to_h
25
+ output[:command] = command || output[:command]
26
+ File.write(config_path, output.to_json)
27
+ end
28
+
29
+ def remove
30
+ FileUtils.rm_rf(config_dir)
31
+ end
32
+
33
+ def option(command = nil)
34
+ option = {}
35
+ save_option = load
36
+ option[:working_dir] = @cli_options['working_dir'] || save_option['working_dir'] || Dir.pwd
37
+ FileUtils.mkdir_p(option[:working_dir])
38
+ Dir::chdir(option[:working_dir])
39
+
40
+ option[:log_file] = @cli_options['log_file'] || save_option['log_file'] || config_dir + '/' + @key + '.log'
41
+ option[:pid_file] = @cli_options['pid_file'] || save_option['pid_file'] || config_dir + '/' + @key + '.pid'
42
+ option[:timeout] = @cli_options['timeout'] || save_option['timeout'] || 10
43
+ option[:processes] = @cli_options['processes'] || save_option['processes'] || 1
44
+ command = command || save_option['command']
45
+ return option, command
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,12 @@
1
+ module Rspawn
2
+ class Core
3
+
4
+ def initialize
5
+ end
6
+
7
+ def sample
8
+ []
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ module Rspawn
2
+ # rspawn version
3
+ VERSION = "0.3.0"
4
+ end
@@ -0,0 +1,18 @@
1
+ require 'daemon_spawn'
2
+
3
+ module Rspawn
4
+ class RspawnWorker < DaemonSpawn::Base
5
+ def start(args)
6
+ command = args.first
7
+ ENV['RSPAWN_INDEX'] = "#{@index}"
8
+ exec(command)
9
+ rescue Exception => e
10
+ puts "error"
11
+ end
12
+
13
+ def stop
14
+ end
15
+
16
+ end
17
+ end
18
+
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/rspawn/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "rspawn"
7
+ gem.version = Rspawn::VERSION
8
+ gem.summary = %q{very simple daemon.}
9
+ gem.description = %q{very simple daemon.}
10
+ gem.license = "MIT"
11
+ gem.authors = ["Hiroshi Toyama"]
12
+ gem.email = "toyama0919@gmail.com"
13
+ gem.homepage = "https://github.com/toyama0919/rspawn"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_dependency 'thor'
21
+ gem.add_dependency 'daemon-spawn'
22
+
23
+ gem.add_development_dependency 'bundler', '~> 1.7.2'
24
+ gem.add_development_dependency 'pry', '~> 0.10.1'
25
+ gem.add_development_dependency 'rake', '~> 10.3.2'
26
+ gem.add_development_dependency 'rspec', '~> 2.4'
27
+ gem.add_development_dependency 'rubocop', '~> 0.24.1'
28
+ gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
29
+ gem.add_development_dependency 'yard', '~> 0.8'
30
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+ require 'rspawn'
3
+
4
+ describe Rspawn::CLI do
5
+ before do
6
+ end
7
+
8
+ it "should stdout sample" do
9
+ output = capture_stdout do
10
+ Rspawn::CLI.start(['help'])
11
+ end
12
+ output.should_not nil
13
+ end
14
+
15
+ after do
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+ require 'rspawn'
3
+
4
+ describe Rspawn::Core do
5
+ before do
6
+ @core = Core.new
7
+ end
8
+
9
+ it "core not nil" do
10
+ @core.should_not nil
11
+ end
12
+
13
+ after do
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'rspawn'
3
+
4
+ describe Rspawn do
5
+ it "should have a VERSION constant" do
6
+ subject.const_get('VERSION').should_not be_empty
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ require 'rspec'
2
+ require 'rspawn/version'
3
+
4
+ include Rspawn
5
+
6
+ def capture_stdout
7
+ out = StringIO.new
8
+ $stdout = out
9
+ yield
10
+ return out.string
11
+ ensure
12
+ $stdout = STDOUT
13
+ end
14
+
15
+ def capture_stderr
16
+ out = StringIO.new
17
+ $stderr = out
18
+ yield
19
+ return out.string
20
+ ensure
21
+ $stderr = STDERR
22
+ end
23
+
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspawn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiroshi Toyama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: daemon-spawn
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.7.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.7.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 10.3.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 10.3.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.24.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.24.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubygems-tasks
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.2'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: yard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.8'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.8'
139
+ description: very simple daemon.
140
+ email: toyama0919@gmail.com
141
+ executables:
142
+ - rspawn
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".document"
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".travis.yml"
150
+ - ".yardopts"
151
+ - ChangeLog.md
152
+ - Gemfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/rspawn
157
+ - lib/rspawn.rb
158
+ - lib/rspawn/cli.rb
159
+ - lib/rspawn/config.rb
160
+ - lib/rspawn/core.rb
161
+ - lib/rspawn/version.rb
162
+ - lib/rspawn/worker.rb
163
+ - rspawn.gemspec
164
+ - spec/cli_spec.rb
165
+ - spec/core_spec.rb
166
+ - spec/rspawn_spec.rb
167
+ - spec/spec_helper.rb
168
+ homepage: https://github.com/toyama0919/rspawn
169
+ licenses:
170
+ - MIT
171
+ metadata: {}
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ requirements: []
187
+ rubyforge_project:
188
+ rubygems_version: 2.4.5
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: very simple daemon.
192
+ test_files:
193
+ - spec/cli_spec.rb
194
+ - spec/core_spec.rb
195
+ - spec/rspawn_spec.rb
196
+ - spec/spec_helper.rb