sync ensures sites

This commit is contained in:
towards-a-new-leftypol 2025-02-04 22:22:22 -05:00
parent 1113539321
commit 430a199190
6 changed files with 58 additions and 32 deletions

View File

@ -136,6 +136,7 @@ executable chan-delorean-consoomer
Data.WordUtil Data.WordUtil
Network.DataClient Network.DataClient
Network.DataClientTypes Network.DataClientTypes
Network.GetLatestPostsPerBoardResponse
Common.Server.ConsumerSettings Common.Server.ConsumerSettings
Common.Server.JSONSettings Common.Server.JSONSettings
PriorityQueue PriorityQueue
@ -165,7 +166,8 @@ executable chan-delorean-consoomer
async, async,
temporary, temporary,
stm, stm,
random random,
transformers
-- Directories containing source files. -- Directories containing source files.
hs-source-dirs: src hs-source-dirs: src

@ -1 +1 @@
Subproject commit 0d85d44cbb5a50b4751751d5ca61707cc1b75587 Subproject commit 88b5f0df7ea5e83a65a6c6153f197da7cd1c6217

View File

@ -36,7 +36,6 @@ import qualified Data.ByteString.Lazy as B
import Data.Aeson (FromJSON) import Data.Aeson (FromJSON)
import JSONParsing import JSONParsing
import Common.Server.JSONSettings
import qualified JSONCommonTypes as JS import qualified JSONCommonTypes as JS
import qualified JSONPost as JSONPosts import qualified JSONPost as JSONPosts
import qualified Network.DataClient as Client import qualified Network.DataClient as Client
@ -45,9 +44,10 @@ import qualified BoardsType as Boards
import qualified ThreadType as Threads import qualified ThreadType as Threads
import qualified Common.AttachmentType as At import qualified Common.AttachmentType as At
import qualified Common.PostsType as Posts import qualified Common.PostsType as Posts
import qualified Hash as Hash import qualified Hash
import qualified Data.WordUtil as Words import qualified Data.WordUtil as Words
import Common.Server.JSONSettings as J import Common.Server.JSONSettings as J
import Common.Network.HttpClient (HttpError)
import qualified Common.Server.ConsumerSettings as CS import qualified Common.Server.ConsumerSettings as CS
newtype SettingsCLI = SettingsCLI newtype SettingsCLI = SettingsCLI
@ -75,10 +75,8 @@ listCatalogDirectories settings = do
doesFileExist catalogPath doesFileExist catalogPath
ensureSiteExists :: JSONSettings -> IO Sites.Site ensureSiteExists :: JSONSettings -> Either HttpError [ Sites.Site ] -> IO Sites.Site
ensureSiteExists settings = do ensureSiteExists settings sitesResult = do
sitesResult <- Client.getAllSites settings
case sitesResult of case sitesResult of
Right siteList -> Right siteList ->
case find (\site -> Sites.name site == site_name settings) siteList of case find (\site -> Sites.name site == site_name settings) siteList of
@ -696,7 +694,9 @@ processBoard settings fgs@FileGetters {..} site board = do
getBoards :: JSONSettings -> [ FilePath ] -> IO (Sites.Site, [ Boards.Board ]) getBoards :: JSONSettings -> [ FilePath ] -> IO (Sites.Site, [ Boards.Board ])
getBoards settings board_names = do getBoards settings board_names = do
site :: Sites.Site <- ensureSiteExists settings sitesResult <- Client.getAllSites settings
site :: Sites.Site <- ensureSiteExists settings sitesResult
let boardsSet = Set.fromList board_names let boardsSet = Set.fromList board_names
let site_id_ = Sites.site_id site let site_id_ = Sites.site_id site
boards_result <- Client.getSiteBoards settings site_id_ boards_result <- Client.getSiteBoards settings site_id_

View File

@ -1,4 +1,6 @@
{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use when" #-}
module Main (main) where module Main (main) where
@ -6,7 +8,7 @@ import System.Exit (exitFailure)
import qualified Data.ByteString.Lazy as B import qualified Data.ByteString.Lazy as B
import System.Console.CmdArgs (cmdArgs, Data, Typeable) import System.Console.CmdArgs (cmdArgs, Data, Typeable)
import Data.Aeson (decode) import Data.Aeson (decode)
import Control.Concurrent.Async (mapConcurrently) import Control.Concurrent.Async (mapConcurrently_)
import Common.Server.ConsumerSettings import Common.Server.ConsumerSettings
import Lib import Lib
@ -53,12 +55,15 @@ main = do
_ <- if http_fill_all settings _ <- if http_fill_all settings
then do then do
putStrLn "Starting web backfill" putStrLn "Starting web backfill"
mapConcurrently (processWebsite settings) (websites settings) mapConcurrently_ (processWebsite settings) (websites settings)
else return [] putStrLn "Finished web backfill"
if http_sync_continously settings
then syncWebsites settings
else return () else return ()
putStrLn "Done" if http_sync_continously settings
then do
putStrLn "Starting web sync loop"
syncWebsites settings
else return ()
putStrLn "Done. Quitting."

View File

@ -222,7 +222,7 @@ eitherDecodeResponse (Left err) = Left err
eitherDecodeResponse (Right bs) = eitherDecodeResponse (Right bs) =
case eitherDecode bs of case eitherDecode bs of
Right val -> Right val Right val -> Right val
Left err -> Left $ StatusCodeError 500 $ LC8.pack $ "Failed to decode JSON: " ++ err ++ " " ++ (show bs) Left err -> Left $ StatusCodeError 500 $ LC8.pack $ "Failed to decode JSON: " ++ err ++ " " ++ show bs
getJSON :: (FromJSON a) => String -> IO (Either HttpError a) getJSON :: (FromJSON a) => String -> IO (Either HttpError a)
@ -250,7 +250,6 @@ getFile url = do
return $ Just tmp_filepath return $ Just tmp_filepath
-- | Function to handle each chunk.
getLatestPostsPerBoard :: T.JSONSettings -> IO (Either HttpError [ GLPPBR.GetLatestPostsPerBoardResponse ]) getLatestPostsPerBoard :: T.JSONSettings -> IO (Either HttpError [ GLPPBR.GetLatestPostsPerBoardResponse ])
getLatestPostsPerBoard settings = getLatestPostsPerBoard settings =
post settings "/rpc/get_latest_posts_per_board" mempty False >>= return . eitherDecodeResponse post settings "/rpc/get_latest_posts_per_board" mempty False >>= return . eitherDecodeResponse

View File

@ -2,32 +2,40 @@
module Sync where module Sync where
import Common.Server.ConsumerSettings as Settings import qualified Common.Server.ConsumerSettings as S
import Common.Server.JSONSettings as JSONSettings import qualified Common.Server.JSONSettings as JS
import Network.DataClient (getLatestPostsPerBoard) import qualified Network.DataClient as Client
import qualified Lib
import Control.Monad.Trans.Except (ExceptT, runExceptT)
consumerSettingsToPartialJSONSettings :: Settings.ConsumerJSONSettings -> JSONSettings.JSONSettings consumerSettingsToPartialJSONSettings :: S.ConsumerJSONSettings -> JS.JSONSettings
consumerSettingsToPartialJSONSettings ConsumerJSONSettings {..} = consumerSettingsToPartialJSONSettings S.ConsumerJSONSettings {..} =
JSONSettings JS.JSONSettings
{ JSONSettings.postgrest_url = postgrest_url { JS.postgrest_url = postgrest_url
, JSONSettings.jwt = jwt , JS.jwt = jwt
, backup_read_root = undefined , backup_read_root = undefined
, JSONSettings.media_root_path , JS.media_root_path
, site_name = undefined , site_name = undefined
, site_url = undefined , site_url = undefined
} }
syncWebsites :: ConsumerJSONSettings -> IO () syncWebsites :: S.ConsumerJSONSettings -> IO ()
syncWebsites consumer_settings = do syncWebsites consumer_settings = do
putStrLn "Starting channel web synchronization." putStrLn "Starting channel web synchronization."
let json_settings = consumerSettingsToPartialJSONSettings consumer_settings let json_settings = consumerSettingsToPartialJSONSettings consumer_settings
asdf <- getLatestPostsPerBoard json_settings sitesResult <- Client.getAllSites json_settings
print asdf sites <- mapM (flip Lib.ensureSiteExists sitesResult . Lib.toClientSettings consumer_settings) (S.websites consumer_settings)
-- first we need all the (Site, Board) tuples
-- perhaps we even want all (Site, Board, Thread) pairs print sites
latest_posts_per_board <- Client.getLatestPostsPerBoard json_settings
print latest_posts_per_board
-- first we need all the (Site, Board) tuples ✓
-- perhaps we even want all (Site, Board, Thread) ✓
-- But then we don't load the posts of each thread, instead only do -- But then we don't load the posts of each thread, instead only do
-- that for threads which change, -- that for threads which change,
-- - which means after we get all the threads -- - which means after we get all the threads
@ -37,3 +45,15 @@ syncWebsites consumer_settings = do
-- - load only the changed/new ones -- - load only the changed/new ones
-- - put board back -- - put board back
-- NEW TODO:
-- - ensure that sites in the settings exist in the database! ✓
-- - ensure that boards per site in the settings exist in the database!
-- - finish using ExceptT and use sites, latest_posts_per_board to populate
-- our PriorityQueue
-- - write event loop that
-- - get pq from stm shared value
-- - uses the pq (there was something about the timestamps in the pq having to be reversed btw)
-- - ensures threads
-- - has a value that should be added to the pq
-- - uses stm to update pq shared value