Skip to content

Commit 1811f15

Browse files
committed
cli: Add compatibility macro for Time.instant vs Time.monotonic
Time.instant was added in Crystal 1.19.0 and Time.monotonic was deprecated. Alpine's Crystal package is older, so we need to use the right method based on Crystal version. The monotonic_time macro selects Time.instant for Crystal >= 1.19.0 and Time.monotonic for older versions.
1 parent f697345 commit 1811f15

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

cli/src/cli.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ require "./manage"
1717
require "./worker"
1818

1919
module Statbus
20+
# Compatibility helper for Time.instant (Crystal 1.19+) vs Time.monotonic (older)
21+
# Time.monotonic was deprecated in favor of Time.instant in Crystal 1.19.0
22+
macro monotonic_time
23+
{% if compare_versions(Crystal::VERSION, "1.19.0") >= 0 %}
24+
Time.instant
25+
{% else %}
26+
Time.monotonic
27+
{% end %}
28+
end
2029
class Cli
2130
enum Mode
2231
Manage

cli/src/import.cr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ module Statbus
287287
when Strategy::Copy
288288
db.exec "CALL test.set_user_from_email($1)", @user_email
289289
copy_stream = db.exec_copy "COPY public.#{upload_view_name}(#{sql_fields_str}) FROM STDIN"
290-
start_time = Time.instant
290+
start_time = Statbus.monotonic_time
291291
row_count = 0
292292

293293
iterate_csv_stream(csv_stream) do |sql_row, csv_row|
@@ -305,7 +305,7 @@ module Statbus
305305
puts "Waiting for processing" if @config.verbose
306306
copy_stream.close
307307

308-
total_duration = Time.instant - start_time
308+
total_duration = Statbus.monotonic_time - start_time
309309
total_rows_per_second = row_count / total_duration.total_seconds
310310
puts "Total rows processed: #{row_count}"
311311
puts "Total time: #{total_duration.total_seconds.round(2)} seconds (#{total_rows_per_second.round(2)} rows/second)"
@@ -323,7 +323,7 @@ module Statbus
323323
db.exec "SET LOCAL statbus.constraints_already_deferred TO 'true';"
324324
db.exec "SET CONSTRAINTS ALL DEFERRED;"
325325
end
326-
start_time = Time.instant
326+
start_time = Statbus.monotonic_time
327327
batch_start_time = start_time
328328
row_count = 0
329329
insert = db.build sql_statement
@@ -342,10 +342,10 @@ module Statbus
342342
end
343343
db.exec "END;"
344344

345-
batch_duration = Time.instant - batch_start_time
345+
batch_duration = Statbus.monotonic_time - batch_start_time
346346
batch_rows_per_second = batch_size / batch_duration.total_seconds
347347
puts "Processed #{batch_size} rows in #{batch_duration.total_seconds.round(2)} seconds (#{batch_rows_per_second.round(2)} rows/second)"
348-
batch_start_time = Time.instant
348+
batch_start_time = Statbus.monotonic_time
349349

350350
db.exec "BEGIN;"
351351
db.exec "CALL test.set_user_from_email($1)", @user_email
@@ -363,7 +363,7 @@ module Statbus
363363
end
364364
db.exec "END;"
365365

366-
total_duration = Time.instant - start_time
366+
total_duration = Statbus.monotonic_time - start_time
367367
total_rows_per_second = row_count / total_duration.total_seconds
368368
puts "Total rows processed: #{row_count}"
369369
puts "Total time: #{total_duration.total_seconds.round(2)} seconds (#{total_rows_per_second.round(2)} rows/second)"

cli/src/migrate.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ module Statbus
327327
STDOUT.flush
328328
end
329329

330-
start_time = Time.instant
330+
start_time = Statbus.monotonic_time
331331
success = false
332332

333333
case migration.extension
@@ -357,7 +357,7 @@ module Statbus
357357
end
358358

359359
if success
360-
duration_ms = (Time.instant - start_time).total_milliseconds.to_i
360+
duration_ms = (Statbus.monotonic_time - start_time).total_milliseconds.to_i
361361

362362
# Record successful migration
363363
db.exec(
@@ -414,7 +414,7 @@ module Statbus
414414
end
415415

416416
# Start timing
417-
start_time = Time.instant
417+
start_time = Statbus.monotonic_time
418418

419419
success = false
420420
output = ""
@@ -444,7 +444,7 @@ module Statbus
444444

445445
if success
446446
# Calculate duration in milliseconds
447-
duration_ms = (Time.instant - start_time).total_milliseconds.to_i
447+
duration_ms = (Statbus.monotonic_time - start_time).total_milliseconds.to_i
448448

449449
# Remove the migration record(s)
450450
db.query_all(delete_sql, version, as: {version: String})

cli/src/worker.cr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ module Statbus
168168

169169
# Wait for acknowledgments or timeout
170170
shutdown_timeout = 5.seconds
171-
shutdown_start = Time.instant
171+
shutdown_start = Statbus.monotonic_time
172172
received_count = 1 # Start with 1 for the main fiber
173173

174174
loop do
175175
# Check if we've exceeded the timeout
176-
if (Time.instant - shutdown_start) > shutdown_timeout
176+
if (Statbus.monotonic_time - shutdown_start) > shutdown_timeout
177177
@log.warn { "Shutdown timeout reached after #{shutdown_timeout.total_seconds} seconds, forcing exit" }
178178
break
179179
end
@@ -496,8 +496,8 @@ module Statbus
496496
end
497497

498498
# Wait for acknowledgments with a short timeout
499-
shutdown_start = Time.instant
500-
while (Time.instant - shutdown_start) < 3.seconds
499+
shutdown_start = Statbus.monotonic_time
500+
while (Statbus.monotonic_time - shutdown_start) < 3.seconds
501501
# Try to receive with a very short timeout to check if channel is empty
502502
begin
503503
select
@@ -654,8 +654,8 @@ module Statbus
654654

655655
# Helper method to wait while checking for shutdown
656656
private def wait_with_shutdown_check(duration : Time::Span)
657-
start_time = Time.instant
658-
while (Time.instant - start_time) < duration
657+
start_time = Statbus.monotonic_time
658+
while (Statbus.monotonic_time - start_time) < duration
659659
return if @shutdown
660660
sleep(0.1.seconds) # Check for shutdown frequently
661661
end
@@ -939,7 +939,7 @@ module Statbus
939939

940940
# Process pending tasks for a specific queue
941941
private def process_tasks(queue : String)
942-
start_time = Time.instant
942+
start_time = Statbus.monotonic_time
943943
consecutive_errors = 0
944944
max_consecutive_errors = 3
945945

@@ -1023,7 +1023,7 @@ module Statbus
10231023
sleep(5.seconds) # Wait before retrying
10241024
end
10251025
ensure
1026-
duration_ms = (Time.instant - start_time).total_milliseconds.to_i
1026+
duration_ms = (Statbus.monotonic_time - start_time).total_milliseconds.to_i
10271027
@log.debug { "Task processing for queue #{queue} completed in #{duration_ms}ms" }
10281028
end
10291029
end

0 commit comments

Comments
 (0)