@@ -1863,7 +1863,7 @@ module Puma
1863
1863
def run
1864
1864
# ...
1865
1865
1866
- # Set the behaviors for signals like `$ kill -s SIGTERMprocess_id ` received.
1866
+ # Set the behaviors for signals like `$ kill -s SIGTERMpuma_process_id ` received.
1867
1867
setup_signals# We will discuss this line later.
1868
1868
1869
1869
set_process_title
@@ -2018,6 +2018,7 @@ module Puma
2018
2018
begin
2019
2019
# This line will cause current thread waiting until a request arrives.
2020
2020
# So it will be the entry of every request!
2021
+ # sockets: [#<IO:fd 23>, #<TCPServer:fd 22, AF_INET, 0.0.0.0, 3000>]
2021
2022
ios= IO .select sockets
2022
2023
2023
2024
ios.first.eachdo |sock |
@@ -2070,7 +2071,7 @@ module Puma
2070
2071
@reaper = nil
2071
2072
2072
2073
@mutex .synchronizedo
2073
- @min .times { spawn_thread }# Pumaspawned @min count threads.
2074
+ @min .times { spawn_thread }# Pumaspawns @min count threads.
2074
2075
end
2075
2076
end
2076
2077
@@ -2215,7 +2216,7 @@ In `#perform`, call `Rails::Server#start`. Then call `Rack::Server#start`.
2215
2216
2216
2217
Then call` Rack::Handler::Puma.run(YourProject::Application.new) ` .
2217
2218
2218
- In` .run ` , Puma will new a always running Thread for` ios = IO.select sockets ` .
2219
+ In` .run ` , Puma will new a always running Thread for` ios = IO.select(#<TCPServer:fd 22, AF_INET, 0.0.0.0, 3000>) ` .
2219
2220
2220
2221
Request is created from` ios ` object.
2221
2222
@@ -2261,12 +2262,14 @@ After you added `thread.join`, you can see:
2261
2262
in console.
2262
2263
2263
2264
# #### Example two
2265
+ Try to run` test_thread_join2.rb` .
2264
2266
` ` ` ruby
2267
+ # ./test_thread_join2.rb
2265
2268
arr = [
2266
2269
Thread.new { sleep 1 },
2267
2270
Thread.new do
2268
2271
sleep 5
2269
- puts 'I amarry [1]'
2272
+ puts 'I amarr [1]'
2270
2273
end,
2271
2274
Thread.new { sleep 8}
2272
2275
]
@@ -2279,7 +2282,7 @@ arr.each { |thread| puts "~~~~~ #{thread}" }
2279
2282
2280
2283
puts Thread.list.size # returns 3 (because arr[0] is dead)
2281
2284
2282
- # arr[1].join #comment off to see differences
2285
+ # arr[1].join #uncomment to see differences
2283
2286
2284
2287
arr.each { |thread| puts "~~~~~#{ thread } " }
2285
2288
@@ -2297,7 +2300,7 @@ module Puma
2297
2300
def run
2298
2301
#...
2299
2302
2300
- # Set the behaviors for signals like` $ kill- sSIGTERM process_id ` .
2303
+ # Set the behaviors for signals like` $ kill- sSIGTERM puma_process_id ` .
2301
2304
setup_signals # Let's step into this line.
2302
2305
2303
2306
set_process_title
@@ -2307,7 +2310,7 @@ module Puma
2307
2310
# ...
2308
2311
end
2309
2312
2310
- # Set the behaviors for signals like` $ kill- sSIGTERM process_id ` .
2313
+ # Set the behaviors for signals like` $ kill- sSIGTERM puma_process_id ` .
2311
2314
# Signal.list #=> {"EXIT"=>0, "HUP"=>1, "INT"=>2, "QUIT"=>3, "ILL"=>4, "TRAP"=>5, "IOT"=>6, "ABRT"=>6, "FPE"=>8, "KILL"=>9, "BUS"=>7, "SEGV"=>11, "SYS"=>31, "PIPE"=>13, "ALRM"=>14, "TERM"=>15, "URG"=>23, "STOP"=>19, "TSTP"=>20, "CONT"=>18, "CHLD"=>17, "CLD"=>17, "TTIN"=>21, "TTOU"=>22, "IO"=>29, "XCPU"=>24, "XFSZ"=>25, "VTALRM"=>26, "PROF"=>27, "WINCH"=>28, "USR1"=>10, "USR2"=>12, "PWR"=>30, "POLL"=>29}
2312
2315
# Press` Control + C ` to quit means 'SIGINT'.
2313
2316
def setup_signals
@@ -2429,13 +2432,14 @@ module Puma
2429
2432
2430
2433
# The created @thread is the @thread in` stop` method below.
2431
2434
@thread = Thread.new {
2435
+ # FYI, this is in the puma starting process.
2432
2436
# 'Thread.current.object_id' returns '70144220123860',
2433
2437
# which is the same as the 'Thread.current.object_id' in 'handle_servers' in Puma::Server#run
2434
2438
# def handle_servers
2435
2439
# begin
2436
2440
# # ...
2437
2441
# ensure
2438
- # # FYI, the 'ensure' part isexecuted after ` $ kill - s SIGTERM process_id ` .
2442
+ # # FYI, the 'ensure' part isin the puma stopping process .
2439
2443
# puts "#{ Thread .current.object_id} " # returns '70144220123860' too.
2440
2444
# end
2441
2445
# end
@@ -2450,8 +2454,8 @@ module Puma
2450
2454
# off the request queue before finally exiting.
2451
2455
def stop(sync=false)
2452
2456
# This line will set '@status = :stop',
2453
- # and cause` ios= IO .select sockets` in method` handle_servers` to return result.
2454
- # So the code after` ios= IO .select sockets` will be executed.
2457
+ # and cause` ios= IO .select sockets` ( in method` handle_servers` ) to return result.
2458
+ # Sothat the code after` ios= IO .select sockets` will be executed.
2455
2459
notify_safely(STOP_COMMAND) # Let's step into this line.
2456
2460
2457
2461
# 'Thread.current.object_id' returns '70144214949920',
@@ -2479,7 +2483,7 @@ module Puma
2479
2483
# ...
2480
2484
2481
2485
while @status == :run
2482
- # After `notify_safely(STOP_COMMAND)` in main thread,this line will be executed and will return result.
2486
+ # After `notify_safely(STOP_COMMAND)` in main thread,`ios = IO.select sockets` will return result.
2483
2487
# FYI, `@check, @notify = IO.pipe`.
2484
2488
# def notify_safely(message)
2485
2489
# @notify << message
@@ -2508,9 +2512,11 @@ module Puma
2508
2512
2509
2513
# ...
2510
2514
ensure
2515
+ # FYI, the 'ensure' part is in the puma stopping process.
2511
2516
# 'Thread.current.object_id' returns '70144220123860',
2512
2517
# which is the same as the 'Thread.current.object_id' in 'Thread.new block' in Puma::Server#run
2513
2518
# @thread = Thread.new do
2519
+ # # FYI, this is in the puma starting process.
2514
2520
# puts "#{Thread.current.object_id}" # returns '70144220123860'
2515
2521
# handle_servers
2516
2522
# end
@@ -2567,13 +2573,13 @@ module Puma
2567
2573
# @workers is an array.
2568
2574
# @workers.dup will not create new thread.
2569
2575
# @workers is an instance variable and will be changed when shutdown (by `@workers.delete th`).
2570
- # So ues dup.
2576
+ # So ues@workers. dup here .
2571
2577
@workers .dup
2572
2578
end
2573
2579
2574
2580
# Wait for threads to finish without force shutdown.
2575
2581
threads.eachdo |thread |
2576
- thread.join# I guess `thread.join` means join the executing of thread to the calling (main) process.
2582
+ thread.join
2577
2583
end
2578
2584
2579
2585
@spawned = 0
@@ -2586,11 +2592,11 @@ module Puma
2586
2592
@spawned = 0 # The count of @spawned threads.
2587
2593
@todo = []# @todo is requests (in puma, it's Puma::Client instance) which need to be processed.
2588
2594
@min = Integer (min)# @min threads count
2589
- @block = block# block will be called in method `spawn_thread` toprocessed a request.
2595
+ @block = block# block will be called in method `spawn_thread` toprocess a request.
2590
2596
@workers = []
2591
2597
2592
2598
@mutex .synchronizedo
2593
- @min .times { spawn_thread }# Pumastarted @min count threads.
2599
+ @min .times { spawn_thread }# Pumaspawns @min count threads.
2594
2600
end
2595
2601
end
2596
2602
@@ -2619,7 +2625,7 @@ module Puma
2619
2625
end
2620
2626
2621
2627
# ...
2622
- # After `@not_empty.broadcast`in executed in '#shutdown', `not_empty` is waked up.
2628
+ # After `@not_empty.broadcast`is executed in '#shutdown', `not_empty` is waked up.
2623
2629
# Ruby will continue to execute the next line here.
2624
2630
not_empty.wait mutex
2625
2631
@@ -2650,7 +2656,7 @@ end
2650
2656
2651
2657
So all the threads in the ThreadPool joined and finished.
2652
2658
2653
- And please look at the caller in block of` Signal.trap "SIGTERM" ` below.
2659
+ Let's inspect the caller in block of` Signal.trap "SIGTERM" ` below.
2654
2660
2655
2661
``` ruby
2656
2662
# .gems/puma-3.12.0/lib/puma/launcher.rb
@@ -2661,7 +2667,7 @@ module Puma
2661
2667
def run
2662
2668
# ...
2663
2669
2664
- # Set the behaviors for signals like `$ kill -s SIGTERMprocess_id `.
2670
+ # Set the behaviors for signals like `$ kill -s SIGTERMpuma_process_id `.
2665
2671
setup_signals# Let's step into this line.
2666
2672
2667
2673
set_process_title
@@ -2679,7 +2685,7 @@ module Puma
2679
2685
begin
2680
2686
# After running `$ kill -s SIGTERM puma_process_id`, Ruby will execute the block of `Signal.trap "SIGTERM"`.
2681
2687
Signal .trap " SIGTERM" do
2682
- # Iadded `caller` to see thecalling stack.
2688
+ # Iinspect `caller` to see thecaller stack.
2683
2689
# caller: [
2684
2690
# "../gems/puma-3.12.0/lib/puma/single.rb:118:in `join'",
2685
2691
# "../gems/puma-3.12.0/lib/puma/single.rb:118:in `run'",