From d1a4ea3d16f29f5f75cc19eaa2944ee0fdb93a1e Mon Sep 17 00:00:00 2001 From: Hugo Peixoto Date: Tue, 1 Aug 2023 14:21:01 +0100 Subject: [PATCH] Retry inserts, lazy patch to solve sqlite3 concurrency --- main.rb | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/main.rb b/main.rb index 014e164..1b1c227 100644 --- a/main.rb +++ b/main.rb @@ -20,6 +20,19 @@ def h(bytes) end end +def attempt(attempts, &block) + begin + block.call + rescue + if attempts > 1 + attempts -= 1 + retry + else + raise + end + end +end + set :public_folder, __dir__ + '/public' get '/' do downloads = ActiveRecord::Base.connection.execute(" @@ -73,12 +86,14 @@ post '/video' do data = JSON.parse(request.body.read) - Download.create!( - video: Video.find_by!(randname: data["video_id"]), - size: data["size"], - sha256: data["sha256"], - email: data["email"], - ) + attempt(10) do + Download.create!( + video: Video.find_by!(randname: data["video_id"]), + size: data["size"], + sha256: data["sha256"], + email: data["email"], + ) + end { status: 'ok' }.to_json end