This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
40: Add asynchronous updating of offline metadata r=ksaric a=erikd I would prefer not to merge this before QA has looked at it. Co-authored-by: Erik de Castro Lopo <erikd@mega-nerd.com>
- Loading branch information
Showing
11 changed files
with
407 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-- Persistent generated migration. | ||
|
||
CREATE FUNCTION migrate() RETURNS void AS $$ | ||
DECLARE | ||
next_version int ; | ||
BEGIN | ||
SELECT stage_two + 1 INTO next_version FROM schema_version ; | ||
IF next_version = 2 THEN | ||
ALTER TABLE "pool_metadata_reference" ALTER COLUMN "pool_id" TYPE text; | ||
ALTER TABLE "pool_metadata_reference" ALTER COLUMN "url" TYPE text; | ||
ALTER TABLE "pool_metadata" ALTER COLUMN "pool_id" TYPE text; | ||
ALTER TABLE "pool_metadata" ALTER COLUMN "ticker_name" TYPE text; | ||
ALTER TABLE "pool_metadata" ALTER COLUMN "metadata" TYPE text; | ||
ALTER TABLE "pool_metadata" ADD COLUMN "pmr_id" INT8 NULL; | ||
ALTER TABLE "reserved_ticker" ALTER COLUMN "name" TYPE text; | ||
-- Hand written SQL statements can be added here. | ||
UPDATE schema_version SET stage_two = 2 ; | ||
RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; | ||
END IF ; | ||
END ; | ||
$$ LANGUAGE plpgsql ; | ||
|
||
SELECT migrate() ; | ||
|
||
DROP FUNCTION migrate() ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module FetchQueue | ||
( FetchQueue -- opaque | ||
, PoolFetchRetry (..) | ||
, Retry -- opaque | ||
, emptyFetchQueue | ||
, lenFetchQueue | ||
, nullFetchQueue | ||
, insertFetchQueue | ||
, partitionFetchQueue | ||
, newRetry | ||
, nextRetry | ||
) where | ||
|
||
|
||
import Cardano.Prelude | ||
|
||
import Data.Time.Clock.POSIX (POSIXTime) | ||
import Data.Map.Strict (Map) | ||
import qualified Data.Map.Strict as Map | ||
|
||
import Cardano.Db.Schema (PoolMetadataReferenceId) | ||
import Cardano.Db.Types (PoolId) | ||
|
||
import FetchQueue.Retry | ||
|
||
|
||
-- Unfortunately I am way too pressed for time and way too tired to make this less savage. | ||
-- Figuring out how to use an existing priority queue for this task would be more time | ||
-- consuming that writing this from scratch. | ||
|
||
newtype FetchQueue = FetchQueue (Map Text PoolFetchRetry) | ||
|
||
data PoolFetchRetry = PoolFetchRetry | ||
{ pfrReferenceId :: !PoolMetadataReferenceId | ||
, pfrPoolIdWtf :: !PoolId | ||
, pfrPoolUrl :: !Text | ||
, pfrPoolMDHash :: !ByteString | ||
, pfrRetry :: !Retry | ||
} | ||
|
||
emptyFetchQueue :: FetchQueue | ||
emptyFetchQueue = FetchQueue mempty | ||
|
||
lenFetchQueue :: FetchQueue -> Int | ||
lenFetchQueue (FetchQueue m) = Map.size m | ||
|
||
nullFetchQueue :: FetchQueue -> Bool | ||
nullFetchQueue (FetchQueue m) = Map.null m | ||
|
||
insertFetchQueue :: [PoolFetchRetry] -> FetchQueue -> FetchQueue | ||
insertFetchQueue xs (FetchQueue mp) = | ||
FetchQueue $ Map.union mp (Map.fromList $ map build xs) | ||
where | ||
build :: PoolFetchRetry -> (Text, PoolFetchRetry) | ||
build pfr = (pfrPoolUrl pfr, pfr) | ||
|
||
partitionFetchQueue :: FetchQueue -> POSIXTime -> ([PoolFetchRetry], FetchQueue) | ||
partitionFetchQueue (FetchQueue mp) now = | ||
case Map.partition isRunnable mp of | ||
(runnable, unrunnable) -> (Map.elems runnable, FetchQueue unrunnable) | ||
where | ||
isRunnable :: PoolFetchRetry -> Bool | ||
isRunnable pfr = retryWhen (pfrRetry pfr) <= now |
Oops, something went wrong.