@@ -438,6 +438,66 @@ def self.perform
438
438
assert_not_equal original_connection , Resque . redis . client . connection . instance_variable_get ( "@sock" )
439
439
end
440
440
441
+ if !defined? ( RUBY_ENGINE ) || defined? ( RUBY_ENGINE ) && RUBY_ENGINE != "jruby"
442
+ test "old signal handling is the default" do
443
+ rescue_time = nil
444
+
445
+ begin
446
+ class LongRunningJob
447
+ @queue = :long_running_job
448
+
449
+ def self . perform ( run_time , rescue_time = nil )
450
+ Resque . redis . client . reconnect # get its own connection
451
+ Resque . redis . rpush ( 'sigterm-test:start' , Process . pid )
452
+ sleep run_time
453
+ Resque . redis . rpush ( 'sigterm-test:result' , 'Finished Normally' )
454
+ rescue Resque ::TermException => e
455
+ Resque . redis . rpush ( 'sigterm-test:result' , %Q(Caught SignalException: #{ e . inspect } ) )
456
+ sleep rescue_time unless rescue_time . nil?
457
+ ensure
458
+ puts 'fuuuu'
459
+ Resque . redis . rpush ( 'sigterm-test:final' , 'exiting.' )
460
+ end
461
+ end
462
+
463
+ Resque . enqueue ( LongRunningJob , 5 , rescue_time )
464
+
465
+ worker_pid = Kernel . fork do
466
+ # ensure we actually fork
467
+ $TESTING = false
468
+ # reconnect since we just forked
469
+ Resque . redis . client . reconnect
470
+
471
+ worker = Resque ::Worker . new ( :long_running_job )
472
+
473
+ worker . work ( 0 )
474
+ exit!
475
+ end
476
+
477
+ # ensure the worker is started
478
+ start_status = Resque . redis . blpop ( 'sigterm-test:start' , 5 )
479
+ assert_not_nil start_status
480
+ child_pid = start_status [ 1 ] . to_i
481
+ assert_operator child_pid , :> , 0
482
+
483
+ # send signal to abort the worker
484
+ Process . kill ( 'TERM' , worker_pid )
485
+ Process . waitpid ( worker_pid )
486
+
487
+ # wait to see how it all came down
488
+ result = Resque . redis . blpop ( 'sigterm-test:result' , 5 )
489
+ assert_nil result
490
+
491
+ # ensure that the child pid is no longer running
492
+ child_still_running = !( `ps -p #{ child_pid . to_s } -o pid=` ) . empty?
493
+ assert !child_still_running
494
+ ensure
495
+ remaining_keys = Resque . redis . keys ( 'sigterm-test:*' ) || [ ]
496
+ Resque . redis . del ( *remaining_keys ) unless remaining_keys . empty?
497
+ end
498
+ end
499
+ end
500
+
441
501
if !defined? ( RUBY_ENGINE ) || defined? ( RUBY_ENGINE ) && RUBY_ENGINE != "jruby"
442
502
[ SignalException , Resque ::TermException ] . each do |exception |
443
503
{
0 commit comments