From 575acd1d6876bdf95e618ebcca7253434f684971 Mon Sep 17 00:00:00 2001 From: ka Date: Thu, 28 Mar 2024 21:29:25 +0900 Subject: [PATCH] Add timeout for nosdump command --- app.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/app.rb b/app.rb index b23a713..0a62896 100644 --- a/app.rb +++ b/app.rb @@ -68,6 +68,7 @@ def validate_event(event) # This validation snippet ref. https://github.com/kaosf/nostr-backup/blob/a3afebf2b20805f3e2d5492e8c42bc76c2ecc01f/validation/run.rb require "open3" +require "timeout" def build_nostr_event(line) body = JSON.parse line @@ -79,15 +80,24 @@ def build_nostr_event(line) NostrEvent.new(id:, kind:, created_at:, body:) end +NOSDUMP_TIMEOUT_SECONDS = ENV.fetch("SINCE_MARGIN_SECONDS") { "900" }.to_i + def fetch_events(since) nostr_events = [] - Open3.popen3("nosdump", "--since", since, "--authors", *AUTHORS, *RELAYS) do |stdin, stdout, _, _| - stdin.close - stdout.each_line do |line| - nostr_events << build_nostr_event(line.chomp) - rescue StandardError => e - LOGGER.error e + begin + Timeout.timeout(NOSDUMP_TIMEOUT_SECONDS) do + Open3.popen3("nosdump", "--since", since, "--authors", *AUTHORS, *RELAYS) do |stdin, stdout, _, _| + stdin.close + stdout.each_line do |line| + nostr_events << build_nostr_event(line.chomp) + rescue StandardError => e + LOGGER.error e + end + end end + rescue Timeout::Error => e + LOGGER.error("fetch_events timeout; NOSDUMP_TIMEOUT_SECONDS: #{NOSDUMP_TIMEOUT_SECONDS} seconds\n#{e}") + nostr_events = [] end nostr_events end