From 86486a56d05f3248159a3841fd1e677f6f67c5c3 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 3 Apr 2022 14:57:43 +0100 Subject: auto-retry db queries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit might work, might make things worse… --- lib/App/MediaControl/DB.rakumod | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/App/MediaControl/DB.rakumod b/lib/App/MediaControl/DB.rakumod index f49cfab..e69efad 100644 --- a/lib/App/MediaControl/DB.rakumod +++ b/lib/App/MediaControl/DB.rakumod @@ -5,14 +5,28 @@ class App::MediaControl::DB { has DB::SQLite $.pool is required; method !db(Callable:D $code) { - my $conn = self.pool.db; - # we need an explicit LEAVE block because on 2021.10, `will - # leave { .finish }` kills precomp - LEAVE { .finish with $conn }; - $conn.begin; - $conn.execute('PRAGMA foreign_keys=true'); - KEEP { .commit with $conn }; - return $code($conn) with $conn; + sub do-it { + my $conn = self.pool.db; + # we need an explicit LEAVE block because on 2021.10, `will + # leave { .finish }` kills precomp + LEAVE { .finish with $conn }; + $conn.begin; + $conn.execute('PRAGMA foreign_keys=true'); + KEEP { .commit with $conn }; + return $code($conn) with $conn; + } + + loop { + CATCH { + when DB::SQLite::Error { + note "DB error: {$_.gist}, retrying"; + sleep 0.5; + redo; + } + } + + return do-it(); + } } method ensure-schema() { -- cgit v1.2.3