dante 0.2.0 → 0.4.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.
data/README.md CHANGED
@@ -10,6 +10,11 @@ can be started via command line or start/stop a daemon, and will store a pid fil
10
10
  If you need to create a ruby executable and you want standard daemon start/stop with pid files
11
11
  and no hassle, this gem will be a great way to get started.
12
12
 
13
+ ## Requirements
14
+
15
+ Dante daemonizes with `fork`, so it needs MRI (CRuby) 2.7 or newer on Unix: Linux, macOS or a BSD. It does not
16
+ run on Windows or JRuby, which have no `fork`.
17
+
13
18
  ## Installation
14
19
 
15
20
  Add to your Gemfile:
@@ -75,21 +80,60 @@ will start the app undaemonized in the terminal and redirect all stdout and stde
75
80
  ./bin/myapp -p 8080 -d -P /var/run/myapp.pid -l /var/log/myapp.log
76
81
  ```
77
82
 
78
- will daemonize and start the process, storing the pid in the specified pid file.
83
+ will daemonize and start the process, storing the pid in the specified pid file. It reports the daemon as
84
+ started once the daemon has kept running for a second, and as not started if it exits before then.
79
85
  All stdout and stderr will be redirected to the specified logfile. If no logfile is specified in daemon mode then all
80
86
  stdout and stderr will be directed to /var/log/<myapp name>.log.
81
87
 
88
+ ```
89
+ sudo install -d -o nobody -g nogroup /var/run/myapp /var/log/myapp
90
+ sudo ./bin/myapp -d -u nobody -G nogroup -P /var/run/myapp/myapp.pid -l /var/log/myapp/myapp.log
91
+ ```
92
+
93
+ will switch to the user nobody before starting, with the group given by `-G`. When started as root, the group
94
+ defaults to the user's primary group, and the process takes on the user's supplementary groups (plus the `-G`
95
+ group, if given). The switch happens before the pid file and the log file are written, so both must be in
96
+ directories that user can write: the defaults, `/var/run/myapp.pid` and `/var/log/myapp.log`, are not, and the
97
+ daemon would fail to start. Keep the pid file and the log in directories that only the daemon's user (or root) can
98
+ write, never in one that other users can write, such as `/tmp`: another user could put a file or a symlink of
99
+ their own at that path. On most Linux systems `/var/run` is emptied at boot, so create its directory at each boot
100
+ (systemd's `RuntimeDirectory=`, below, or a `tmpfiles.d` entry) rather than once.
101
+
82
102
  ```
83
103
  ./bin/myapp -k -P /var/run/myapp.pid
84
104
  ```
85
105
 
86
- will stop all daemonized processes for the specified pid file.
106
+ will stop the daemon whose pid is in the specified pid file: it sends SIGTERM, waits up to 5 seconds for the
107
+ daemon to exit, and then removes the pid file. If the daemon is still running, it says "Failed to kill daemonized
108
+ process" and keeps the pid file.
109
+
110
+ ```
111
+ ./bin/myapp -k -f -P /var/run/myapp.pid
112
+ ```
113
+
114
+ will do the same, and if the daemon survives SIGTERM, send it SIGKILL (`-f`/`--force`, or `:force => true`) and
115
+ wait up to 5 more seconds.
87
116
 
88
117
  ```
89
118
  ./bin/myapp --help
90
119
  ```
91
120
 
92
- Will return a useful help banner message explaining the simple usage.
121
+ Will return a useful help banner message explaining the simple usage. These are all the built-in options:
122
+
123
+ ```
124
+ -p, --port PORT Specify port
125
+ (default: )
126
+ -P, --pid FILE save PID in FILE when using -d option.
127
+ (default: /var/run/myapp.pid)
128
+ -d, --daemon Daemonize mode
129
+ -l, --log FILE Logfile for output
130
+ (default: /var/log/myapp.log)
131
+ -k, --kill [PORT] Stop the daemon in the pid file (PORT is ignored)
132
+ -u, --user USER User to run as
133
+ -G, --group GROUP Group to run as
134
+ -f, --force Force kill if SIGTERM fails
135
+ -?, --help Display this usage information.
136
+ ```
93
137
 
94
138
  ### Advanced
95
139
 
@@ -149,6 +193,83 @@ Dante::Runner.new('gitdocs').execute(:daemonize => true, :restart => true, :pid_
149
193
 
150
194
  so you can use dante as part of a more complex CLI executable.
151
195
 
196
+ ### Options from somewhere other than ARGV
197
+
198
+ `execute` parses the command-line options in `ARGV`. Inside a program that has options of its own (a test runner,
199
+ say, or a Rake task), pass dante the options to parse with `:argv` instead, to `Dante::Runner.new`, `Dante.run`
200
+ or `execute`:
201
+
202
+ ```ruby
203
+ # parses only these, and leaves ARGV alone
204
+ Dante::Runner.new('gitdocs', :argv => ['-d', '-P', @pid]).execute { something! }
205
+ # parses nothing
206
+ Dante::Runner.new('gitdocs').execute(:argv => [], :daemonize => true, :pid_path => @pid) { something! }
207
+ ```
208
+
209
+ Without `:argv`, ARGV is parsed as before, and the options dante knows are removed from it. The array given as
210
+ `:argv` is never changed.
211
+
212
+ ## systemd
213
+
214
+ Under systemd, let systemd run the process in the foreground (`Type=simple`), as the user it names, with the
215
+ output going to the journal:
216
+
217
+ ```ini
218
+ # /etc/systemd/system/myapp.service
219
+ [Unit]
220
+ Description=myapp
221
+ After=network.target
222
+
223
+ [Service]
224
+ Type=simple
225
+ User=myapp
226
+ Group=myapp
227
+ ExecStart=/path/to/myapp/bin/myapp -p 8080
228
+ Restart=on-failure
229
+
230
+ [Install]
231
+ WantedBy=multi-user.target
232
+ ```
233
+
234
+ systemd stops it with SIGTERM, which dante handles. To have dante daemonize instead, use `Type=forking` with the
235
+ pid file and the log in directories systemd creates for that user:
236
+
237
+ ```ini
238
+ [Service]
239
+ Type=forking
240
+ User=myapp
241
+ Group=myapp
242
+ RuntimeDirectory=myapp
243
+ LogsDirectory=myapp
244
+ PIDFile=/run/myapp/myapp.pid
245
+ ExecStart=/path/to/myapp/bin/myapp -d -p 8080 -P /run/myapp/myapp.pid -l /var/log/myapp/myapp.log
246
+ Restart=on-failure
247
+ ```
248
+
249
+ Use `User=` and `Group=` rather than `-u` and `-G` under systemd, so that the process starts as that user and the
250
+ directories systemd creates belong to it.
251
+
252
+ ## Log rotation
253
+
254
+ The daemon opens its log file once, for appending, and has no SIGHUP handler: SIGHUP stops it. So rotate the log
255
+ with logrotate's `copytruncate`, rather than a `postrotate` script that sends SIGHUP:
256
+
257
+ ```
258
+ # /etc/logrotate.d/myapp
259
+ /var/log/myapp/myapp.log {
260
+ weekly
261
+ rotate 4
262
+ compress
263
+ delaycompress
264
+ missingok
265
+ notifempty
266
+ copytruncate
267
+ }
268
+ ```
269
+
270
+ `copytruncate` copies the log and empties it in place, and the daemon carries on writing at the start of the
271
+ emptied file. Lines written between the copy and the truncation are lost.
272
+
152
273
  ## God
153
274
 
154
275
  Dante can be used well in conjunction with the excellent God process manager. Simply, use Dante to daemonize a process
@@ -181,4 +302,4 @@ and that's all. Of course now you can also easily daemonize as well as start/sto
181
302
 
182
303
  ## Copyright
183
304
 
184
- Copyright © 2011 Nathan Esquenazi. See [LICENSE](https://github.com/bazaarlabs/dante/blob/master/LICENSE) for details.
305
+ Copyright © 2011-2026 Nathan Esquenazi. See [LICENSE](https://github.com/nesquena/dante/blob/master/LICENSE) for details.
data/Rakefile CHANGED
@@ -2,14 +2,22 @@ require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
  # require 'yard'
4
4
 
5
- task :test do
6
- Rake::TestTask.new do |t|
7
- t.libs.push "lib"
8
- t.test_files = FileList[File.expand_path('../test/**/*_test.rb', __FILE__)]
9
- t.verbose = true
10
- end
5
+ TEST_FILES = FileList[File.expand_path('../test/**/*_test.rb', __FILE__)]
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs.push "lib"
9
+ t.test_files = TEST_FILES
10
+ t.verbose = true
11
+ end
12
+
13
+ # Rake::TestTask treats an empty file list as a passing run of nothing.
14
+ task :test_files_present do
15
+ abort "No test files match test/**/*_test.rb" if TEST_FILES.empty?
11
16
  end
17
+ task :test => :test_files_present
18
+
19
+ task :default => :test
12
20
 
13
21
  # task :doc do
14
22
  # YARD::CLI::Yardoc.new.run
15
- # end
23
+ # end
data/dante.gemspec CHANGED
@@ -7,18 +7,23 @@ Gem::Specification.new do |s|
7
7
  s.version = Dante::VERSION
8
8
  s.authors = ["Nathan Esquenazi"]
9
9
  s.email = ["nesquena@gmail.com"]
10
- s.homepage = "https://github.com/bazaarlabs/dante"
10
+ s.homepage = "https://github.com/nesquena/dante"
11
11
  s.summary = %q{Turn any process into a demon}
12
12
  s.description = %q{Turn any process into a demon.}
13
-
14
- s.rubyforge_project = "dante"
13
+ s.license = "MIT"
14
+ s.metadata = {
15
+ "source_code_uri" => "https://github.com/nesquena/dante",
16
+ "changelog_uri" => "https://github.com/nesquena/dante/blob/master/CHANGELOG.md",
17
+ "bug_tracker_uri" => "https://github.com/nesquena/dante/issues",
18
+ "rubygems_mfa_required" => "true"
19
+ }
15
20
 
16
21
  s.files = `git ls-files`.split("\n")
17
22
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
23
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
24
  s.require_paths = ["lib"]
25
+ s.required_ruby_version = '>= 2.7'
20
26
 
21
27
  s.add_development_dependency 'rake'
22
- s.add_development_dependency 'minitest'
23
- s.add_development_dependency 'mocha'
28
+ s.add_development_dependency 'minitest', '>= 5.0'
24
29
  end