File tree Expand file tree Collapse file tree 4 files changed +77
-0
lines changed
Expand file tree Collapse file tree 4 files changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ def initialize
3636 @source = ::ENV [ "FUNCTION_SOURCE" ] || ::FunctionsFramework ::DEFAULT_SOURCE
3737 @env = nil
3838 @port = nil
39+ @pidfile = nil
3940 @bind = nil
4041 @min_threads = nil
4142 @max_threads = nil
@@ -67,6 +68,12 @@ def error?
6768 #
6869 attr_reader :error_message
6970
71+ ##
72+ # @return [String] The pidfile.
73+ # @return [nil] if not running.
74+ #
75+ attr_reader :pidfile
76+
7077 ##
7178 # Parse the given command line arguments.
7279 # Exits if argument parsing failed.
@@ -89,6 +96,9 @@ def parse_args argv # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
8996 "Supported values are 'http' and 'cloudevent'." do |val |
9097 @signature_type = val
9198 end
99+ op . on "-P" , "--pidfile PIDFILE" , "Set the pidfile for the server (defaults to puma.pid)" do |val |
100+ @pidfile = val
101+ end
92102 op . on "-p" , "--port PORT" , "Set the port to listen to (defaults to 8080)" do |val |
93103 @port = val . to_i
94104 end
@@ -218,6 +228,7 @@ def start_server
218228 ::FunctionsFramework . start function do |config |
219229 config . rack_env = @env
220230 config . port = @port
231+ config . pidfile = @pidfile
221232 config . bind_addr = @bind
222233 config . show_error_details = @detailed_errors
223234 config . min_threads = @min_threads
Original file line number Diff line number Diff line change @@ -152,6 +152,24 @@ def running?
152152 @server &.thread &.alive?
153153 end
154154
155+ ##
156+ # Returns pidfile if server is currently running
157+ #
158+ # @return [String, nil]
159+ #
160+ def pidfile
161+ @config . pidfile if running?
162+ end
163+
164+ ##
165+ # Returns whether pidfile is present.
166+ #
167+ # @return [Boolean]
168+ #
169+ def pidfile?
170+ !!@config . pidfile && running?
171+ end
172+
155173 ##
156174 # Cause this server to respond to SIGTERM, SIGINT, and SIGHUP by shutting
157175 # down gracefully.
@@ -214,6 +232,7 @@ def initialize
214232 self . rack_env = nil
215233 self . bind_addr = nil
216234 self . port = nil
235+ self . pidfile = nil
217236 self . min_threads = nil
218237 self . max_threads = nil
219238 self . show_error_details = nil
@@ -245,6 +264,14 @@ def port= port
245264 @port = ( port || ::ENV [ "PORT" ] || 8080 ) . to_i
246265 end
247266
267+ ##
268+ # Set the pidfile string, or `nil` to use the default.
269+ # @param path [String,nil]
270+ #
271+ def pidfile = path
272+ @pidfile = ( path || ::ENV [ "PIDFILE" ] || "puma.pid" ) . to_s
273+ end
274+
248275 ##
249276 # Set the minimum number of worker threads, or `nil` to use the default.
250277 # @param min_threads [Integer,nil]
@@ -306,6 +333,14 @@ def port
306333 @port
307334 end
308335
336+ ##
337+ # Returns the current pidfile string.
338+ # @return [String]
339+ #
340+ def pidfile
341+ @pidfile
342+ end
343+
309344 ##
310345 # Returns the minimum number of worker threads in the thread pool.
311346 # @return [Integer]
Original file line number Diff line number Diff line change 2727 let ( :retry_count ) { 10 }
2828 let ( :retry_interval ) { 0.5 }
2929 let ( :port ) { "8066" }
30+ let ( :pidfile ) { "server.pid" }
3031 let ( :timeout ) { 10 }
3132
3233 def run_with_retry cli
@@ -104,6 +105,21 @@ def run_in_timeout cli
104105 assert_equal "I received a request: GET http://127.0.0.1:#{ port } /" , response . body
105106 end
106107
108+ it "runs an http server with a pidfile" do
109+ args = [
110+ "--source" , http_source ,
111+ "--target" , "simple_http" ,
112+ "--port" , port ,
113+ "--pidfile" , pidfile ,
114+ "-q"
115+ ]
116+ cli = FunctionsFramework ::CLI . new . parse_args args
117+ _response = run_with_retry cli do
118+ Net ::HTTP . get_response URI ( "http://127.0.0.1:#{ port } /" )
119+ end
120+ assert_equal pidfile , cli . pidfile
121+ end
122+
107123 it "runs an http server with a function that includes a return" do
108124 args = [
109125 "--source" , http_return_source ,
Original file line number Diff line number Diff line change 3838 end
3939 }
4040 let ( :port ) { 8077 }
41+ let ( :pidfile ) { "server.pid" }
4142 let ( :server_url ) { "http://127.0.0.1:#{ port } " }
4243 let ( :quiet_logger ) {
4344 logger = ::Logger . new $stderr
@@ -56,6 +57,7 @@ def make_basic_server function, show_error_details: true
5657 config . min_threads = 1
5758 config . max_threads = 1
5859 config . port = port
60+ config . pidfile = pidfile
5961 config . bind_addr = "127.0.0.1"
6062 config . rack_env = "development"
6163 config . logger = quiet_logger
@@ -94,6 +96,19 @@ def query_server_with_retry server
9496 http_server . stop . wait_until_stopped timeout : 10
9597 end
9698
99+ it "uses a pidfile" do
100+ refute http_server . pidfile?
101+ refute http_server . pidfile
102+ http_server . start
103+ assert http_server . pidfile?
104+ assert http_server . pidfile
105+ http_server . stop . wait_until_stopped timeout : 10
106+ refute http_server . running?
107+ refute http_server . pidfile?
108+ ensure
109+ http_server . stop . wait_until_stopped timeout : 10
110+ end
111+
97112 it "handles post requests" do
98113 response = query_server_with_retry http_server do
99114 ::Net ::HTTP . post URI ( "#{ server_url } /" ) , "Hello, world!" , "Content-Type" => "text/plain"
You can’t perform that action at this time.
0 commit comments