Skip to content

Commit

Permalink
Remove use of etc and time gems in Puma (#3035)
Browse files Browse the repository at this point in the history
* Remove 'time' gem requirement

* Remove 'etc' gem requirement

* test_integration_cluster.rb - check for iso 8601 format
  • Loading branch information
MSP-Greg committed Dec 24, 2022
1 parent 423d17c commit 97ec248
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 0 additions & 2 deletions lib/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# Standard libraries
require 'socket'
require 'tempfile'
require 'time'
require 'etc'
require 'uri'
require 'stringio'

Expand Down
8 changes: 3 additions & 5 deletions lib/puma/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
require_relative 'cluster/worker_handle'
require_relative 'cluster/worker'

require 'time'

module Puma
# This class is instantiated by the `Puma::Launcher` and used
# to boot and serve a Ruby application when puma "workers" are needed
Expand Down Expand Up @@ -252,18 +250,18 @@ def stats
old_worker_count = @workers.count { |w| w.phase != @phase }
worker_status = @workers.map do |w|
{
started_at: w.started_at.utc.iso8601,
started_at: utc_iso8601(w.started_at),
pid: w.pid,
index: w.index,
phase: w.phase,
booted: w.booted?,
last_checkin: w.last_checkin.utc.iso8601,
last_checkin: utc_iso8601(w.last_checkin),
last_status: w.last_status,
}
end

{
started_at: @started_at.utc.iso8601,
started_at: utc_iso8601(@started_at),
workers: @workers.size,
phase: @phase,
booted_workers: worker_status.count { |w| w[:booted] },
Expand Down
4 changes: 4 additions & 0 deletions lib/puma/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ def ensure_output_directory_exists(path, io_name)
end
end

def utc_iso8601(val)
"#{val.utc.strftime '%FT%T'}Z"
end

def stats
{
versions: {
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/single.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Single < Runner
# @!attribute [r] stats
def stats
{
started_at: @started_at.utc.iso8601
started_at: utc_iso8601(@started_at)
}.merge(@server.stats).merge(super)
end

Expand Down
10 changes: 8 additions & 2 deletions test/test_integration_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,22 @@ def test_stuck_phased_restart
end

def test_worker_check_interval
# iso8601 2022-12-14T00:05:49Z
re_8601 = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\z/
@control_tcp_port = UniquePort.call
worker_check_interval = 1

cli_server "-w 1 -t 1:1 --control-url tcp://#{HOST}:#{@control_tcp_port} --control-token #{TOKEN} test/rackup/hello.ru", config: "worker_check_interval #{worker_check_interval}"

sleep worker_check_interval + 1
last_checkin_1 = Time.parse(get_stats["worker_status"].first["last_checkin"])
checkin_1 = get_stats["worker_status"].first["last_checkin"]
assert_match re_8601, checkin_1
last_checkin_1 = Time.parse checkin_1

sleep worker_check_interval + 1
last_checkin_2 = Time.parse(get_stats["worker_status"].first["last_checkin"])
checkin_2 = get_stats["worker_status"].first["last_checkin"]
assert_match re_8601, checkin_2
last_checkin_2 = Time.parse checkin_2

assert(last_checkin_2 > last_checkin_1)
end
Expand Down

0 comments on commit 97ec248

Please sign in to comment.