@@ -26,6 +26,7 @@ import Data.Char (isAlpha, isAscii, toUpper)
26
26
import Data.Either (fromRight )
27
27
import Data.Functor (($>) )
28
28
import Data.Ini (Ini , lookupValue , readIniFile )
29
+ import Data.Int (Int64 )
29
30
import Data.List (find , isPrefixOf )
30
31
import qualified Data.List.NonEmpty as L
31
32
import Data.Maybe (fromMaybe , isJust , isNothing )
@@ -117,7 +118,7 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
117
118
confirmOrExit
118
119
(" WARNING: message log file " <> storeMsgsFilePath <> " will be imported to journal directory " <> storeMsgsJournalDir)
119
120
" Messages not imported"
120
- ms <- newJournalMsgStore MQStoreCfg
121
+ ms <- newJournalMsgStore logPath MQStoreCfg
121
122
readQueueStore True (mkQueue ms False ) storeLogFile $ stmQueueStore ms
122
123
msgStats <- importMessages True ms storeMsgsFilePath Nothing False -- no expiration
123
124
putStrLn " Import completed"
@@ -135,7 +136,7 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
135
136
confirmOrExit
136
137
(" WARNING: journal directory " <> storeMsgsJournalDir <> " will be exported to message log file " <> storeMsgsFilePath)
137
138
" Journal not exported"
138
- ms <- newJournalMsgStore MQStoreCfg
139
+ ms <- newJournalMsgStore logPath MQStoreCfg
139
140
-- TODO [postgres] in case postgres configured, queues must be read from database
140
141
readQueueStore True (mkQueue ms False ) storeLogFile $ stmQueueStore ms
141
142
exportMessages True ms storeMsgsFilePath False
@@ -178,14 +179,7 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
178
179
confirmOrExit
179
180
(" WARNING: store log file " <> storeLogFile <> " will be compacted and imported to PostrgreSQL database: " <> B. unpack connstr <> " , schema: " <> B. unpack schema)
180
181
" Queue records not imported"
181
- ms <- newJournalMsgStore MQStoreCfg
182
- sl <- readWriteQueueStore True (mkQueue ms False ) storeLogFile (queueStore ms)
183
- closeStoreLog sl
184
- queues <- readTVarIO $ loadedQueues $ stmQueueStore ms
185
- let storeCfg = PostgresStoreCfg {dbOpts = dbOpts {createSchema = True }, dbStoreLogPath = Nothing , confirmMigrations = MCConsole , deletedTTL = iniDeletedTTL ini}
186
- ps <- newJournalMsgStore $ PQStoreCfg storeCfg
187
- qCnt <- batchInsertQueues @ (JournalQueue 'QSMemory) True queues $ postgresQueueStore ps
188
- renameFile storeLogFile $ storeLogFile <> " .bak"
182
+ qCnt <- importStoreLogToDatabase logPath storeLogFile dbOpts
189
183
putStrLn $ " Import completed: " <> show qCnt <> " queues"
190
184
putStrLn $ case readStoreType ini of
191
185
Right (ASType SQSMemory SMSMemory ) -> setToDbStr <> " \n store_messages set to `memory`, import messages to journal to use PostgreSQL database for queues (`smp-server journal import`)"
@@ -207,10 +201,7 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
207
201
confirmOrExit
208
202
(" WARNING: PostrgreSQL database schema " <> B. unpack schema <> " (database: " <> B. unpack connstr <> " ) will be exported to store log file " <> storeLogFilePath)
209
203
" Queue records not exported"
210
- let storeCfg = PostgresStoreCfg {dbOpts, dbStoreLogPath = Nothing , confirmMigrations = MCConsole , deletedTTL = iniDeletedTTL ini}
211
- ps <- newJournalMsgStore $ PQStoreCfg storeCfg
212
- sl <- openWriteStoreLog False storeLogFilePath
213
- Sum qCnt <- foldQueueRecs True True (postgresQueueStore ps) Nothing $ \ (rId, qr) -> logCreateQueue sl rId qr $> Sum (1 :: Int )
204
+ qCnt <- exportDatabaseToStoreLog logPath dbOpts storeLogFilePath
214
205
putStrLn $ " Export completed: " <> show qCnt <> " queues"
215
206
putStrLn $ case readStoreType ini of
216
207
Right (ASType SQSPostgres SMSJournal ) -> " store_queues set to `database`, update it to `memory` in INI file."
@@ -239,16 +230,12 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
239
230
(pure storeLogFile)
240
231
(putStrLn (" Store log file " <> storeLogFile <> " not found" ) >> exitFailure)
241
232
Nothing -> putStrLn " Store log disabled, see `[STORE_LOG] enable`" >> exitFailure
242
- newJournalMsgStore :: QStoreCfg s -> IO (JournalMsgStore s )
243
- newJournalMsgStore qsCfg =
244
- let cfg = mkJournalStoreConfig qsCfg storeMsgsJournalDir defaultMsgQueueQuota defaultMaxJournalMsgCount defaultMaxJournalStateLines $ checkInterval defaultMessageExpiration
245
- in newMsgStore cfg
246
233
iniFile = combine cfgPath " smp-server.ini"
247
234
serverVersion = " SMP server v" <> simplexMQVersion
248
235
executableName = " smp-server"
249
236
storeLogFilePath = combine logPath " smp-server-store.log"
250
237
storeMsgsFilePath = combine logPath " smp-server-messages.log"
251
- storeMsgsJournalDir = combine logPath " messages "
238
+ storeMsgsJournalDir = storeMsgsJournalDir' logPath
252
239
storeNtfsFilePath = combine logPath " smp-server-ntfs.log"
253
240
readStoreType :: Ini -> Either String AStoreType
254
241
readStoreType ini = case (iniStoreQueues, iniStoreMessage) of
@@ -567,8 +554,37 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
567
554
putStrLn $ " Error: both " <> storeLogFilePath <> " file and " <> B. unpack schema <> " schema are present (database: " <> B. unpack connstr <> " )."
568
555
putStrLn " Configure queue storage."
569
556
exitFailure
557
+
558
+ importStoreLogToDatabase :: FilePath -> FilePath -> DBOpts -> IO Int64
559
+ importStoreLogToDatabase logPath storeLogFile dbOpts = do
560
+ ms <- newJournalMsgStore logPath MQStoreCfg
561
+ sl <- readWriteQueueStore True (mkQueue ms False ) storeLogFile (queueStore ms)
562
+ closeStoreLog sl
563
+ queues <- readTVarIO $ loadedQueues $ stmQueueStore ms
564
+ let storeCfg = PostgresStoreCfg {dbOpts = dbOpts {createSchema = True }, dbStoreLogPath = Nothing , confirmMigrations = MCConsole , deletedTTL = 86400 * defaultDeletedTTL}
565
+ ps <- newJournalMsgStore logPath $ PQStoreCfg storeCfg
566
+ qCnt <- batchInsertQueues @ (JournalQueue 'QSMemory) True queues $ postgresQueueStore ps
567
+ renameFile storeLogFile $ storeLogFile <> " .bak"
568
+ pure qCnt
569
+
570
+ exportDatabaseToStoreLog :: FilePath -> DBOpts -> FilePath -> IO Int
571
+ exportDatabaseToStoreLog logPath dbOpts storeLogFilePath = do
572
+ let storeCfg = PostgresStoreCfg {dbOpts, dbStoreLogPath = Nothing , confirmMigrations = MCConsole , deletedTTL = 86400 * defaultDeletedTTL}
573
+ ps <- newJournalMsgStore logPath $ PQStoreCfg storeCfg
574
+ sl <- openWriteStoreLog False storeLogFilePath
575
+ Sum qCnt <- foldQueueRecs True True (postgresQueueStore ps) Nothing $ \ (rId, qr) -> logCreateQueue sl rId qr $> Sum (1 :: Int )
576
+ closeStoreLog sl
577
+ pure qCnt
570
578
#endif
571
579
580
+ newJournalMsgStore :: FilePath -> QStoreCfg s -> IO (JournalMsgStore s )
581
+ newJournalMsgStore logPath qsCfg =
582
+ let cfg = mkJournalStoreConfig qsCfg (storeMsgsJournalDir' logPath) defaultMsgQueueQuota defaultMaxJournalMsgCount defaultMaxJournalStateLines $ checkInterval defaultMessageExpiration
583
+ in newMsgStore cfg
584
+
585
+ storeMsgsJournalDir' :: FilePath -> FilePath
586
+ storeMsgsJournalDir' logPath = combine logPath " messages"
587
+
572
588
data EmbeddedWebParams = EmbeddedWebParams
573
589
{ webStaticPath :: FilePath ,
574
590
webHttpPort :: Maybe Int ,
0 commit comments