daemon_controller 1.2.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 +5 -13
- data/LICENSE.txt +1 -2
- data/{README.markdown → README.md} +108 -142
- data/Rakefile +16 -574
- data/daemon_controller.gemspec +5 -4
- data/lib/daemon_controller/lock_file.rb +106 -105
- data/lib/daemon_controller/packaging.rb +10 -16
- data/lib/daemon_controller/spawn.rb +8 -7
- data/lib/daemon_controller/version.rb +10 -8
- data/lib/daemon_controller.rb +837 -851
- data/spec/daemon_controller_spec.rb +686 -409
- data/spec/echo_server.rb +147 -115
- data/spec/test_helper.rb +178 -105
- data/spec/unresponsive_daemon.rb +9 -9
- metadata +7 -21
- checksums.yaml.gz.asc +0 -12
- data/debian.template/changelog +0 -5
- data/debian.template/compat +0 -1
- data/debian.template/control.template +0 -15
- data/debian.template/copyright +0 -24
- data/debian.template/ruby-daemon-controller.install +0 -2
- data/debian.template/rules +0 -17
- data/debian.template/source/format +0 -1
- data/rpm/get_distro_id.py +0 -4
- data/rpm/rubygem-daemon_controller.spec.template +0 -102
- data/spec/run_echo_server +0 -9
- data.tar.gz.asc +0 -12
- metadata.gz.asc +0 -12
data/lib/daemon_controller.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# daemon_controller, library for robust daemon management
|
2
|
-
# Copyright (c) 2010-
|
3
|
-
#
|
4
|
+
# Copyright (c) 2010-2025 Asynchronous B.V.
|
5
|
+
#
|
4
6
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
7
|
# of this software and associated documentation files (the "Software"), to deal
|
6
8
|
# in the Software without restriction, including without limitation the rights
|
7
9
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
10
|
# copies of the Software, and to permit persons to whom the Software is
|
9
11
|
# furnished to do so, subject to the following conditions:
|
10
|
-
#
|
12
|
+
#
|
11
13
|
# The above copyright notice and this permission notice shall be included in
|
12
14
|
# all copies or substantial portions of the Software.
|
13
|
-
#
|
15
|
+
#
|
14
16
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
17
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
18
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
@@ -19,855 +21,839 @@
|
|
19
21
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
22
|
# THE SOFTWARE.
|
21
23
|
|
22
|
-
require
|
23
|
-
require
|
24
|
-
require
|
25
|
-
require
|
26
|
-
|
27
|
-
|
28
|
-
end
|
24
|
+
require "tempfile"
|
25
|
+
require "fcntl"
|
26
|
+
require "socket"
|
27
|
+
require "pathname"
|
28
|
+
require "timeout"
|
29
|
+
require "rbconfig"
|
29
30
|
|
30
|
-
|
31
|
+
require_relative "daemon_controller/lock_file"
|
31
32
|
|
32
33
|
# Main daemon controller object. See the README for an introduction and tutorial.
|
33
34
|
class DaemonController
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
end
|
859
|
-
else
|
860
|
-
# On Ruby 1.9, Thread#kill (which is called by timeout.rb) may
|
861
|
-
# not be able to interrupt Process.waitpid. So here we use a
|
862
|
-
# special version that's a bit less efficient but is at least
|
863
|
-
# interruptable.
|
864
|
-
def interruptable_waitpid(pid)
|
865
|
-
result = nil
|
866
|
-
while !result
|
867
|
-
result = Process.waitpid(pid, Process::WNOHANG)
|
868
|
-
sleep 0.01 if !result
|
869
|
-
end
|
870
|
-
return result
|
871
|
-
end
|
872
|
-
end
|
35
|
+
ALLOWED_CONNECT_EXCEPTIONS = [Errno::ECONNREFUSED, Errno::ENETUNREACH,
|
36
|
+
Errno::ETIMEDOUT, Errno::ECONNRESET, Errno::EINVAL,
|
37
|
+
Errno::EADDRNOTAVAIL]
|
38
|
+
|
39
|
+
SPAWNER_FILE = File.absolute_path(File.join(File.dirname(__FILE__),
|
40
|
+
"daemon_controller", "spawn.rb"))
|
41
|
+
|
42
|
+
class Error < StandardError
|
43
|
+
end
|
44
|
+
|
45
|
+
class TimeoutError < Error
|
46
|
+
end
|
47
|
+
|
48
|
+
class AlreadyStarted < Error
|
49
|
+
end
|
50
|
+
|
51
|
+
class StartError < Error
|
52
|
+
end
|
53
|
+
|
54
|
+
class StartTimeout < TimeoutError
|
55
|
+
end
|
56
|
+
|
57
|
+
class StopError < Error
|
58
|
+
end
|
59
|
+
|
60
|
+
class StopTimeout < TimeoutError
|
61
|
+
end
|
62
|
+
|
63
|
+
class ConnectError < Error
|
64
|
+
end
|
65
|
+
|
66
|
+
InternalCommandOkResult = Struct.new(:pid, :output)
|
67
|
+
InternalCommandErrorResult = Struct.new(:pid, :output, :exit_status)
|
68
|
+
InternalCommandTimeoutResult = Struct.new(:pid, :output)
|
69
|
+
|
70
|
+
# Create a new DaemonController object.
|
71
|
+
#
|
72
|
+
# See doc/OPTIONS.md for options docs.
|
73
|
+
def initialize(identifier:, start_command:, ping_command:, pid_file:, log_file:,
|
74
|
+
lock_file: nil, stop_command: nil, restart_command: nil, before_start: nil,
|
75
|
+
start_timeout: 30, start_abort_timeout: 10, stop_timeout: 30,
|
76
|
+
log_file_activity_timeout: 10, ping_interval: 0.1, stop_graceful_signal: "TERM", dont_stop_if_pid_file_invalid: false,
|
77
|
+
daemonize_for_me: false, keep_ios: nil, env: nil, logger: nil)
|
78
|
+
@identifier = identifier
|
79
|
+
@start_command = start_command
|
80
|
+
@ping_command = ping_command
|
81
|
+
@pid_file = pid_file
|
82
|
+
@log_file = log_file
|
83
|
+
|
84
|
+
@lock_file = determine_lock_file(lock_file, identifier, pid_file)
|
85
|
+
@stop_command = stop_command
|
86
|
+
@restart_command = restart_command
|
87
|
+
@before_start = before_start
|
88
|
+
@start_timeout = start_timeout
|
89
|
+
@start_abort_timeout = start_abort_timeout
|
90
|
+
@stop_timeout = stop_timeout
|
91
|
+
@log_file_activity_timeout = log_file_activity_timeout
|
92
|
+
@ping_interval = ping_interval
|
93
|
+
@stop_graceful_signal = stop_graceful_signal
|
94
|
+
@dont_stop_if_pid_file_invalid = dont_stop_if_pid_file_invalid
|
95
|
+
@daemonize_for_me = daemonize_for_me
|
96
|
+
@keep_ios = keep_ios
|
97
|
+
@env = env
|
98
|
+
@logger = logger
|
99
|
+
end
|
100
|
+
|
101
|
+
# Start the daemon and wait until it can be pinged.
|
102
|
+
#
|
103
|
+
# Raises:
|
104
|
+
# - AlreadyStarted - the daemon is already running.
|
105
|
+
# - StartError - the start command failed.
|
106
|
+
# - StartTimeout - the daemon did not start in time. This could also
|
107
|
+
# mean that the daemon failed after it has gone into the background.
|
108
|
+
def start
|
109
|
+
@lock_file.exclusive_lock do
|
110
|
+
start_without_locking
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Connect to the daemon by running the given block, which contains the
|
115
|
+
# connection logic. If the daemon isn't already running, then it will be
|
116
|
+
# started.
|
117
|
+
#
|
118
|
+
# The block must return nil or raise Errno::ECONNREFUSED, Errno::ENETUNREACH,
|
119
|
+
# Errno::ETIMEDOUT, Errno::ECONNRESET, Errno::EINVAL and Errno::EADDRNOTAVAIL
|
120
|
+
# to indicate that the daemon cannot be
|
121
|
+
# connected to. It must return non-nil if the daemon can be connected to.
|
122
|
+
# Upon successful connection, the return value of the block will
|
123
|
+
# be returned by #connect.
|
124
|
+
#
|
125
|
+
# Note that the block may be called multiple times.
|
126
|
+
#
|
127
|
+
# Raises:
|
128
|
+
# - StartError - an attempt to start the daemon was made, but the start
|
129
|
+
# command failed with an error.
|
130
|
+
# - StartTimeout - an attempt to start the daemon was made, but the daemon
|
131
|
+
# did not start in time, or it failed after it has gone into the background.
|
132
|
+
# - ConnectError - the daemon wasn't already running, but we couldn't connect
|
133
|
+
# to the daemon even after starting it.
|
134
|
+
def connect
|
135
|
+
connection = nil
|
136
|
+
@lock_file.shared_lock do
|
137
|
+
connection = yield
|
138
|
+
rescue *ALLOWED_CONNECT_EXCEPTIONS
|
139
|
+
connection = nil
|
140
|
+
end
|
141
|
+
if connection.nil?
|
142
|
+
@lock_file.exclusive_lock do
|
143
|
+
if !daemon_is_running?
|
144
|
+
start_without_locking
|
145
|
+
end
|
146
|
+
connect_exception = nil
|
147
|
+
begin
|
148
|
+
connection = yield
|
149
|
+
rescue *ALLOWED_CONNECT_EXCEPTIONS => e
|
150
|
+
connection = nil
|
151
|
+
connect_exception = e
|
152
|
+
end
|
153
|
+
if connection.nil?
|
154
|
+
# Daemon is running but we couldn't connect to it. Possible
|
155
|
+
# reasons:
|
156
|
+
# - The daemon froze.
|
157
|
+
# - Bizarre security restrictions.
|
158
|
+
# - There's a bug in the yielded code.
|
159
|
+
if connect_exception
|
160
|
+
raise ConnectError, "Cannot connect to the daemon: #{connect_exception} (#{connect_exception.class})"
|
161
|
+
else
|
162
|
+
raise ConnectError, "Cannot connect to the daemon"
|
163
|
+
end
|
164
|
+
else
|
165
|
+
connection
|
166
|
+
end
|
167
|
+
end
|
168
|
+
else
|
169
|
+
connection
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# Stop the daemon and wait until it has exited.
|
174
|
+
#
|
175
|
+
# Raises:
|
176
|
+
# - StopError - the stop command failed.
|
177
|
+
# - StopTimeout - the daemon didn't stop in time.
|
178
|
+
def stop
|
179
|
+
@lock_file.exclusive_lock do
|
180
|
+
timeoutable(@stop_timeout) do
|
181
|
+
allow_timeout do
|
182
|
+
kill_daemon
|
183
|
+
wait_until { !daemon_is_running? }
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
rescue Timeout::Error
|
188
|
+
kill_daemon_with_signal(force: true)
|
189
|
+
wait_until { !daemon_is_running? }
|
190
|
+
raise StopTimeout, "Daemon '#{@identifier}' did not exit in time (force killed)"
|
191
|
+
end
|
192
|
+
|
193
|
+
# Restarts the daemon. Uses the restart_command if provided, otherwise
|
194
|
+
# calls #stop and #start.
|
195
|
+
def restart
|
196
|
+
if @restart_command
|
197
|
+
run_command(@restart_command)
|
198
|
+
else
|
199
|
+
stop
|
200
|
+
start
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Returns the daemon's PID, as reported by its PID file. Returns the PID
|
205
|
+
# as an integer, or nil there is no valid PID in the PID file.
|
206
|
+
#
|
207
|
+
# This method doesn't check whether the daemon's actually running.
|
208
|
+
# Use #running? if you want to check whether it's actually running.
|
209
|
+
#
|
210
|
+
# Raises SystemCallError or IOError if something went wrong during
|
211
|
+
# reading of the PID file.
|
212
|
+
def pid
|
213
|
+
@lock_file.shared_lock do
|
214
|
+
read_pid_file
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
# Checks whether the daemon is still running. This is done by reading
|
219
|
+
# the PID file and then checking whether there is a process with that
|
220
|
+
# PID.
|
221
|
+
#
|
222
|
+
# Raises SystemCallError or IOError if something went wrong during
|
223
|
+
# reading of the PID file.
|
224
|
+
def running?
|
225
|
+
@lock_file.shared_lock do
|
226
|
+
daemon_is_running?
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
# Checks whether ping Unix domain sockets is supported. Currently
|
231
|
+
# this is supported on all Ruby implementations, except JRuby.
|
232
|
+
def self.can_ping_unix_sockets?
|
233
|
+
RUBY_PLATFORM != "java"
|
234
|
+
end
|
235
|
+
|
236
|
+
private
|
237
|
+
|
238
|
+
def start_without_locking
|
239
|
+
raise AlreadyStarted, "Daemon '#{@identifier}' is already started" if daemon_is_running?
|
240
|
+
|
241
|
+
save_log_file_information
|
242
|
+
delete_pid_file
|
243
|
+
spawn_result = nil
|
244
|
+
|
245
|
+
begin
|
246
|
+
_, remaining_time = timeoutable(@start_timeout) do
|
247
|
+
allow_timeout { before_start }
|
248
|
+
spawn_result = allow_timeout { spawn_daemon }
|
249
|
+
daemon_spawned
|
250
|
+
record_activity
|
251
|
+
|
252
|
+
if spawn_result.is_a?(InternalCommandOkResult)
|
253
|
+
allow_timeout do
|
254
|
+
# We wait until the PID file is available and until
|
255
|
+
# the daemon responds to pings, but we wait no longer
|
256
|
+
# than @start_timeout seconds in total (including daemon
|
257
|
+
# spawn time).
|
258
|
+
# Furthermore, if the log file hasn't changed for
|
259
|
+
# @log_file_activity_timeout seconds, and the PID file
|
260
|
+
# still isn't available or the daemon still doesn't
|
261
|
+
# respond to pings, then assume that the daemon has
|
262
|
+
# terminated with an error.
|
263
|
+
wait_until do
|
264
|
+
if log_file_has_changed?
|
265
|
+
record_activity
|
266
|
+
elsif no_activity?(@log_file_activity_timeout)
|
267
|
+
raise Timeout::Error, "Log file inactivity"
|
268
|
+
end
|
269
|
+
pid_file_available?
|
270
|
+
end
|
271
|
+
wait_until(sleep_interval: @ping_interval) do
|
272
|
+
if log_file_has_changed?
|
273
|
+
record_activity
|
274
|
+
elsif no_activity?(@log_file_activity_timeout)
|
275
|
+
raise Timeout::Error, "Log file inactivity"
|
276
|
+
end
|
277
|
+
run_ping_command || !daemon_is_running?
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
spawn_result
|
283
|
+
end
|
284
|
+
rescue Timeout::Error
|
285
|
+
# If we got here then it means either the #before_start timed out (= no PID),
|
286
|
+
# or the code after #spawn_daemon timed out (already daemonized, so use PID file).
|
287
|
+
# #spawn_daemon itself won't trigger Timeout:Error because that's handled as
|
288
|
+
# InternalCommandTimeoutResult.
|
289
|
+
pid = spawn_result ? read_pid_file : nil
|
290
|
+
start_timed_out(pid)
|
291
|
+
debug "Timeout waiting for daemon to be ready, PID #{pid.inspect}"
|
292
|
+
abort_start(pid: pid, is_direct_child: false) if pid
|
293
|
+
raise StartTimeout, concat_spawn_output_and_logs(spawn_result ? spawn_result.output : nil,
|
294
|
+
differences_in_log_file, nil, "timed out")
|
295
|
+
end
|
296
|
+
|
297
|
+
case spawn_result
|
298
|
+
when InternalCommandOkResult
|
299
|
+
success, _ = timeoutable(remaining_time) { allow_timeout { run_ping_command } }
|
300
|
+
if success
|
301
|
+
true
|
302
|
+
else
|
303
|
+
raise StartError, concat_spawn_output_and_logs(spawn_result.output, differences_in_log_file)
|
304
|
+
end
|
305
|
+
|
306
|
+
when InternalCommandErrorResult
|
307
|
+
raise StartError, concat_spawn_output_and_logs(spawn_result.output,
|
308
|
+
differences_in_log_file, spawn_result.exit_status)
|
309
|
+
|
310
|
+
when InternalCommandTimeoutResult
|
311
|
+
daemonization_timed_out(spawn_result.pid)
|
312
|
+
abort_start(pid: spawn_result.pid, is_direct_child: true)
|
313
|
+
debug "Timeout waiting for daemon to fork, PID #{spawn_result.pid}"
|
314
|
+
raise StartTimeout, concat_spawn_output_and_logs(spawn_result.output,
|
315
|
+
differences_in_log_file, nil, "timed out")
|
316
|
+
|
317
|
+
else
|
318
|
+
raise "Bug: unexpected result from #spawn_daemon: #{spawn_result.inspect}"
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
def before_start
|
323
|
+
if @before_start
|
324
|
+
@before_start.call
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
def spawn_daemon
|
329
|
+
if @start_command.respond_to?(:call)
|
330
|
+
run_command(@start_command.call)
|
331
|
+
else
|
332
|
+
run_command(@start_command)
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
def kill_daemon
|
337
|
+
if @stop_command
|
338
|
+
if @dont_stop_if_pid_file_invalid && read_pid_file.nil?
|
339
|
+
return
|
340
|
+
end
|
341
|
+
|
342
|
+
result = run_command(@stop_command)
|
343
|
+
case result
|
344
|
+
when InternalCommandOkResult
|
345
|
+
# Success
|
346
|
+
when InternalCommandErrorResult
|
347
|
+
raise StopError, concat_spawn_output_and_logs(result.output, nil, result.exit_status)
|
348
|
+
when InternalCommandTimeoutResult
|
349
|
+
raise StopError, concat_spawn_output_and_logs(result.output, nil, nil, "timed out")
|
350
|
+
else
|
351
|
+
raise "Bug: unexpected result from #run_command: #{result.inspect}"
|
352
|
+
end
|
353
|
+
else
|
354
|
+
kill_daemon_with_signal
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
def kill_daemon_with_signal(force: false)
|
359
|
+
pid = read_pid_file
|
360
|
+
if pid
|
361
|
+
if force
|
362
|
+
Process.kill("SIGKILL", pid)
|
363
|
+
else
|
364
|
+
Process.kill(normalize_signal_name(@stop_graceful_signal), pid)
|
365
|
+
end
|
366
|
+
end
|
367
|
+
rescue Errno::ESRCH, Errno::ENOENT
|
368
|
+
end
|
369
|
+
|
370
|
+
def daemon_is_running?
|
371
|
+
pid = read_pid_file
|
372
|
+
if pid.nil?
|
373
|
+
nil
|
374
|
+
elsif check_pid(pid)
|
375
|
+
true
|
376
|
+
else
|
377
|
+
delete_pid_file
|
378
|
+
false
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
def read_pid_file
|
383
|
+
begin
|
384
|
+
pid = File.read(@pid_file).strip
|
385
|
+
rescue Errno::ENOENT
|
386
|
+
return nil
|
387
|
+
end
|
388
|
+
if /\A\d+\Z/.match?(pid)
|
389
|
+
pid.to_i
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
def delete_pid_file
|
394
|
+
File.unlink(@pid_file)
|
395
|
+
rescue Errno::EPERM, Errno::EACCES, Errno::ENOENT # ignore
|
396
|
+
end
|
397
|
+
|
398
|
+
def check_pid(pid)
|
399
|
+
Process.kill(0, pid)
|
400
|
+
true
|
401
|
+
rescue Errno::ESRCH
|
402
|
+
false
|
403
|
+
rescue Errno::EPERM
|
404
|
+
# We didn't have permission to kill the process. Either the process
|
405
|
+
# is owned by someone else, or the system has draconian security
|
406
|
+
# settings and we aren't allowed to kill *any* process. Assume that
|
407
|
+
# the process is running.
|
408
|
+
true
|
409
|
+
end
|
410
|
+
|
411
|
+
def wait_until(sleep_interval: 0.1)
|
412
|
+
until yield
|
413
|
+
sleep(sleep_interval)
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
def wait_until_pid_file_is_available_or_log_file_has_changed
|
418
|
+
until pid_file_available? || log_file_has_changed?
|
419
|
+
sleep 0.1
|
420
|
+
end
|
421
|
+
pid_file_is_available?
|
422
|
+
end
|
423
|
+
|
424
|
+
def wait_until_daemon_responds_to_ping_or_has_exited_or_log_file_has_changed
|
425
|
+
until run_ping_command || !daemon_is_running? || log_file_has_changed?
|
426
|
+
sleep(@ping_interval)
|
427
|
+
end
|
428
|
+
run_ping_command
|
429
|
+
end
|
430
|
+
|
431
|
+
def record_activity
|
432
|
+
@last_activity_time = Time.now
|
433
|
+
end
|
434
|
+
|
435
|
+
# Check whether there has been no recorded activity in the past +seconds+ seconds.
|
436
|
+
def no_activity?(seconds)
|
437
|
+
Time.now - @last_activity_time > seconds
|
438
|
+
end
|
439
|
+
|
440
|
+
def pid_file_available?
|
441
|
+
File.exist?(@pid_file) && File.stat(@pid_file).size != 0
|
442
|
+
end
|
443
|
+
|
444
|
+
# This method does nothing and only serves as a hook for the unit test.
|
445
|
+
def daemon_spawned
|
446
|
+
end
|
447
|
+
|
448
|
+
# This method does nothing and only serves as a hook for the unit test.
|
449
|
+
def start_timed_out(pid)
|
450
|
+
end
|
451
|
+
|
452
|
+
# This method does nothing and only serves as a hook for the unit test.
|
453
|
+
def daemonization_timed_out(pid)
|
454
|
+
end
|
455
|
+
|
456
|
+
# Aborts a daemon that we tried to start, but timed out.
|
457
|
+
def abort_start(pid:, is_direct_child:)
|
458
|
+
begin
|
459
|
+
debug "Killing process #{pid}"
|
460
|
+
Process.kill("SIGTERM", pid)
|
461
|
+
rescue SystemCallError
|
462
|
+
end
|
463
|
+
|
464
|
+
begin
|
465
|
+
timeoutable(@start_abort_timeout) do
|
466
|
+
allow_timeout do
|
467
|
+
if is_direct_child
|
468
|
+
begin
|
469
|
+
debug "Waiting directly for process #{pid}"
|
470
|
+
Process.waitpid(pid)
|
471
|
+
rescue SystemCallError
|
472
|
+
end
|
473
|
+
|
474
|
+
# The daemon may have:
|
475
|
+
# 1. Written a PID file before forking. We delete this PID file.
|
476
|
+
# -OR-
|
477
|
+
# 2. It might have forked (and written a PID file) right before
|
478
|
+
# we terminated it. We'll want the fork to stay alive rather
|
479
|
+
# than going through the (complicated) trouble of killing it.
|
480
|
+
# Don't touch the PID file.
|
481
|
+
pid2 = read_pid_file
|
482
|
+
debug "PID file contains #{pid2.inspect}"
|
483
|
+
delete_pid_file if pid == pid2
|
484
|
+
else
|
485
|
+
debug "Waiting until daemon is no longer running"
|
486
|
+
wait_until { !daemon_is_running? }
|
487
|
+
end
|
488
|
+
end
|
489
|
+
end
|
490
|
+
rescue Timeout::Error
|
491
|
+
begin
|
492
|
+
Process.kill("SIGKILL", pid)
|
493
|
+
rescue SystemCallError
|
494
|
+
end
|
495
|
+
|
496
|
+
allow_timeout do
|
497
|
+
if is_direct_child
|
498
|
+
begin
|
499
|
+
debug "Waiting directly for process #{pid}"
|
500
|
+
Process.waitpid(pid)
|
501
|
+
rescue SystemCallError
|
502
|
+
end
|
503
|
+
|
504
|
+
# The daemon may have:
|
505
|
+
# 1. Written a PID file before forking. We delete this PID file.
|
506
|
+
# -OR-
|
507
|
+
# 2. It might have forked (and written a PID file) right before
|
508
|
+
# we terminated it. We'll want the fork to stay alive rather
|
509
|
+
# than going through the (complicated) trouble of killing it.
|
510
|
+
# Don't touch the PID file.
|
511
|
+
pid2 = read_pid_file
|
512
|
+
debug "PID file contains #{pid2.inspect}"
|
513
|
+
delete_pid_file if pid == pid2
|
514
|
+
else
|
515
|
+
debug "Waiting until daemon is no longer running"
|
516
|
+
wait_until { !daemon_is_running? }
|
517
|
+
end
|
518
|
+
end
|
519
|
+
end
|
520
|
+
end
|
521
|
+
|
522
|
+
def save_log_file_information
|
523
|
+
@original_log_file_stat = begin
|
524
|
+
File.stat(@log_file)
|
525
|
+
rescue
|
526
|
+
nil
|
527
|
+
end
|
528
|
+
@current_log_file_stat = @original_log_file_stat
|
529
|
+
end
|
530
|
+
|
531
|
+
def log_file_has_changed?
|
532
|
+
if @current_log_file_stat
|
533
|
+
stat = begin
|
534
|
+
File.stat(@log_file)
|
535
|
+
rescue
|
536
|
+
nil
|
537
|
+
end
|
538
|
+
if stat
|
539
|
+
result = @current_log_file_stat.mtime != stat.mtime ||
|
540
|
+
@current_log_file_stat.size != stat.size
|
541
|
+
@current_log_file_stat = stat
|
542
|
+
result
|
543
|
+
else
|
544
|
+
true
|
545
|
+
end
|
546
|
+
else
|
547
|
+
false
|
548
|
+
end
|
549
|
+
end
|
550
|
+
|
551
|
+
def differences_in_log_file
|
552
|
+
if @original_log_file_stat && @original_log_file_stat.file?
|
553
|
+
File.open(@log_file, "r") do |f|
|
554
|
+
f.seek(@original_log_file_stat.size, IO::SEEK_SET)
|
555
|
+
f.read.strip
|
556
|
+
end
|
557
|
+
end
|
558
|
+
rescue Errno::ENOENT, Errno::ESPIPE
|
559
|
+
# ESPIPE means the log file is a pipe.
|
560
|
+
nil
|
561
|
+
end
|
562
|
+
|
563
|
+
def determine_lock_file(given_lock_file, identifier, pid_file)
|
564
|
+
if given_lock_file
|
565
|
+
LockFile.new(File.absolute_path(given_lock_file))
|
566
|
+
else
|
567
|
+
LockFile.new(File.absolute_path(pid_file + ".lock"))
|
568
|
+
end
|
569
|
+
end
|
570
|
+
|
571
|
+
def run_command(command)
|
572
|
+
if should_capture_output_while_running_command?
|
573
|
+
# Create tempfile for storing the command's output.
|
574
|
+
tempfile = Tempfile.new("daemon-output")
|
575
|
+
tempfile.chmod(0o666)
|
576
|
+
tempfile_path = tempfile.path
|
577
|
+
tempfile.close
|
578
|
+
|
579
|
+
spawn_options = {
|
580
|
+
in: "/dev/null",
|
581
|
+
out: tempfile_path,
|
582
|
+
err: tempfile_path,
|
583
|
+
close_others: true
|
584
|
+
}
|
585
|
+
else
|
586
|
+
spawn_options = {
|
587
|
+
in: "/dev/null",
|
588
|
+
out: :out,
|
589
|
+
err: :err,
|
590
|
+
close_others: true
|
591
|
+
}
|
592
|
+
end
|
593
|
+
|
594
|
+
if @keep_ios
|
595
|
+
@keep_ios.each do |io|
|
596
|
+
spawn_options[io] = io
|
597
|
+
end
|
598
|
+
end
|
599
|
+
|
600
|
+
pid = if @daemonize_for_me
|
601
|
+
Process.spawn(@env || {}, ruby_interpreter, SPAWNER_FILE,
|
602
|
+
command, spawn_options)
|
603
|
+
else
|
604
|
+
Process.spawn(@env || {}, command, spawn_options)
|
605
|
+
end
|
606
|
+
|
607
|
+
# run_command might be running in a timeout block (like
|
608
|
+
# in #start_without_locking).
|
609
|
+
begin
|
610
|
+
Process.waitpid(pid)
|
611
|
+
rescue Errno::ECHILD
|
612
|
+
# Maybe a background thread or whatever waitpid()'ed
|
613
|
+
# this child process before we had the chance. There's
|
614
|
+
# no way to obtain the exit status now. Assume that
|
615
|
+
# it started successfully; if it didn't we'll know
|
616
|
+
# that later by checking the PID file and by pinging
|
617
|
+
# it.
|
618
|
+
return InternalCommandOkResult.new(pid, tempfile_path ? File.read(tempfile_path).strip : nil)
|
619
|
+
rescue Timeout::Error
|
620
|
+
return InternalCommandTimeoutResult.new(pid, tempfile_path ? File.read(tempfile_path).strip : nil)
|
621
|
+
end
|
622
|
+
|
623
|
+
child_status = $?
|
624
|
+
output = File.read(tempfile_path).strip if tempfile_path
|
625
|
+
if child_status.success?
|
626
|
+
InternalCommandOkResult.new(pid, output)
|
627
|
+
else
|
628
|
+
InternalCommandErrorResult.new(pid, output, child_status)
|
629
|
+
end
|
630
|
+
ensure
|
631
|
+
begin
|
632
|
+
File.unlink(tempfile_path) if tempfile_path
|
633
|
+
rescue SystemCallError
|
634
|
+
nil
|
635
|
+
end
|
636
|
+
end
|
637
|
+
|
638
|
+
def should_capture_output_while_running_command?
|
639
|
+
if is_std_channel_chardev?(@log_file)
|
640
|
+
false
|
641
|
+
else
|
642
|
+
begin
|
643
|
+
real_log_file = Pathname.new(@log_file).realpath.to_s
|
644
|
+
rescue SystemCallError
|
645
|
+
real_log_file = nil
|
646
|
+
end
|
647
|
+
if real_log_file
|
648
|
+
!is_std_channel_chardev?(real_log_file)
|
649
|
+
else
|
650
|
+
true
|
651
|
+
end
|
652
|
+
end
|
653
|
+
end
|
654
|
+
|
655
|
+
def is_std_channel_chardev?(path)
|
656
|
+
path == "/dev/stdout" ||
|
657
|
+
path == "/dev/stderr" ||
|
658
|
+
path == "/dev/fd/1" ||
|
659
|
+
path == "/dev/fd/2" ||
|
660
|
+
path =~ %r{\A/proc/([0-9]+|self)/fd/[12]\Z}
|
661
|
+
end
|
662
|
+
|
663
|
+
def run_ping_command
|
664
|
+
if @ping_command.respond_to?(:call)
|
665
|
+
begin
|
666
|
+
value = @ping_command.call
|
667
|
+
if value.respond_to?(:close)
|
668
|
+
begin
|
669
|
+
value.close
|
670
|
+
rescue
|
671
|
+
nil
|
672
|
+
end
|
673
|
+
end
|
674
|
+
value
|
675
|
+
rescue *ALLOWED_CONNECT_EXCEPTIONS
|
676
|
+
false
|
677
|
+
end
|
678
|
+
elsif @ping_command.is_a?(Array)
|
679
|
+
type, *args = @ping_command
|
680
|
+
if self.class.can_ping_unix_sockets?
|
681
|
+
case type
|
682
|
+
when :tcp
|
683
|
+
hostname, port = args
|
684
|
+
sockaddr = Socket.pack_sockaddr_in(port, hostname)
|
685
|
+
ping_tcp_socket(sockaddr)
|
686
|
+
when :unix
|
687
|
+
socket_domain = Socket::Constants::AF_LOCAL
|
688
|
+
sockaddr = Socket.pack_sockaddr_un(args[0])
|
689
|
+
ping_socket(socket_domain, sockaddr)
|
690
|
+
else
|
691
|
+
raise ArgumentError, "Unknown ping command type #{type.inspect}"
|
692
|
+
end
|
693
|
+
else
|
694
|
+
case type
|
695
|
+
when :tcp
|
696
|
+
hostname, port = args
|
697
|
+
ping_socket(hostname, port)
|
698
|
+
when :unix
|
699
|
+
raise "Pinging Unix domain sockets is not supported on this Ruby implementation"
|
700
|
+
else
|
701
|
+
raise ArgumentError, "Unknown ping command type #{type.inspect}"
|
702
|
+
end
|
703
|
+
end
|
704
|
+
else
|
705
|
+
system(@ping_command)
|
706
|
+
end
|
707
|
+
end
|
708
|
+
|
709
|
+
if !can_ping_unix_sockets?
|
710
|
+
require "java"
|
711
|
+
|
712
|
+
def ping_socket(host_name, port)
|
713
|
+
channel = java.nio.channels.SocketChannel.open
|
714
|
+
begin
|
715
|
+
address = java.net.InetSocketAddress.new(host_name, port)
|
716
|
+
channel.configure_blocking(false)
|
717
|
+
if channel.connect(address)
|
718
|
+
return true
|
719
|
+
end
|
720
|
+
|
721
|
+
deadline = Time.now.to_f + 0.1
|
722
|
+
while true
|
723
|
+
begin
|
724
|
+
if channel.finish_connect
|
725
|
+
return true
|
726
|
+
end
|
727
|
+
rescue java.net.ConnectException => e
|
728
|
+
if /Connection refused/i.match?(e.message)
|
729
|
+
return false
|
730
|
+
else
|
731
|
+
throw e
|
732
|
+
end
|
733
|
+
end
|
734
|
+
|
735
|
+
# Not done connecting and no error.
|
736
|
+
sleep 0.01
|
737
|
+
if Time.now.to_f >= deadline
|
738
|
+
return false
|
739
|
+
end
|
740
|
+
end
|
741
|
+
ensure
|
742
|
+
channel.close
|
743
|
+
end
|
744
|
+
end
|
745
|
+
else
|
746
|
+
def ping_socket(socket_domain, sockaddr)
|
747
|
+
socket = Socket.new(socket_domain, Socket::Constants::SOCK_STREAM, 0)
|
748
|
+
begin
|
749
|
+
socket.connect_nonblock(sockaddr)
|
750
|
+
rescue Errno::ENOENT, Errno::EINPROGRESS, Errno::EAGAIN, Errno::EWOULDBLOCK
|
751
|
+
if select(nil, [socket], nil, 0.1)
|
752
|
+
begin
|
753
|
+
socket.connect_nonblock(sockaddr)
|
754
|
+
rescue Errno::EISCONN
|
755
|
+
rescue Errno::EINVAL
|
756
|
+
if RUBY_PLATFORM.match?(/freebsd/i)
|
757
|
+
raise Errno::ECONNREFUSED
|
758
|
+
else
|
759
|
+
raise
|
760
|
+
end
|
761
|
+
end
|
762
|
+
else
|
763
|
+
raise Errno::ECONNREFUSED
|
764
|
+
end
|
765
|
+
end
|
766
|
+
true
|
767
|
+
rescue Errno::ECONNREFUSED, Errno::ENOENT
|
768
|
+
false
|
769
|
+
ensure
|
770
|
+
socket.close if socket
|
771
|
+
end
|
772
|
+
|
773
|
+
def ping_tcp_socket(sockaddr)
|
774
|
+
ping_socket(Socket::Constants::AF_INET, sockaddr)
|
775
|
+
rescue Errno::EAFNOSUPPORT
|
776
|
+
ping_socket(Socket::Constants::AF_INET6, sockaddr)
|
777
|
+
end
|
778
|
+
end
|
779
|
+
|
780
|
+
def ruby_interpreter
|
781
|
+
rb_config = if defined?(RbConfig)
|
782
|
+
RbConfig::CONFIG
|
783
|
+
else
|
784
|
+
Config::CONFIG
|
785
|
+
end
|
786
|
+
File.join(
|
787
|
+
rb_config["bindir"],
|
788
|
+
rb_config["RUBY_INSTALL_NAME"]
|
789
|
+
) + rb_config["EXEEXT"]
|
790
|
+
end
|
791
|
+
|
792
|
+
def timeoutable(amount, &block)
|
793
|
+
Thread.handle_interrupt(Timeout::Error => :never) do
|
794
|
+
start_time = monotonic_time
|
795
|
+
result = Timeout.timeout(amount, Timeout::Error, &block)
|
796
|
+
[result, [monotonic_time - start_time, 0].max]
|
797
|
+
end
|
798
|
+
end
|
799
|
+
|
800
|
+
def allow_timeout(&block)
|
801
|
+
Thread.handle_interrupt(Timeout::Error => :on_blocking, &block)
|
802
|
+
end
|
803
|
+
|
804
|
+
def monotonic_time
|
805
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
806
|
+
end
|
807
|
+
|
808
|
+
def signal_termination_message(process_status)
|
809
|
+
if process_status.signaled?
|
810
|
+
"terminated with signal #{signal_name_for(process_status.termsig)}"
|
811
|
+
else
|
812
|
+
"exited with status #{process_status.exitstatus}"
|
813
|
+
end
|
814
|
+
end
|
815
|
+
|
816
|
+
def normalize_signal_name(name)
|
817
|
+
name.start_with?("SIG") ? name : "SIG#{name}"
|
818
|
+
end
|
819
|
+
|
820
|
+
def signal_name_for(num)
|
821
|
+
if (name = Signal.list.find { |name, n| n == num }[0])
|
822
|
+
"SIG#{name}"
|
823
|
+
else
|
824
|
+
num.to_s
|
825
|
+
end
|
826
|
+
end
|
827
|
+
|
828
|
+
def concat_spawn_output_and_logs(output, logs, exit_status = nil, suffix_message = nil)
|
829
|
+
if output.nil? && logs.nil?
|
830
|
+
result_inner = [
|
831
|
+
"logs not available",
|
832
|
+
exit_status ? signal_termination_message(exit_status) : nil,
|
833
|
+
suffix_message
|
834
|
+
].compact.join("; ")
|
835
|
+
"(#{result_inner})"
|
836
|
+
elsif (output && output.empty? && logs && logs.empty?) || (output && output.empty? && logs.nil?) || (output.nil? && logs && logs.empty?)
|
837
|
+
result_inner = [
|
838
|
+
"logs empty",
|
839
|
+
exit_status ? signal_termination_message(exit_status) : nil,
|
840
|
+
suffix_message
|
841
|
+
].compact.join("; ")
|
842
|
+
"(#{result_inner})"
|
843
|
+
else
|
844
|
+
result = ((output || "") + "\n" + (logs || "")).strip
|
845
|
+
result_suffix = [
|
846
|
+
exit_status ? signal_termination_message(exit_status) : nil,
|
847
|
+
suffix_message
|
848
|
+
].compact.join("; ")
|
849
|
+
if !result_suffix.empty?
|
850
|
+
result << "\n(#{result_suffix})"
|
851
|
+
end
|
852
|
+
result
|
853
|
+
end
|
854
|
+
end
|
855
|
+
|
856
|
+
def debug(message)
|
857
|
+
@logger.debug(message) if @logger
|
858
|
+
end
|
873
859
|
end
|