daemon_controller 2.0.0 → 3.0.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 +4 -4
- data/README.md +77 -75
- data/lib/daemon_controller/version.rb +1 -1
- data/lib/daemon_controller.rb +270 -374
- data/spec/daemon_controller_spec.rb +341 -227
- data/spec/echo_server.rb +9 -1
- data/spec/test_helper.rb +66 -20
- data/spec/unresponsive_daemon.rb +6 -12
- metadata +2 -3
- data/spec/run_echo_server +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd4f2d08b82c7340e6b00c8a97e63f66a6677fbc28e9b705ecb6c53d75701e00
|
4
|
+
data.tar.gz: 11bc6d68d7837db49c123b5e2b08fc932bf60c1b0c29742e7e891e66d8f4f82a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c29f7ea6b92d607774bef4f42dba2e03215f85078079fb3febd404cf87efff8f0536e03ba870234c72925f7404142dba2bf73217d7586175328d6611ba72e3d8
|
7
|
+
data.tar.gz: 1b5d67e7165fb4c2ba1c667c774e089a5a5029e57ba47dd1f91cbcd17e2dcdef7136947b0d115d71b7db9c154013043f9da01428d55659898eee198bd1ca3d3a
|
data/README.md
CHANGED
@@ -31,12 +31,6 @@ It provides the following functionality:
|
|
31
31
|
gem install daemon_controller
|
32
32
|
```
|
33
33
|
|
34
|
-
## Resources
|
35
|
-
|
36
|
-
* [Website](https://github.com/FooBarWidget/daemon_controller)
|
37
|
-
* [RDoc](http://rdoc.info/projects/FooBarWidget/daemon_controller)
|
38
|
-
* [Git repository](git://github.com/FooBarWidget/daemon_controller.git)
|
39
|
-
|
40
34
|
|
41
35
|
What is it for?
|
42
36
|
===============
|
@@ -87,13 +81,15 @@ daemon varies. We've observed that waiting a fixed amount of time is by far the
|
|
87
81
|
most common way. For example, UltraSphinx's daemon starting code looks like
|
88
82
|
this:
|
89
83
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
84
|
+
```ruby
|
85
|
+
system "searchd --config '#{Ultrasphinx::CONF_PATH}'"
|
86
|
+
sleep(4) # give daemon a chance to write the pid file
|
87
|
+
if ultrasphinx_daemon_running?
|
88
|
+
say "started successfully"
|
89
|
+
else
|
90
|
+
say "failed to start"
|
91
|
+
end
|
92
|
+
```
|
97
93
|
|
98
94
|
This is in no way a slam against UltraSphinx. However, if the daemon starts in
|
99
95
|
200 miliseconds, then the user who issued the start command will be waiting for
|
@@ -291,7 +287,7 @@ without having to start the daemons manually.
|
|
291
287
|
Tutorial #1: controlling Apache
|
292
288
|
===============================
|
293
289
|
|
294
|
-
Suppose that you're a [Phusion Passenger](
|
290
|
+
Suppose that you're a [Phusion Passenger](https://www.phusionpasseenger.com/) developer,
|
295
291
|
and you need to write tests for the Apache module. In particular, you want to
|
296
292
|
test whether the different Phusion Passenger configuration directives are
|
297
293
|
working as expected. Obviously, to test the Apache module, the Apache web
|
@@ -306,29 +302,30 @@ server must be running. For every test, you will want the unit test suite to:
|
|
306
302
|
|
307
303
|
That can be done with the following code:
|
308
304
|
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
305
|
+
```ruby
|
306
|
+
require "daemon_controller"
|
307
|
+
|
308
|
+
File.open("apache.conf", "w") do |f|
|
309
|
+
f.write("PidFile apache.pid\n")
|
310
|
+
f.write("LogFile apache.log\n")
|
311
|
+
f.write("Listen 1234\n")
|
312
|
+
f.write(... other relevant configuration options ...)
|
313
|
+
end
|
314
|
+
|
315
|
+
controller = DaemonController.new(
|
316
|
+
identifier: "Apache web server",
|
317
|
+
start_command: "apachectl -f apache.conf -k start",
|
318
|
+
ping_command: [:tcp, "localhost", 1234],
|
319
|
+
pid_file: "apache.pid",
|
320
|
+
log_file: "apache.log"
|
321
|
+
)
|
322
|
+
controller.start
|
323
|
+
|
324
|
+
# .... apache is now started ....
|
325
|
+
# .... some test code here ....
|
326
|
+
|
327
|
+
controller.stop
|
328
|
+
```
|
332
329
|
|
333
330
|
The `File.open` line is obvious: it writes the relevant Apache configuration
|
334
331
|
file.
|
@@ -336,8 +333,8 @@ file.
|
|
336
333
|
The next line is for creating a new DaemonController object. We pass a
|
337
334
|
human-readable identifier for this daemon ("Apache web server") to the
|
338
335
|
constructor. This is used for generating friendlier error messages.
|
339
|
-
We also tell it how Apache is supposed to be started (
|
340
|
-
check whether it can be connected to (
|
336
|
+
We also tell it how Apache is supposed to be started (`start_command:`), how to
|
337
|
+
check whether it can be connected to (`ping_command:`), and where its PID file
|
341
338
|
and log file is. If Apache failed with an error during startup, then it will be
|
342
339
|
reported. If Apache failed with an error after it has gone into the background,
|
343
340
|
then that will be reported too: the given log file is monitored for new error
|
@@ -345,7 +342,8 @@ messages.
|
|
345
342
|
Finally, a timeout of 25 seconds is given. If Apache doesn't start within 25
|
346
343
|
seconds, then an exception will be raised.
|
347
344
|
|
348
|
-
The ping command
|
345
|
+
The ping command specifies which socket to connect to in order to check whether
|
346
|
+
the daemon is ready. It can also be a `Proc` which returns true or false. If the Proc
|
349
347
|
raises `Errno::ECONNREFUSED`, then that's also interpreted by DaemonController
|
350
348
|
as meaning that the daemon isn't responding yet.
|
351
349
|
|
@@ -392,41 +390,43 @@ isn't running.
|
|
392
390
|
|
393
391
|
This can be achieved with the following code:
|
394
392
|
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
send_query(socket, search_terms)
|
415
|
-
return retrieve_results(socket)
|
416
|
-
end
|
417
|
-
|
418
|
-
private
|
419
|
-
def before_start
|
420
|
-
generate_configuration_file
|
421
|
-
if !index_exists?
|
422
|
-
generate_index
|
423
|
-
end
|
424
|
-
end
|
425
|
-
|
426
|
-
...
|
393
|
+
```ruby
|
394
|
+
require "daemon_controller"
|
395
|
+
|
396
|
+
class SearchServer
|
397
|
+
SEARCH_SERVER_PORT = 1234
|
398
|
+
|
399
|
+
def initialize
|
400
|
+
@controller = DaemonController.new(
|
401
|
+
identifier: "Sphinx search server",
|
402
|
+
start_command: "searchd -c config/sphinx.conf",
|
403
|
+
before_start: method(:before_start),
|
404
|
+
ping_command: [:tcp, "localhost", SEARCH_SERVER_PORT],
|
405
|
+
pid_file: "tmp/pids/sphinx.pid",
|
406
|
+
log_file: "log/sphinx.log")
|
407
|
+
end
|
408
|
+
|
409
|
+
def query(search_terms)
|
410
|
+
socket = @controller.connect do
|
411
|
+
TCPSocket.new("localhost", SEARCH_SERVER_PORT)
|
427
412
|
end
|
413
|
+
send_query(socket, search_terms)
|
414
|
+
retrieve_results(socket)
|
415
|
+
end
|
416
|
+
|
417
|
+
private
|
418
|
+
def before_start
|
419
|
+
generate_configuration_file
|
420
|
+
if !index_exists?
|
421
|
+
generate_index
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
# ...
|
426
|
+
end
|
427
|
+
```
|
428
428
|
|
429
|
-
Notice the
|
429
|
+
Notice the `before_start:` option. We pass a block of code which is to be run,
|
430
430
|
just before the daemon is started. This block, along with starting the daemon,
|
431
431
|
is completely serialized. That is, if you're inside the block, then it's
|
432
432
|
guaranteed that no other process is running this block at the same time as well.
|
@@ -498,5 +498,7 @@ synchronization. This has a few implications:
|
|
498
498
|
API documentation
|
499
499
|
=================
|
500
500
|
|
501
|
-
Detailed API documentation is available
|
502
|
-
|
501
|
+
Detailed API documentation is available here:
|
502
|
+
- [Configuration options](doc/OPTIONS.md)
|
503
|
+
- [Stop flow](doc/STOP_FLOW.md)
|
504
|
+
- Inline comments in `lib/daemon_controller.rb`.
|