app-service 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +3 -0
- data/Gemfile +7 -0
- data/README.adoc +137 -0
- data/README.ja.adoc +137 -0
- data/Rakefile +96 -0
- data/app-service.gemspec +25 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/app/service.rb +180 -0
- data/lib/app/service/version.rb +7 -0
- data/sample/sample1.rb +11 -0
- data/sample/sample2.rb +25 -0
- data/sample/sample3.rb +14 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ecede2dd862dd858ea23141f7337e661433bdab75599e5a0e42fc724af12f9cd
|
4
|
+
data.tar.gz: 3f3206a7c651bb897887f23935688e645f0b44fe4a8001594ab20023d4419978
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bf0ae3ebd9a94f33561582d16aa6c162fc95d95f0f3875b5dcb3c3826d52ce6eeb99db8f4b97670244b21f50bb5fed0554b43ec946003cc4e2c56eff429f529a
|
7
|
+
data.tar.gz: 9ddcbea0783c8b3f7fddf285294f9e7f2255ef72efa655c0897eb44da0c564c27d8bac641cada42ea87a9ad7b33dfee7a5d9996a32cfd9b7a81b4d733d3ea0d2
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
*.bundle
|
11
|
+
*.so
|
12
|
+
*.o
|
13
|
+
*.a
|
14
|
+
mkmf.log
|
15
|
+
/vendor/
|
16
|
+
*.pid
|
17
|
+
*.log
|
18
|
+
*.log.*
|
19
|
+
/var/
|
20
|
+
/log/
|
21
|
+
*.swp
|
22
|
+
/.rspec_status
|
23
|
+
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.adoc
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
= App::Service
|
2
|
+
|
3
|
+
A library for service application daemonize.
|
4
|
+
|
5
|
+
== Features
|
6
|
+
|
7
|
+
* Easy switch from terminal application to background service.
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
|
11
|
+
Add this line to your app's Gemfile:
|
12
|
+
|
13
|
+
[source,ruby]
|
14
|
+
----
|
15
|
+
gem 'app-service'
|
16
|
+
----
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle install
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install app-service
|
25
|
+
or
|
26
|
+
$ gem install -l app-service-x.x.x.gem
|
27
|
+
|
28
|
+
== Usage
|
29
|
+
|
30
|
+
=== Example
|
31
|
+
|
32
|
+
[source,ruby]
|
33
|
+
----
|
34
|
+
require "app/service"
|
35
|
+
|
36
|
+
# execute service application with default settings.
|
37
|
+
App::Service.setup( ARGV.shift )
|
38
|
+
|
39
|
+
# execute service application with custom settings.
|
40
|
+
App::Service.setup( ARGV.shift, **options )
|
41
|
+
----
|
42
|
+
|
43
|
+
[source,bash]
|
44
|
+
----
|
45
|
+
# run on terminal application.
|
46
|
+
ruby sample.rb run
|
47
|
+
|
48
|
+
# start background service.
|
49
|
+
ruby sample.rb start
|
50
|
+
|
51
|
+
# stop background service.
|
52
|
+
ruby sample.rb stop
|
53
|
+
|
54
|
+
# restart background service.
|
55
|
+
ruby sample.rb restart
|
56
|
+
|
57
|
+
# show status of background service.
|
58
|
+
ruby sample.rb status
|
59
|
+
----
|
60
|
+
|
61
|
+
== Reference
|
62
|
+
|
63
|
+
=== Setup App::Service.
|
64
|
+
|
65
|
+
[source,ruby]
|
66
|
+
----
|
67
|
+
App::Service.setup( mode = nil, **options )
|
68
|
+
----
|
69
|
+
|
70
|
+
* Result:
|
71
|
+
** none.
|
72
|
+
|
73
|
+
* Parameter:
|
74
|
+
** mode: Run mode. [run start stop restart status]
|
75
|
+
** argv: Commandline arguments. (default: ARGV)
|
76
|
+
** app_dir: Application working directory. (default: "..")
|
77
|
+
** tmp_dir: Temporary directory. (default: "tmp")
|
78
|
+
** log_dir: Logging directory. (default: "log")
|
79
|
+
** log_sync: Logging sync mode. (default: false)
|
80
|
+
** shift_age: Log file count, or log change event.
|
81
|
+
[0.., "daily", "weekly", "monthly"] (default: 0)
|
82
|
+
** shift_size: Log rotation limit size. (default: 1048576)
|
83
|
+
** log_level: Log level. (default: Logger::Severity::DEBUG)
|
84
|
+
[0:DEBUG, 1:INFO, 2:WARN, 3:ERROR, 4:FATAL, 5:UNKNOWN]
|
85
|
+
** progname: (default: nil)
|
86
|
+
** formatter: (default: Formatter.new)
|
87
|
+
** datetime_format: (default: '%Y-%m-%d %H:%M:%S')
|
88
|
+
** shift_period_suffix: (default: '%Y%m%d')
|
89
|
+
|
90
|
+
=== Shutdown App::Service.
|
91
|
+
|
92
|
+
[source,ruby]
|
93
|
+
----
|
94
|
+
App::Service.shutdown( cond = 0 )
|
95
|
+
----
|
96
|
+
|
97
|
+
* Result:
|
98
|
+
** なし.
|
99
|
+
|
100
|
+
* Parameter:
|
101
|
+
** cond: process exit condition. (default: 0)
|
102
|
+
|
103
|
+
=== Get run mode.
|
104
|
+
|
105
|
+
[source,ruby]
|
106
|
+
----
|
107
|
+
App::Service.mode
|
108
|
+
----
|
109
|
+
|
110
|
+
* Result:
|
111
|
+
** String: Run mode. [run start stop restart status]
|
112
|
+
|
113
|
+
* Parameter:
|
114
|
+
** none.
|
115
|
+
|
116
|
+
=== Get logger instance.
|
117
|
+
|
118
|
+
[source,ruby]
|
119
|
+
----
|
120
|
+
App::Service.logger
|
121
|
+
----
|
122
|
+
|
123
|
+
* Result:
|
124
|
+
** Logger instance.
|
125
|
+
|
126
|
+
* Parameter:
|
127
|
+
** none.
|
128
|
+
|
129
|
+
== Contributing
|
130
|
+
|
131
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/arimay/app-service.
|
132
|
+
|
133
|
+
== License
|
134
|
+
|
135
|
+
The gem is available as open source under the terms of the http://opensource.org/licenses/MIT[MIT License].
|
136
|
+
|
137
|
+
Copyright (c) ARIMA Yasuhiro <arima.yasuhiro@gmail.com>
|
data/README.ja.adoc
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
= App::Service
|
2
|
+
|
3
|
+
サービスアプリケーションをデーモン化するライブラリ。
|
4
|
+
|
5
|
+
== 特徴
|
6
|
+
|
7
|
+
* 端末アプリからバックグラウンドサービスへ容易に切り替える。
|
8
|
+
|
9
|
+
== 導入
|
10
|
+
|
11
|
+
アプリの Gemfile にこの行を追加
|
12
|
+
|
13
|
+
[source,ruby]
|
14
|
+
----
|
15
|
+
gem 'app-service'
|
16
|
+
----
|
17
|
+
|
18
|
+
それから実行
|
19
|
+
|
20
|
+
$ bundle install
|
21
|
+
|
22
|
+
または次のように手動で導入
|
23
|
+
|
24
|
+
$ gem install app-service
|
25
|
+
or
|
26
|
+
$ gem install -l app-service-x.x.x.gem
|
27
|
+
|
28
|
+
== 使い方
|
29
|
+
|
30
|
+
=== 例
|
31
|
+
|
32
|
+
[source,ruby]
|
33
|
+
----
|
34
|
+
require "app/service"
|
35
|
+
|
36
|
+
# デフォルト設定でサービスアプリを実行する.
|
37
|
+
App::Service.setup( ARGV.shift )
|
38
|
+
|
39
|
+
# カスタム設定でサービスアプリを実行する.
|
40
|
+
App::Service.setup( ARGV.shift, **options )
|
41
|
+
----
|
42
|
+
|
43
|
+
[source,bash]
|
44
|
+
----
|
45
|
+
# 端末アプリを実行する.
|
46
|
+
ruby sample.rb run
|
47
|
+
|
48
|
+
# バックグラウンドサービスを開始する.
|
49
|
+
ruby sample.rb start
|
50
|
+
|
51
|
+
# バックグラウンドサービスを停止する.
|
52
|
+
ruby sample.rb stop
|
53
|
+
|
54
|
+
# バックグラウンドサービスを再起動する.
|
55
|
+
ruby sample.rb restart
|
56
|
+
|
57
|
+
# バックグラウンドサービスの状態を表示する.
|
58
|
+
ruby sample.rb status
|
59
|
+
----
|
60
|
+
|
61
|
+
== リファレンス
|
62
|
+
|
63
|
+
=== App::Service を準備する.
|
64
|
+
|
65
|
+
[source,ruby]
|
66
|
+
----
|
67
|
+
App::Service.setup( mode = nil, **options )
|
68
|
+
----
|
69
|
+
|
70
|
+
* Result:
|
71
|
+
** なし.
|
72
|
+
|
73
|
+
* Parameter:
|
74
|
+
** mode: 動作モード [run start stop restart status]
|
75
|
+
** argv: コマンドライン引数. (default: ARGV)
|
76
|
+
** app_dir: アプリ作業ディレクトリ. (default: "..")
|
77
|
+
** tmp_dir: 一時使用ディレクトリ. (default: "tmp")
|
78
|
+
** log_dir: ログ保管ディクトリ. (default: "log")
|
79
|
+
** log_sync: ログ同期モード. (default: false)
|
80
|
+
** shift_age: ログファイルを保持する数か、ログファイルを切り替える頻度.
|
81
|
+
[0.., "daily", "weekly", "monthly"] (default: 0)
|
82
|
+
** shift_size: ログファイルを切り替えるサイズ. (default: 1048576)
|
83
|
+
** log_level: ログレベル. (default: Logger::Severity::DEBUG)
|
84
|
+
[0:DEBUG, 1:INFO, 2:WARN, 3:ERROR, 4:FATAL, 5:UNKNOWN]
|
85
|
+
** progname: (default: nil)
|
86
|
+
** formatter: (default: Formatter.new)
|
87
|
+
** datetime_format: (default: '%Y-%m-%d %H:%M:%S')
|
88
|
+
** shift_period_suffix: (default: '%Y%m%d')
|
89
|
+
|
90
|
+
=== App::Service を終了する.
|
91
|
+
|
92
|
+
[source,ruby]
|
93
|
+
----
|
94
|
+
App::Service.shutdown( cond = 0 )
|
95
|
+
----
|
96
|
+
|
97
|
+
* Result:
|
98
|
+
** none.
|
99
|
+
|
100
|
+
* Parameter:
|
101
|
+
** cond: プロセス終了コード. (default: 0)
|
102
|
+
|
103
|
+
=== 動作モードを取得する.
|
104
|
+
|
105
|
+
[source,ruby]
|
106
|
+
----
|
107
|
+
App::Service.mode
|
108
|
+
----
|
109
|
+
|
110
|
+
* Result:
|
111
|
+
** String: 動作モード [run start stop restart status]
|
112
|
+
|
113
|
+
* Parameter:
|
114
|
+
** なし.
|
115
|
+
|
116
|
+
=== ロガーを取得する.
|
117
|
+
|
118
|
+
[source,ruby]
|
119
|
+
----
|
120
|
+
App::Service.logger
|
121
|
+
----
|
122
|
+
|
123
|
+
* Result:
|
124
|
+
** Loggerインスタンス.
|
125
|
+
|
126
|
+
* Parameter:
|
127
|
+
** なし.
|
128
|
+
|
129
|
+
== 貢献
|
130
|
+
|
131
|
+
不具合報告とプルリクエストは GitHub https://github.com/arimay/app-service まで.
|
132
|
+
|
133
|
+
== ライセンス
|
134
|
+
|
135
|
+
この Gem は、 http://opensource.org/licenses/MIT[MITライセンス] の条件に基づいてオープンソースとして入手できる.
|
136
|
+
|
137
|
+
Copyright (c) ARIMA Yasuhiro <arima.yasuhiro@gmail.com>
|
data/Rakefile
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require "bundler/gem_helper"
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rspec/core/rake_task"
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
class Bundler::GemHelper
|
10
|
+
|
11
|
+
def git_archive( dir = "../zip" )
|
12
|
+
FileUtils.mkdir_p dir
|
13
|
+
dest_path = File.join(dir, "#{name}-#{version}.zip")
|
14
|
+
cmnd = "git archive --format zip --prefix=#{name}/ HEAD > #{dest_path}"
|
15
|
+
|
16
|
+
_, code = sh_with_status( cmnd )
|
17
|
+
raise "Couldn't archive gem," unless code == 0
|
18
|
+
|
19
|
+
Bundler.ui.confirm "#{name} #{version} archived to #{dest_path}."
|
20
|
+
end
|
21
|
+
|
22
|
+
def git_push
|
23
|
+
ver = version.to_s
|
24
|
+
|
25
|
+
cmnd = "git push origin #{ver} "
|
26
|
+
_, code = sh_with_status( cmnd )
|
27
|
+
raise "Couldn't git push origin." unless code == 0
|
28
|
+
|
29
|
+
cmnd = "git push "
|
30
|
+
_, code = sh_with_status( cmnd )
|
31
|
+
raise "Couldn't git push." unless code == 0
|
32
|
+
|
33
|
+
Bundler.ui.confirm "Git Push #{ver}."
|
34
|
+
end
|
35
|
+
|
36
|
+
def update_version( new_version )
|
37
|
+
version_filename = %x[ find . -type f -name "version.rb" | grep -v vendor | head -1 ].chomp
|
38
|
+
version_pathname = File.expand_path( version_filename )
|
39
|
+
lines = File.open( version_pathname ).read
|
40
|
+
lines = lines.gsub( /VERSION\s*=\s*\"\d+\.\d+\.\d+\"/, "VERSION = \"#{new_version}\"" )
|
41
|
+
File.open( version_pathname, "w" ) do |file|
|
42
|
+
file.write( lines )
|
43
|
+
end
|
44
|
+
|
45
|
+
cmnd = "git add #{version_pathname} "
|
46
|
+
_, code = sh_with_status( cmnd )
|
47
|
+
raise "Couldn't git add," unless code == 0
|
48
|
+
|
49
|
+
cmnd = "git commit -m '#{new_version}' "
|
50
|
+
_, code = sh_with_status( cmnd )
|
51
|
+
raise "Couldn't git commit." unless code == 0
|
52
|
+
|
53
|
+
cmnd = "git tag #{new_version} "
|
54
|
+
_, code = sh_with_status( cmnd )
|
55
|
+
raise "Couldn't git tag." unless code == 0
|
56
|
+
|
57
|
+
Bundler.ui.confirm "Update Tags to #{new_version}."
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
Bundler::GemHelper.new(Dir.pwd).instance_eval do
|
63
|
+
|
64
|
+
desc "Archive #{name}-#{version}.zip from repository"
|
65
|
+
task 'zip' do
|
66
|
+
git_archive
|
67
|
+
end
|
68
|
+
|
69
|
+
desc "Git Push"
|
70
|
+
task 'push' do
|
71
|
+
git_push
|
72
|
+
end
|
73
|
+
|
74
|
+
desc "Update Version Tiny"
|
75
|
+
task 'tiny' do
|
76
|
+
major, minor, tiny = version.to_s.split('.')
|
77
|
+
new_version = [major, minor, tiny.to_i + 1].join('.')
|
78
|
+
update_version( new_version )
|
79
|
+
end
|
80
|
+
|
81
|
+
desc "Update Version Minor"
|
82
|
+
task 'minor' do
|
83
|
+
major, minor, _tiny = version.to_s.split('.')
|
84
|
+
new_version = [major, minor.to_i + 1, 0].join('.')
|
85
|
+
update_version( new_version )
|
86
|
+
end
|
87
|
+
|
88
|
+
desc "Update Version Major"
|
89
|
+
task 'major' do
|
90
|
+
major, _minor, _tiny = version.to_s.split('.')
|
91
|
+
new_version = [major.to_i + 1, 0, 0].join('.')
|
92
|
+
update_version( new_version )
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
data/app-service.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/app/service/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "app-service"
|
7
|
+
spec.version = App::Service::VERSION
|
8
|
+
spec.authors = ["ARIMA Yasuhiro"]
|
9
|
+
spec.email = ["arima.yasuhiro@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{ Service application daemonize. }
|
12
|
+
spec.description = %q{ App::Service is a library for service application daemonize. }
|
13
|
+
spec.homepage = "https://github.com/arimay/app-service"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
17
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "app/service"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/app/service.rb
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "service/version"
|
4
|
+
require "fileutils"
|
5
|
+
require "logger"
|
6
|
+
|
7
|
+
module App
|
8
|
+
class Service
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
PROGRAM_DIR = File.expand_path( File.dirname( $PROGRAM_NAME ) )
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def setup( mode = nil, argv: ARGV, app_dir: nil, tmp_dir: nil, log_dir: nil, **options )
|
15
|
+
setup_vars( mode, argv, app_dir, tmp_dir, log_dir, options[:progname] )
|
16
|
+
|
17
|
+
case @@mode
|
18
|
+
when "run"
|
19
|
+
if alive?
|
20
|
+
puts "already in alive."
|
21
|
+
::Kernel.exit
|
22
|
+
end
|
23
|
+
setup_logger( **options )
|
24
|
+
|
25
|
+
when "start"
|
26
|
+
if alive?
|
27
|
+
#puts "already in alive."
|
28
|
+
::Kernel.exit
|
29
|
+
end
|
30
|
+
Process.daemon( true, true )
|
31
|
+
setup_service
|
32
|
+
setup_logger( log_path: @@log_path, **options )
|
33
|
+
|
34
|
+
when "restart"
|
35
|
+
while alive?
|
36
|
+
request_to_stop
|
37
|
+
::Kernel.sleep( 1 )
|
38
|
+
end
|
39
|
+
Process.daemon( true, true )
|
40
|
+
setup_service
|
41
|
+
setup_logger( log_path: @@log_path, **options )
|
42
|
+
|
43
|
+
when "stop"
|
44
|
+
while alive?
|
45
|
+
request_to_stop
|
46
|
+
::Kernel.sleep( 1 )
|
47
|
+
end
|
48
|
+
# puts "successed to stop."
|
49
|
+
::Kernel.exit
|
50
|
+
|
51
|
+
when "status"
|
52
|
+
if alive?
|
53
|
+
puts "in alive."
|
54
|
+
else
|
55
|
+
puts "not in alive."
|
56
|
+
end
|
57
|
+
::Kernel.exit
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def shutdown( cond = 0 )
|
63
|
+
delete_pid_path
|
64
|
+
::Kernel.exit( cond )
|
65
|
+
end
|
66
|
+
|
67
|
+
def logger
|
68
|
+
@@logger
|
69
|
+
end
|
70
|
+
|
71
|
+
def mode
|
72
|
+
@@mode
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def setup_vars( mode, argv, app_dir, tmp_dir, log_dir, progname )
|
78
|
+
if mode.nil?
|
79
|
+
@@mode = argv.dup.shift.to_s.downcase
|
80
|
+
else
|
81
|
+
@@mode = mode.to_s.downcase
|
82
|
+
end
|
83
|
+
|
84
|
+
@@progname = progname || File.basename( $PROGRAM_NAME, '.rb' )
|
85
|
+
unless %w[ run start stop restart status ].include?( @@mode )
|
86
|
+
STDERR.puts <<~USAGE
|
87
|
+
usage: #{@@progname} [run|start|stop|restart|status]
|
88
|
+
USAGE
|
89
|
+
::Kernel.exit( 1 )
|
90
|
+
end
|
91
|
+
|
92
|
+
if ( !app_dir.nil? && app_dir[0] == "/" )
|
93
|
+
@@app_dir = app_dir
|
94
|
+
else
|
95
|
+
@@app_dir = File.expand_path( File.join( PROGRAM_DIR, ( app_dir || ".." ) ) )
|
96
|
+
end
|
97
|
+
Dir.chdir( @@app_dir )
|
98
|
+
|
99
|
+
if ( !tmp_dir.nil? && tmp_dir[0] == "/" )
|
100
|
+
@@tmp_dir = tmp_dir
|
101
|
+
else
|
102
|
+
@@tmp_dir = File.expand_path( File.join( @@app_dir, ( tmp_dir || "tmp" ) ) )
|
103
|
+
end
|
104
|
+
unless Dir.exist?( @@tmp_dir )
|
105
|
+
FileUtils.makedirs( @@tmp_dir )
|
106
|
+
end
|
107
|
+
|
108
|
+
@@pid_path = File.expand_path( File.join( @@tmp_dir, @@progname + ".pid" ) )
|
109
|
+
|
110
|
+
if ( !log_dir.nil? && log_dir[0] == "/" )
|
111
|
+
@@log_dir = log_dir
|
112
|
+
else
|
113
|
+
@@log_dir = File.expand_path( File.join( @@app_dir, ( log_dir || "log" ) ) )
|
114
|
+
end
|
115
|
+
|
116
|
+
unless Dir.exist?( @@log_dir )
|
117
|
+
FileUtils.makedirs( @@log_dir )
|
118
|
+
end
|
119
|
+
|
120
|
+
@@log_path = File.expand_path( File.join( @@log_dir, @@progname + ".log" ) )
|
121
|
+
end
|
122
|
+
|
123
|
+
def setup_service
|
124
|
+
File.open( @@pid_path, "w" ) do |f|
|
125
|
+
f.puts( Process.pid )
|
126
|
+
end
|
127
|
+
$0 = "[#{ @@progname }]"
|
128
|
+
end
|
129
|
+
|
130
|
+
def delete_pid_path
|
131
|
+
if File.exist?( @@pid_path )
|
132
|
+
File.delete( @@pid_path )
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def setup_logger( log_path: nil, shift_age: nil, shift_size: nil, log_sync: nil, **options )
|
137
|
+
sync = ( log_sync.nil? ? true : log_sync )
|
138
|
+
if log_path
|
139
|
+
if shift_age && shift_size
|
140
|
+
@@logger = ::Logger.new( log_path, shift_age, shift_size.to_i, **options )
|
141
|
+
elsif shift_age && shift_size.nil?
|
142
|
+
@@logger = ::Logger.new( log_path, shift_age, **options )
|
143
|
+
else
|
144
|
+
@@logger = ::Logger.new( log_path, **options )
|
145
|
+
end
|
146
|
+
|
147
|
+
dev = @@logger.instance_variable_get(:@logdev).dev
|
148
|
+
$stdout.reopen dev
|
149
|
+
$stdout.sync = sync
|
150
|
+
$stderr.reopen dev
|
151
|
+
$stderr.sync = sync
|
152
|
+
else
|
153
|
+
@@logger = ::Logger.new( STDOUT, **options )
|
154
|
+
STDOUT.sync = sync
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# send INT signal to background process.
|
159
|
+
def request_to_stop
|
160
|
+
pid = File.open( @@pid_path ).read.to_i rescue raise( ArgumentError, "could not load '#{ @@pid_path }'." )
|
161
|
+
Process.kill( :INT, pid ) rescue raise( ArgumentError, "could not send signal." )
|
162
|
+
rescue => e
|
163
|
+
puts e.message
|
164
|
+
end
|
165
|
+
|
166
|
+
def alive?
|
167
|
+
result = true
|
168
|
+
begin
|
169
|
+
pid = File.open( @@pid_path ).read.to_i
|
170
|
+
Process.kill( 0, pid )
|
171
|
+
rescue => e
|
172
|
+
result = false
|
173
|
+
end
|
174
|
+
result
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
data/sample/sample1.rb
ADDED
data/sample/sample2.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "app/service"
|
2
|
+
|
3
|
+
options = {
|
4
|
+
# app_dir: "..",
|
5
|
+
# tmp_dir: "tmp",
|
6
|
+
# log_dir: "log",
|
7
|
+
# log_sync: true, # Boolean
|
8
|
+
|
9
|
+
# shift_age: 0, # 0, Positive, "daily", "weekly", "monthly"
|
10
|
+
# shift_size: 1048576,# Positive
|
11
|
+
# level: Logger::Severity::DEBUG,
|
12
|
+
# 0:DEBUG, 1:INFO, 2:WARN, 3:ERROR, 4:FATAL, 5:UNKNOWN
|
13
|
+
# progname: nil,
|
14
|
+
# formatter: Formatter.new,
|
15
|
+
# datetime_format: '%Y-%m-%d %H:%M:%S',
|
16
|
+
# shift_period_suffix: '%Y%m%d',
|
17
|
+
}
|
18
|
+
|
19
|
+
App::Service.setup( ARGV.shift, **options )
|
20
|
+
|
21
|
+
logger = App::Service.logger
|
22
|
+
logger.info App::Service.mode
|
23
|
+
sleep 1
|
24
|
+
logger.info "quit"
|
25
|
+
|
data/sample/sample3.rb
ADDED
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: app-service
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ARIMA Yasuhiro
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-01-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
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: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: " App::Service is a library for service application daemonize. "
|
42
|
+
email:
|
43
|
+
- arima.yasuhiro@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- Gemfile
|
51
|
+
- README.adoc
|
52
|
+
- README.ja.adoc
|
53
|
+
- Rakefile
|
54
|
+
- app-service.gemspec
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- lib/app/service.rb
|
58
|
+
- lib/app/service/version.rb
|
59
|
+
- sample/sample1.rb
|
60
|
+
- sample/sample2.rb
|
61
|
+
- sample/sample3.rb
|
62
|
+
homepage: https://github.com/arimay/app-service
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubygems_version: 3.2.3
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Service application daemonize.
|
85
|
+
test_files: []
|