2020-05-02 00:30:54 +03:00
|
|
|
{- |
|
|
|
|
# Signal Handling (SIGTERM, SIGINT)
|
|
|
|
|
|
|
|
We handle SIGTERM by causing the main thread to raise a `UserInterrupt`
|
|
|
|
exception. This is the same behavior as SIGINT (the signal sent upon
|
|
|
|
`CTRL-C`).
|
|
|
|
|
|
|
|
The main thread is therefore responsible for handling this exception
|
|
|
|
and causing everything to shut down properly.
|
|
|
|
|
|
|
|
# Crashing and Shutting Down
|
|
|
|
|
|
|
|
Rule number one: The King never crashes.
|
|
|
|
|
|
|
|
This rule is asperational at the moment, but it needs to become as
|
|
|
|
close to truth as possible. Shut down ships in extreme cases, but
|
|
|
|
never let the king go down.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-
|
|
|
|
TODO These some old scribbled notes. They don't belong here
|
|
|
|
anymore. Do something about it.
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
# Event Pruning
|
|
|
|
|
|
|
|
- `king discard-events NUM_EVENTS`: Delete the last `n` events from
|
|
|
|
the event log.
|
|
|
|
|
|
|
|
- `king discard-events-interactive`: Iterate through the events in
|
|
|
|
the event log, from last to first, pretty-print each event, and
|
|
|
|
ask if it should be pruned.
|
|
|
|
|
|
|
|
# Implement subcommands to test event and effect parsing.
|
|
|
|
|
|
|
|
- `king * --collect-fx`: All effects that come from the serf get
|
|
|
|
written into the `effects` LMDB database.
|
|
|
|
|
|
|
|
- `king clear-fx PIER`: Deletes all collected effects.
|
|
|
|
|
|
|
|
- `king full-replay PIER`: Replays the whole event log events, print
|
|
|
|
any failures. On success, replace the snapshot.
|
|
|
|
|
|
|
|
|
|
|
|
# Full Replay -- An Integration Test
|
|
|
|
|
|
|
|
- Copy the event log:
|
|
|
|
|
|
|
|
- Create a new event log at the destination.
|
|
|
|
- Stream events from the first event log.
|
|
|
|
- Parse each event.
|
|
|
|
- Re-Serialize each event.
|
|
|
|
- Verify that the round-trip was successful.
|
|
|
|
- Write the event into the new database.
|
|
|
|
|
|
|
|
- Replay the event log at the destination.
|
|
|
|
- If `--collect-fx` is set, then record effects as well.
|
|
|
|
|
|
|
|
- Snapshot.
|
|
|
|
|
|
|
|
- Verify that the final mug is the same as it was before.
|
|
|
|
|
|
|
|
# Implement Remaining Serf Flags
|
|
|
|
|
|
|
|
- `DebugRam`: Memory debugging.
|
|
|
|
- `DebugCpu`: Profiling
|
|
|
|
- `CheckCorrupt`: Heap Corruption Tests
|
|
|
|
- `CheckFatal`: TODO What is this?
|
|
|
|
- `Verbose`: TODO Just the `-v` flag?
|
|
|
|
- `DryRun`: TODO Just the `-N` flag?
|
|
|
|
- `Quiet`: TODO Just the `-q` flag?
|
|
|
|
- `Hashless`: Don't use hashboard for jets.
|
|
|
|
-}
|
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
module Urbit.King.Main (main) where
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Prelude
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
import Data.Conduit
|
|
|
|
import Network.HTTP.Client.TLS
|
|
|
|
import RIO.Directory
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Arvo
|
|
|
|
import Urbit.King.Config
|
|
|
|
import Urbit.Vere.Dawn
|
|
|
|
import Urbit.Vere.Pier
|
2020-08-05 22:33:37 +03:00
|
|
|
import Urbit.Vere.Ports
|
2020-08-04 23:52:40 +03:00
|
|
|
import Urbit.Vere.Eyre.Multi (multiEyre, MultiEyreConf(..))
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Vere.Pier.Types
|
|
|
|
import Urbit.Vere.Serf
|
2020-06-11 00:17:55 +03:00
|
|
|
import Urbit.King.App
|
2020-01-24 08:28:38 +03:00
|
|
|
|
2020-01-24 09:23:43 +03:00
|
|
|
import Control.Concurrent (myThreadId)
|
2020-01-24 08:37:51 +03:00
|
|
|
import Control.Exception (AsyncException(UserInterrupt))
|
|
|
|
import Control.Lens ((&))
|
2020-01-24 08:47:06 +03:00
|
|
|
import System.Process (system)
|
2020-01-24 08:37:51 +03:00
|
|
|
import Text.Show.Pretty (pPrint)
|
|
|
|
import Urbit.Noun.Conversions (cordToUW)
|
2020-06-08 20:24:05 +03:00
|
|
|
import Urbit.Noun.Time (Wen)
|
2020-01-24 08:37:51 +03:00
|
|
|
import Urbit.Vere.LockFile (lockFile)
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-02-04 04:27:16 +03:00
|
|
|
import qualified Data.Set as Set
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Network.HTTP.Client as C
|
|
|
|
import qualified System.Posix.Signals as Sys
|
|
|
|
import qualified System.ProgressBar as PB
|
|
|
|
import qualified System.Random as Sys
|
2020-06-09 00:27:58 +03:00
|
|
|
import qualified Urbit.EventLog.LMDB as Log
|
2020-02-04 04:27:16 +03:00
|
|
|
import qualified Urbit.King.CLI as CLI
|
|
|
|
import qualified Urbit.King.EventBrowser as EventBrowser
|
|
|
|
import qualified Urbit.Ob as Ob
|
|
|
|
import qualified Urbit.Vere.Pier as Pier
|
|
|
|
import qualified Urbit.Vere.Serf as Serf
|
|
|
|
import qualified Urbit.Vere.Term as Term
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
removeFileIfExists :: HasLogFunc env => FilePath -> RIO env ()
|
|
|
|
removeFileIfExists pax = do
|
|
|
|
exists <- doesFileExist pax
|
|
|
|
when exists $ do
|
|
|
|
removeFile pax
|
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
|
2020-06-11 02:41:09 +03:00
|
|
|
-- Compile CLI Flags to Pier Configuration -------------------------------------
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-06-11 02:41:09 +03:00
|
|
|
{-
|
|
|
|
TODO: This is not all of the flags.
|
|
|
|
Urbit is basically useless with hashboard, so we ignore that flag.
|
|
|
|
-}
|
2020-05-27 02:01:03 +03:00
|
|
|
toSerfFlags :: CLI.Opts -> [Serf.Flag]
|
2020-01-23 07:16:09 +03:00
|
|
|
toSerfFlags CLI.Opts{..} = catMaybes m
|
|
|
|
where
|
2020-06-11 02:41:09 +03:00
|
|
|
m = [ setFrom oQuiet Serf.Quiet
|
|
|
|
, setFrom oTrace Serf.Trace
|
|
|
|
, setFrom (oHashless || True) Serf.Hashless
|
|
|
|
, setFrom oQuiet Serf.Quiet
|
|
|
|
, setFrom oVerbose Serf.Verbose
|
|
|
|
, setFrom (oDryRun || isJust oDryFrom) Serf.DryRun
|
2020-01-23 07:16:09 +03:00
|
|
|
]
|
2020-06-11 02:41:09 +03:00
|
|
|
setFrom True flag = Just flag
|
|
|
|
setFrom False _ = Nothing
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
toPierConfig :: FilePath -> CLI.Opts -> PierConfig
|
2020-06-11 02:41:09 +03:00
|
|
|
toPierConfig pierPath o@(CLI.Opts{..}) = PierConfig { .. }
|
2020-03-03 02:24:28 +03:00
|
|
|
where
|
2020-06-11 02:41:09 +03:00
|
|
|
_pcPierPath = pierPath
|
|
|
|
_pcDryRun = oDryRun || isJust oDryFrom
|
|
|
|
_pcSerfExe = fromMaybe "urbit-worker" oSerfExe
|
|
|
|
_pcSerfFlags = toSerfFlags o
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
toNetworkConfig :: CLI.Opts -> NetworkConfig
|
2020-03-03 02:24:28 +03:00
|
|
|
toNetworkConfig CLI.Opts {..} = NetworkConfig { .. }
|
|
|
|
where
|
|
|
|
dryRun = oDryRun || isJust oDryFrom
|
|
|
|
offline = dryRun || oOffline
|
|
|
|
|
|
|
|
mode = case (dryRun, offline, oLocalhost) of
|
|
|
|
(True, _ , _ ) -> NMNone
|
|
|
|
(_ , True, _ ) -> NMNone
|
|
|
|
(_ , _ , True) -> NMLocalhost
|
|
|
|
(_ , _ , _ ) -> NMNormal
|
|
|
|
|
|
|
|
_ncNetMode = mode
|
|
|
|
_ncAmesPort = oAmesPort
|
|
|
|
_ncHttpPort = oHttpPort
|
|
|
|
_ncHttpsPort = oHttpsPort
|
|
|
|
_ncLocalPort = oLoopbackPort
|
2020-05-13 02:55:49 +03:00
|
|
|
_ncNoAmes = oNoAmes
|
|
|
|
_ncNoHttp = oNoHttp
|
|
|
|
_ncNoHttps = oNoHttps
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-06-11 00:17:55 +03:00
|
|
|
logStderr :: HasStderrLogFunc e => RIO LogFunc a -> RIO e a
|
|
|
|
logStderr action = do
|
|
|
|
logFunc <- view stderrLogFuncL
|
|
|
|
runRIO logFunc action
|
|
|
|
|
|
|
|
logSlogs :: HasStderrLogFunc e => RIO e (TVar (Text -> IO ()))
|
|
|
|
logSlogs = logStderr $ do
|
2020-05-28 21:56:51 +03:00
|
|
|
env <- ask
|
2020-06-11 00:17:55 +03:00
|
|
|
newTVarIO (runRIO env . logOther "serf" . display . T.strip)
|
2020-05-28 21:56:51 +03:00
|
|
|
|
2020-05-13 22:35:57 +03:00
|
|
|
tryBootFromPill
|
|
|
|
:: Bool
|
|
|
|
-> Pill
|
|
|
|
-> Bool
|
|
|
|
-> Ship
|
|
|
|
-> LegacyBootEvent
|
|
|
|
-> RIO PierEnv ()
|
2020-08-04 23:52:40 +03:00
|
|
|
tryBootFromPill oExit pill lite ship boot = do
|
2020-06-07 02:03:20 +03:00
|
|
|
mStart <- newEmptyMVar
|
|
|
|
vSlog <- logSlogs
|
2020-08-04 23:52:40 +03:00
|
|
|
runOrExitImmediately vSlog (bootedPier vSlog) oExit mStart
|
2020-06-07 02:03:20 +03:00
|
|
|
where
|
|
|
|
bootedPier vSlog = do
|
|
|
|
view pierPathL >>= lockFile
|
2020-06-11 05:02:09 +03:00
|
|
|
rio $ logDebug "Starting boot"
|
2020-06-11 02:41:09 +03:00
|
|
|
sls <- Pier.booted vSlog pill lite ship boot
|
2020-06-11 05:02:09 +03:00
|
|
|
rio $ logDebug "Completed boot"
|
2020-06-07 02:03:20 +03:00
|
|
|
pure sls
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-27 02:01:03 +03:00
|
|
|
runOrExitImmediately
|
2020-05-30 03:15:16 +03:00
|
|
|
:: TVar (Text -> IO ())
|
|
|
|
-> RAcquire PierEnv (Serf, Log.EventLog)
|
2020-05-27 02:01:03 +03:00
|
|
|
-> Bool
|
|
|
|
-> MVar ()
|
2020-05-13 22:35:57 +03:00
|
|
|
-> RIO PierEnv ()
|
2020-08-04 23:52:40 +03:00
|
|
|
runOrExitImmediately vSlog getPier oExit mStart = do
|
2020-05-27 02:01:03 +03:00
|
|
|
rwith getPier (if oExit then shutdownImmediately else runPier)
|
|
|
|
where
|
2020-05-30 03:15:16 +03:00
|
|
|
shutdownImmediately :: (Serf, Log.EventLog) -> RIO PierEnv ()
|
2020-05-27 02:01:03 +03:00
|
|
|
shutdownImmediately (serf, log) = do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Sending shutdown signal"
|
2020-06-01 20:51:37 +03:00
|
|
|
Serf.stop serf
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Shutdown!"
|
2020-05-27 02:01:03 +03:00
|
|
|
|
2020-05-30 03:15:16 +03:00
|
|
|
runPier :: (Serf, Log.EventLog) -> RIO PierEnv ()
|
2020-05-27 02:01:03 +03:00
|
|
|
runPier serfLog = do
|
2020-08-04 23:52:40 +03:00
|
|
|
runRAcquire (Pier.pier serfLog vSlog mStart)
|
2020-05-11 23:42:30 +03:00
|
|
|
|
|
|
|
tryPlayShip
|
2020-05-13 22:35:57 +03:00
|
|
|
:: Bool
|
2020-05-11 23:42:30 +03:00
|
|
|
-> Bool
|
|
|
|
-> Maybe Word64
|
|
|
|
-> MVar ()
|
2020-05-13 22:35:57 +03:00
|
|
|
-> RIO PierEnv ()
|
2020-08-04 23:52:40 +03:00
|
|
|
tryPlayShip exitImmediately fullReplay playFrom mStart = do
|
2020-05-22 21:12:28 +03:00
|
|
|
when fullReplay wipeSnapshot
|
2020-05-30 03:15:16 +03:00
|
|
|
vSlog <- logSlogs
|
2020-08-04 23:52:40 +03:00
|
|
|
runOrExitImmediately vSlog (resumeShip vSlog) exitImmediately mStart
|
2020-05-22 21:12:28 +03:00
|
|
|
where
|
|
|
|
wipeSnapshot = do
|
|
|
|
shipPath <- view pierPathL
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "wipeSnapshot"
|
2020-05-22 21:12:28 +03:00
|
|
|
logDebug $ display $ pack @Text ("Wiping " <> north shipPath)
|
|
|
|
logDebug $ display $ pack @Text ("Wiping " <> south shipPath)
|
|
|
|
removeFileIfExists (north shipPath)
|
|
|
|
removeFileIfExists (south shipPath)
|
|
|
|
|
|
|
|
north shipPath = shipPath <> "/.urb/chk/north.bin"
|
|
|
|
south shipPath = shipPath <> "/.urb/chk/south.bin"
|
|
|
|
|
2020-05-30 03:15:16 +03:00
|
|
|
resumeShip :: TVar (Text -> IO ()) -> RAcquire PierEnv (Serf, Log.EventLog)
|
|
|
|
resumeShip vSlog = do
|
2020-05-22 21:12:28 +03:00
|
|
|
view pierPathL >>= lockFile
|
2020-06-11 05:02:09 +03:00
|
|
|
rio $ logDebug "RESUMING SHIP"
|
2020-06-11 02:41:09 +03:00
|
|
|
sls <- Pier.resumed vSlog playFrom
|
2020-06-11 05:02:09 +03:00
|
|
|
rio $ logDebug "SHIP RESUMED"
|
2020-05-22 21:12:28 +03:00
|
|
|
pure sls
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
runRAcquire :: (MonadUnliftIO (m e), MonadIO (m e), MonadReader e (m e))
|
|
|
|
=> RAcquire e a -> m e a
|
|
|
|
runRAcquire act = rwith act pure
|
|
|
|
|
2020-06-11 02:41:09 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-05-22 21:12:28 +03:00
|
|
|
checkEvs :: FilePath -> Word64 -> Word64 -> RIO KingEnv ()
|
2020-01-23 07:16:09 +03:00
|
|
|
checkEvs pierPath first last = do
|
2020-05-22 21:12:28 +03:00
|
|
|
rwith (Log.existing logPath) $ \log -> do
|
|
|
|
let ident = Log.identity log
|
|
|
|
let pbSty = PB.defStyle { PB.stylePostfix = PB.exact }
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug (displayShow ident)
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-06-03 02:03:04 +03:00
|
|
|
last <- atomically $ Log.lastEv log <&> \lastReal -> min last lastReal
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-22 21:12:28 +03:00
|
|
|
let evCount = fromIntegral (last - first)
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-22 21:12:28 +03:00
|
|
|
pb <- PB.newProgressBar pbSty 10 (PB.Progress 1 evCount ())
|
|
|
|
|
|
|
|
runConduit $ Log.streamEvents log first .| showEvents
|
|
|
|
pb
|
|
|
|
first
|
|
|
|
(fromIntegral $ lifecycleLen ident)
|
|
|
|
where
|
|
|
|
logPath :: FilePath
|
|
|
|
logPath = pierPath <> "/.urb/log"
|
|
|
|
|
|
|
|
showEvents
|
|
|
|
:: PB.ProgressBar ()
|
|
|
|
-> EventId
|
|
|
|
-> EventId
|
|
|
|
-> ConduitT ByteString Void (RIO KingEnv) ()
|
|
|
|
showEvents pb eId _ | eId > last = pure ()
|
|
|
|
showEvents pb eId cycle = await >>= \case
|
|
|
|
Nothing -> do
|
|
|
|
lift $ PB.killProgressBar pb
|
2020-06-11 05:02:09 +03:00
|
|
|
lift $ logDebug "Everything checks out."
|
2020-05-22 21:12:28 +03:00
|
|
|
Just bs -> do
|
|
|
|
lift $ PB.incProgress pb 1
|
|
|
|
lift $ do
|
|
|
|
n <- io $ cueBSExn bs
|
|
|
|
when (eId > cycle) $ do
|
|
|
|
(mug, wen, evNoun) <- unpackJob n
|
|
|
|
fromNounErr evNoun & \case
|
|
|
|
Left err -> logError (displayShow (eId, err))
|
|
|
|
Right (_ :: Ev) -> pure ()
|
|
|
|
showEvents pb (succ eId) cycle
|
|
|
|
|
|
|
|
unpackJob :: Noun -> RIO KingEnv (Mug, Wen, Noun)
|
|
|
|
unpackJob = io . fromNounExn
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-06-11 02:41:09 +03:00
|
|
|
collectAllFx :: FilePath -> RIO KingEnv ()
|
|
|
|
collectAllFx = error "TODO"
|
|
|
|
|
|
|
|
{-
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
This runs the serf at `$top/.tmpdir`, but we disable snapshots,
|
|
|
|
so this should never actually be created. We just do this to avoid
|
|
|
|
letting the serf use an existing snapshot.
|
|
|
|
-}
|
2020-05-22 21:12:28 +03:00
|
|
|
collectAllFx :: FilePath -> RIO KingEnv ()
|
2020-01-23 07:16:09 +03:00
|
|
|
collectAllFx top = do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug $ display $ pack @Text top
|
2020-05-28 21:56:51 +03:00
|
|
|
vSlog <- logSlogs
|
|
|
|
rwith (collectedFX vSlog) $ \() ->
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Done collecting effects!"
|
2020-01-23 07:16:09 +03:00
|
|
|
where
|
|
|
|
tmpDir :: FilePath
|
2020-01-25 03:20:13 +03:00
|
|
|
tmpDir = top </> ".tmpdir"
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-30 03:15:16 +03:00
|
|
|
collectedFX :: TVar (Text -> IO ()) -> RAcquire KingEnv ()
|
2020-05-28 21:56:51 +03:00
|
|
|
collectedFX vSlog = do
|
2020-01-23 07:16:09 +03:00
|
|
|
lockFile top
|
|
|
|
log <- Log.existing (top <> "/.urb/log")
|
2020-05-28 21:56:51 +03:00
|
|
|
serf <- Pier.runSerf vSlog tmpDir serfFlags
|
2020-06-05 00:10:33 +03:00
|
|
|
rio $ Serf.collectFX serf log
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-27 02:01:03 +03:00
|
|
|
serfFlags :: [Serf.Flag]
|
2020-01-23 07:16:09 +03:00
|
|
|
serfFlags = [Serf.Hashless, Serf.DryRun]
|
2020-06-11 02:41:09 +03:00
|
|
|
-}
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-06-03 02:03:04 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-05-22 21:12:28 +03:00
|
|
|
replayPartEvs :: FilePath -> Word64 -> RIO KingEnv ()
|
2020-01-24 05:57:22 +03:00
|
|
|
replayPartEvs top last = do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug $ display $ pack @Text top
|
2020-01-24 05:57:22 +03:00
|
|
|
fetchSnapshot
|
|
|
|
rwith replayedEvs $ \() ->
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Done replaying events!"
|
2020-01-24 05:57:22 +03:00
|
|
|
where
|
2020-05-22 21:12:28 +03:00
|
|
|
fetchSnapshot :: RIO KingEnv ()
|
2020-01-24 05:57:22 +03:00
|
|
|
fetchSnapshot = do
|
|
|
|
snap <- Pier.getSnapshot top last
|
|
|
|
case snap of
|
|
|
|
Nothing -> pure ()
|
|
|
|
Just sn -> do
|
|
|
|
liftIO $ system $ "cp -r \"" <> sn <> "\" \"" <> tmpDir <> "\""
|
|
|
|
pure ()
|
|
|
|
|
|
|
|
tmpDir :: FilePath
|
2020-01-25 03:20:13 +03:00
|
|
|
tmpDir = top </> ".partial-replay" </> show last
|
2020-01-24 05:57:22 +03:00
|
|
|
|
2020-05-22 21:12:28 +03:00
|
|
|
replayedEvs :: RAcquire KingEnv ()
|
2020-01-24 05:57:22 +03:00
|
|
|
replayedEvs = do
|
|
|
|
lockFile top
|
|
|
|
log <- Log.existing (top <> "/.urb/log")
|
2020-05-27 02:01:03 +03:00
|
|
|
let onSlog = print
|
|
|
|
let onStdr = print
|
|
|
|
let onDead = error "DIED"
|
|
|
|
let config = Serf.Config "urbit-worker" tmpDir serfFlags onSlog onStdr onDead
|
|
|
|
(serf, info) <- io (Serf.start config)
|
2020-01-24 05:57:22 +03:00
|
|
|
rio $ do
|
2020-05-27 02:01:03 +03:00
|
|
|
eSs <- Serf.execReplay serf log (Just last)
|
|
|
|
case eSs of
|
2020-06-08 20:45:41 +03:00
|
|
|
Left bail -> error (show bail)
|
|
|
|
Right 0 -> io (Serf.snapshot serf)
|
|
|
|
Right num -> pure ()
|
2020-01-24 05:57:22 +03:00
|
|
|
io $ threadDelay 500000 -- Copied from runOrExitImmediately
|
|
|
|
pure ()
|
|
|
|
|
2020-05-27 02:01:03 +03:00
|
|
|
serfFlags :: [Serf.Flag]
|
2020-01-24 05:57:22 +03:00
|
|
|
serfFlags = [Serf.Hashless]
|
|
|
|
|
2020-06-03 02:03:04 +03:00
|
|
|
|
2020-01-24 05:57:22 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Interesting
|
|
|
|
-}
|
|
|
|
testPill :: HasLogFunc e => FilePath -> Bool -> Bool -> RIO e ()
|
|
|
|
testPill pax showPil showSeq = do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Reading pill file."
|
2020-01-23 07:16:09 +03:00
|
|
|
pillBytes <- readFile pax
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Cueing pill file."
|
2020-01-23 07:16:09 +03:00
|
|
|
pillNoun <- io $ cueBS pillBytes & either throwIO pure
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Parsing pill file."
|
2020-01-23 07:16:09 +03:00
|
|
|
pill <- fromNounErr pillNoun & either (throwIO . uncurry ParseErr) pure
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Using pill to generate boot sequence."
|
2020-06-07 02:34:27 +03:00
|
|
|
bootSeq <- genBootSeq (Ship 0) pill False (Fake (Ship 0))
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Validate jam/cue and toNoun/fromNoun on pill value"
|
2020-01-23 07:16:09 +03:00
|
|
|
reJam <- validateNounVal pill
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Checking if round-trip matches input file:"
|
2020-01-23 07:16:09 +03:00
|
|
|
unless (reJam == pillBytes) $ do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug " Our jam does not match the file...\n"
|
|
|
|
logDebug " This is surprising, but it is probably okay."
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
when showPil $ do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "\n\n== Pill ==\n"
|
2020-01-23 07:16:09 +03:00
|
|
|
io $ pPrint pill
|
|
|
|
|
|
|
|
when showSeq $ do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "\n\n== Boot Sequence ==\n"
|
2020-01-23 07:16:09 +03:00
|
|
|
io $ pPrint bootSeq
|
|
|
|
|
|
|
|
validateNounVal :: (HasLogFunc e, Eq a, ToNoun a, FromNoun a)
|
|
|
|
=> a -> RIO e ByteString
|
|
|
|
validateNounVal inpVal = do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug " jam"
|
2020-01-23 07:16:09 +03:00
|
|
|
inpByt <- evaluate $ jamBS $ toNoun inpVal
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug " cue"
|
2020-01-23 07:16:09 +03:00
|
|
|
outNon <- cueBS inpByt & either throwIO pure
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug " fromNoun"
|
2020-01-23 07:16:09 +03:00
|
|
|
outVal <- fromNounErr outNon & either (throwIO . uncurry ParseErr) pure
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug " toNoun"
|
2020-01-23 07:16:09 +03:00
|
|
|
outNon <- evaluate (toNoun outVal)
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug " jam"
|
2020-01-23 07:16:09 +03:00
|
|
|
outByt <- evaluate $ jamBS outNon
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Checking if: x == cue (jam x)"
|
2020-01-23 07:16:09 +03:00
|
|
|
unless (inpVal == outVal) $
|
|
|
|
error "Value fails test: x == cue (jam x)"
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug "Checking if: jam x == jam (cue (jam x))"
|
2020-01-23 07:16:09 +03:00
|
|
|
unless (inpByt == outByt) $
|
|
|
|
error "Value fails test: jam x == jam (cue (jam x))"
|
|
|
|
|
|
|
|
pure outByt
|
|
|
|
|
2020-06-03 02:03:04 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-08-04 23:52:40 +03:00
|
|
|
pillFrom :: CLI.PillSource -> RIO RunningEnv Pill
|
2020-05-13 22:06:32 +03:00
|
|
|
pillFrom = \case
|
|
|
|
CLI.PillSourceFile pillPath -> do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug $ display $ "boot: reading pill from " ++ (pack pillPath :: Text)
|
2020-05-13 22:06:32 +03:00
|
|
|
io (loadFile pillPath >>= either throwIO pure)
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-13 22:06:32 +03:00
|
|
|
CLI.PillSourceURL url -> do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug $ display $ "boot: retrieving pill from " ++ (pack url :: Text)
|
2020-05-13 22:06:32 +03:00
|
|
|
-- Get the jamfile with the list of stars accepting comets right now.
|
|
|
|
manager <- io $ C.newManager tlsManagerSettings
|
|
|
|
request <- io $ C.parseRequest url
|
|
|
|
response <- io $ C.httpLbs (C.setRequestCheckStatus request) manager
|
|
|
|
let body = toStrict $ C.responseBody response
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-13 22:06:32 +03:00
|
|
|
noun <- cueBS body & either throwIO pure
|
|
|
|
fromNounErr noun & either (throwIO . uncurry ParseErr) pure
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-13 22:35:57 +03:00
|
|
|
newShip :: CLI.New -> CLI.Opts -> RIO KingEnv ()
|
2020-05-13 21:29:50 +03:00
|
|
|
newShip CLI.New{..} opts = do
|
|
|
|
{-
|
|
|
|
TODO XXX HACK
|
|
|
|
|
|
|
|
Because the "new ship" flow *may* automatically start the ship,
|
|
|
|
we need to create this, but it's not actually correct.
|
2020-05-11 23:42:30 +03:00
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
The right solution is to separate out the "new ship" flow from the
|
|
|
|
"run ship" flow, and possibly sequence them from the outside if
|
|
|
|
that's really needed.
|
|
|
|
-}
|
|
|
|
multi <- multiEyre (MultiEyreConf Nothing Nothing True)
|
|
|
|
|
2020-08-05 22:33:37 +03:00
|
|
|
-- TODO: We hit the same problem as above: we need the running options to
|
|
|
|
-- determine how to configure the ports
|
2020-08-07 20:04:57 +03:00
|
|
|
let ports = buildInactivePorts
|
2020-08-05 22:33:37 +03:00
|
|
|
|
2020-08-04 23:52:40 +03:00
|
|
|
-- here we are with a king env, and we now need a multi env.
|
2020-08-05 22:33:37 +03:00
|
|
|
runRunningEnv multi ports go
|
2020-01-23 07:16:09 +03:00
|
|
|
where
|
2020-08-04 23:52:40 +03:00
|
|
|
go :: RIO RunningEnv ()
|
|
|
|
go = case nBootType of
|
|
|
|
CLI.BootComet -> do
|
|
|
|
pill <- pillFrom nPillSource
|
|
|
|
putStrLn "boot: retrieving list of stars currently accepting comets"
|
|
|
|
starList <- dawnCometList
|
|
|
|
putStrLn ("boot: " ++ (tshow $ length starList) ++
|
|
|
|
" star(s) currently accepting comets")
|
|
|
|
putStrLn "boot: mining a comet"
|
|
|
|
eny <- io $ Sys.randomIO
|
|
|
|
let seed = mineComet (Set.fromList starList) eny
|
|
|
|
putStrLn ("boot: found comet " ++ renderShip (sShip seed))
|
|
|
|
bootFromSeed pill seed
|
|
|
|
|
|
|
|
CLI.BootFake name -> do
|
|
|
|
pill <- pillFrom nPillSource
|
|
|
|
ship <- shipFrom name
|
|
|
|
runTryBootFromPill pill name ship (Fake ship)
|
|
|
|
|
|
|
|
CLI.BootFromKeyfile keyFile -> do
|
|
|
|
text <- readFileUtf8 keyFile
|
|
|
|
asAtom <- case cordToUW (Cord $ T.strip text) of
|
|
|
|
Nothing -> error "Couldn't parse keyfile. Hint: keyfiles start with 0w?"
|
|
|
|
Just (UW a) -> pure a
|
|
|
|
|
|
|
|
asNoun <- cueExn asAtom
|
|
|
|
seed :: Seed <- case fromNoun asNoun of
|
|
|
|
Nothing -> error "Keyfile does not seem to contain a seed."
|
|
|
|
Just s -> pure s
|
|
|
|
|
|
|
|
pill <- pillFrom nPillSource
|
|
|
|
|
|
|
|
bootFromSeed pill seed
|
|
|
|
|
|
|
|
shipFrom :: Text -> RIO RunningEnv Ship
|
2020-01-23 07:16:09 +03:00
|
|
|
shipFrom name = case Ob.parsePatp name of
|
|
|
|
Left x -> error "Invalid ship name"
|
|
|
|
Right p -> pure $ Ship $ fromIntegral $ Ob.fromPatp p
|
|
|
|
|
|
|
|
pierPath :: Text -> FilePath
|
|
|
|
pierPath name = case nPierPath of
|
|
|
|
Just x -> x
|
|
|
|
Nothing -> "./" <> unpack name
|
|
|
|
|
2020-08-04 23:52:40 +03:00
|
|
|
nameFromShip :: Ship -> RIO RunningEnv Text
|
2020-01-23 07:16:09 +03:00
|
|
|
nameFromShip s = name
|
|
|
|
where
|
|
|
|
nameWithSig = Ob.renderPatp $ Ob.patp $ fromIntegral s
|
|
|
|
name = case stripPrefix "~" nameWithSig of
|
|
|
|
Nothing -> error "Urbit.ob didn't produce string with ~"
|
|
|
|
Just x -> pure x
|
|
|
|
|
2020-08-04 23:52:40 +03:00
|
|
|
bootFromSeed :: Pill -> Seed -> RIO RunningEnv ()
|
|
|
|
bootFromSeed pill seed = do
|
2020-01-23 07:16:09 +03:00
|
|
|
ethReturn <- dawnVent seed
|
|
|
|
|
|
|
|
case ethReturn of
|
|
|
|
Left x -> error $ unpack x
|
|
|
|
Right dawn -> do
|
|
|
|
let ship = sShip $ dSeed dawn
|
|
|
|
name <- nameFromShip ship
|
2020-08-04 23:52:40 +03:00
|
|
|
runTryBootFromPill pill name ship (Dawn dawn)
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
-- Now that we have all the information for running an application with a
|
|
|
|
-- PierConfig, do so.
|
2020-08-04 23:52:40 +03:00
|
|
|
runTryBootFromPill :: Pill -> Text -> Ship -> LegacyBootEvent
|
|
|
|
-> RIO RunningEnv ()
|
|
|
|
runTryBootFromPill pill name ship bootEvent = do
|
|
|
|
env <- ask
|
|
|
|
let vKill = (env ^. kingEnvL) ^. kingEnvKillSignal
|
2020-01-23 07:16:09 +03:00
|
|
|
let pierConfig = toPierConfig (pierPath name) opts
|
|
|
|
let networkConfig = toNetworkConfig opts
|
2020-06-07 02:03:20 +03:00
|
|
|
runPierEnv pierConfig networkConfig vKill $
|
2020-08-04 23:52:40 +03:00
|
|
|
tryBootFromPill True pill nLite ship bootEvent
|
2020-01-23 07:16:09 +03:00
|
|
|
------ tryBootFromPill (CLI.oExit opts) pill nLite flags ship bootEvent
|
|
|
|
|
2020-08-04 23:52:40 +03:00
|
|
|
runShipEnv :: CLI.Run -> CLI.Opts -> TMVar () -> RIO PierEnv a -> RIO RunningEnv a
|
2020-06-07 02:03:20 +03:00
|
|
|
runShipEnv (CLI.Run pierPath) opts vKill act = do
|
|
|
|
runPierEnv pierConfig netConfig vKill act
|
|
|
|
where
|
|
|
|
pierConfig = toPierConfig pierPath opts
|
|
|
|
netConfig = toNetworkConfig opts
|
|
|
|
|
|
|
|
runShip
|
2020-08-04 23:52:40 +03:00
|
|
|
:: CLI.Run -> CLI.Opts -> Bool -> RIO PierEnv ()
|
|
|
|
runShip (CLI.Run pierPath) opts daemon = do
|
2020-06-07 00:33:15 +03:00
|
|
|
mStart <- newEmptyMVar
|
2020-01-23 07:29:59 +03:00
|
|
|
if daemon
|
|
|
|
then runPier mStart
|
|
|
|
else do
|
2020-06-07 00:33:15 +03:00
|
|
|
-- Wait until the pier has started up, then connect a terminal. If
|
|
|
|
-- the terminal ever shuts down, ask the ship to go down.
|
2020-01-23 07:29:59 +03:00
|
|
|
connectionThread <- async $ do
|
|
|
|
readMVar mStart
|
2020-06-07 00:33:15 +03:00
|
|
|
finally (connTerm pierPath) $ do
|
2020-06-07 02:03:20 +03:00
|
|
|
view killPierActionL >>= atomically
|
2020-06-07 00:33:15 +03:00
|
|
|
|
|
|
|
-- Run the pier until it finishes, and then kill the terminal.
|
|
|
|
finally (runPier mStart) $ do
|
|
|
|
cancel connectionThread
|
2020-01-23 07:29:59 +03:00
|
|
|
where
|
2020-06-11 02:41:09 +03:00
|
|
|
runPier :: MVar () -> RIO PierEnv ()
|
2020-06-07 00:33:15 +03:00
|
|
|
runPier mStart = do
|
2020-06-07 02:03:20 +03:00
|
|
|
tryPlayShip
|
|
|
|
(CLI.oExit opts)
|
|
|
|
(CLI.oFullReplay opts)
|
|
|
|
(CLI.oDryFrom opts)
|
|
|
|
mStart
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
|
2020-08-07 19:42:00 +03:00
|
|
|
buildPortHandler :: (HasLogFunc e) => Bool -> RIO e PortControlApi
|
2020-08-07 20:04:57 +03:00
|
|
|
buildPortHandler False = pure $ buildInactivePorts
|
2020-08-07 19:42:00 +03:00
|
|
|
buildPortHandler True = buildNATPorts
|
2020-08-05 22:33:37 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
startBrowser :: HasLogFunc e => FilePath -> RIO e ()
|
|
|
|
startBrowser pierPath = runRAcquire $ do
|
|
|
|
-- lockFile pierPath
|
|
|
|
log <- Log.existing (pierPath <> "/.urb/log")
|
|
|
|
rio $ EventBrowser.run log
|
|
|
|
|
|
|
|
checkDawn :: HasLogFunc e => FilePath -> RIO e ()
|
|
|
|
checkDawn keyfilePath = do
|
|
|
|
-- The keyfile is a jammed Seed then rendered in UW format
|
|
|
|
text <- readFileUtf8 keyfilePath
|
|
|
|
asAtom <- case cordToUW (Cord $ T.strip text) of
|
|
|
|
Nothing -> error "Couldn't parse keyfile. Hint: keyfiles start with 0w?"
|
|
|
|
Just (UW a) -> pure a
|
|
|
|
|
|
|
|
asNoun <- cueExn asAtom
|
|
|
|
seed :: Seed <- case fromNoun asNoun of
|
|
|
|
Nothing -> error "Keyfile does not seem to contain a seed."
|
|
|
|
Just s -> pure s
|
|
|
|
|
|
|
|
print $ show seed
|
|
|
|
|
|
|
|
e <- dawnVent seed
|
|
|
|
print $ show e
|
|
|
|
|
|
|
|
|
|
|
|
checkComet :: HasLogFunc e => RIO e ()
|
|
|
|
checkComet = do
|
|
|
|
starList <- dawnCometList
|
|
|
|
putStrLn "Stars currently accepting comets:"
|
|
|
|
let starNames = map (Ob.renderPatp . Ob.patp . fromIntegral) starList
|
|
|
|
print starNames
|
|
|
|
putStrLn "Trying to mine a comet..."
|
|
|
|
eny <- io $ Sys.randomIO
|
|
|
|
let s = mineComet (Set.fromList starList) eny
|
|
|
|
print s
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2020-05-13 21:29:50 +03:00
|
|
|
args <- CLI.parseArgs
|
2020-06-11 02:41:09 +03:00
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
hSetBuffering stdout NoBuffering
|
|
|
|
setupSignalHandlers
|
|
|
|
|
2020-05-13 22:35:57 +03:00
|
|
|
runKingEnv args $ case args of
|
2020-05-13 21:29:50 +03:00
|
|
|
CLI.CmdRun ko ships -> runShips ko ships
|
|
|
|
CLI.CmdNew n o -> newShip n o
|
|
|
|
CLI.CmdBug (CLI.CollectAllFX pax ) -> collectAllFx pax
|
|
|
|
CLI.CmdBug (CLI.EventBrowser pax ) -> startBrowser pax
|
|
|
|
CLI.CmdBug (CLI.ValidatePill pax pil s) -> testPill pax pil s
|
|
|
|
CLI.CmdBug (CLI.ValidateEvents pax f l) -> checkEvs pax f l
|
|
|
|
CLI.CmdBug (CLI.ValidateFX pax f l) -> checkFx pax f l
|
|
|
|
CLI.CmdBug (CLI.ReplayEvents pax l ) -> replayPartEvs pax l
|
|
|
|
CLI.CmdBug (CLI.CheckDawn pax ) -> checkDawn pax
|
|
|
|
CLI.CmdBug CLI.CheckComet -> checkComet
|
|
|
|
CLI.CmdCon pier -> connTerm pier
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
where
|
2020-06-11 05:07:10 +03:00
|
|
|
runKingEnv args =
|
|
|
|
let verb = verboseLogging args
|
|
|
|
in if willRunTerminal args
|
|
|
|
then runKingEnvLogFile verb
|
|
|
|
else runKingEnvStderr verb
|
2020-02-19 19:39:11 +03:00
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
setupSignalHandlers = do
|
2020-01-23 07:16:09 +03:00
|
|
|
mainTid <- myThreadId
|
2020-05-02 01:32:08 +03:00
|
|
|
let onKillSig = throwTo mainTid UserInterrupt
|
2020-05-13 21:29:50 +03:00
|
|
|
for_ [Sys.sigTERM, Sys.sigINT] $ \sig -> do
|
|
|
|
Sys.installHandler sig (Sys.Catch onKillSig) Nothing
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-06-11 05:07:10 +03:00
|
|
|
verboseLogging :: CLI.Cmd -> Bool
|
|
|
|
verboseLogging = \case
|
|
|
|
CLI.CmdRun ko ships -> any CLI.oVerbose (ships <&> \(_, o, _) -> o)
|
|
|
|
_ -> False
|
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
willRunTerminal :: CLI.Cmd -> Bool
|
|
|
|
willRunTerminal = \case
|
|
|
|
CLI.CmdCon _ -> True
|
|
|
|
CLI.CmdRun ko [(_,_,daemon)] -> not daemon
|
|
|
|
CLI.CmdRun ko _ -> False
|
|
|
|
_ -> False
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-02 00:30:54 +03:00
|
|
|
|
|
|
|
{-
|
|
|
|
Runs a ship but restarts it if it crashes or shuts down on it's own.
|
|
|
|
|
|
|
|
Once `waitForKillRequ` returns, the ship will be terminated and this
|
|
|
|
routine will exit.
|
|
|
|
|
|
|
|
TODO Use logging system instead of printing.
|
|
|
|
-}
|
2020-05-13 22:35:57 +03:00
|
|
|
runShipRestarting
|
2020-08-04 23:52:40 +03:00
|
|
|
:: CLI.Run -> CLI.Opts -> RIO RunningEnv ()
|
|
|
|
runShipRestarting r o = do
|
2020-05-02 00:30:54 +03:00
|
|
|
let pier = pack (CLI.rPierPath r)
|
2020-08-04 23:52:40 +03:00
|
|
|
loop = runShipRestarting r o
|
2020-06-07 02:03:20 +03:00
|
|
|
|
|
|
|
onKill <- view onKillKingSigL
|
|
|
|
vKillPier <- newEmptyTMVarIO
|
2020-05-02 00:30:54 +03:00
|
|
|
|
2020-08-04 23:52:40 +03:00
|
|
|
tid <- asyncBound $ runShipEnv r o vKillPier $ runShip r o True
|
2020-05-02 00:30:54 +03:00
|
|
|
|
|
|
|
let onShipExit = Left <$> waitCatchSTM tid
|
2020-06-07 02:03:20 +03:00
|
|
|
onKillRequ = Right <$> onKill
|
2020-05-02 00:30:54 +03:00
|
|
|
|
|
|
|
atomically (onShipExit <|> onKillRequ) >>= \case
|
|
|
|
Left exit -> do
|
|
|
|
case exit of
|
2020-05-13 22:06:32 +03:00
|
|
|
Left err -> logError $ display (tshow err <> ": " <> pier)
|
|
|
|
Right () ->
|
|
|
|
logError $ display ("Ship exited on it's own. Why? " <> pier)
|
2020-05-02 00:30:54 +03:00
|
|
|
threadDelay 250_000
|
|
|
|
loop
|
|
|
|
Right () -> do
|
2020-06-05 00:10:33 +03:00
|
|
|
logTrace $ display (pier <> " shutdown requested")
|
|
|
|
race_ (wait tid) $ do
|
|
|
|
threadDelay 5_000_000
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug $ display (pier <> " not down after 5s, killing with fire.")
|
2020-06-05 00:10:33 +03:00
|
|
|
cancel tid
|
2020-05-13 22:06:32 +03:00
|
|
|
logTrace $ display ("Ship terminated: " <> pier)
|
2020-05-02 00:30:54 +03:00
|
|
|
|
2020-06-05 00:10:33 +03:00
|
|
|
{-
|
|
|
|
TODO This is messy and shared a lot of logic with `runShipRestarting`.
|
|
|
|
-}
|
2020-06-07 02:03:20 +03:00
|
|
|
runShipNoRestart
|
2020-08-04 23:52:40 +03:00
|
|
|
:: CLI.Run -> CLI.Opts -> Bool -> RIO RunningEnv ()
|
|
|
|
runShipNoRestart r o d = do
|
|
|
|
-- killing ship same as killing king
|
|
|
|
env <- ask
|
|
|
|
let vKill = (env ^. kingEnvL) ^. kingEnvKillSignal
|
|
|
|
tid <- asyncBound (runShipEnv r o vKill $ runShip r o d)
|
2020-06-07 02:03:20 +03:00
|
|
|
onKill <- view onKillKingSigL
|
2020-06-05 00:10:33 +03:00
|
|
|
|
|
|
|
let pier = pack (CLI.rPierPath r)
|
|
|
|
|
|
|
|
let onShipExit = Left <$> waitCatchSTM tid
|
2020-06-07 02:03:20 +03:00
|
|
|
onKillRequ = Right <$> onKill
|
2020-06-05 00:10:33 +03:00
|
|
|
|
|
|
|
atomically (onShipExit <|> onKillRequ) >>= \case
|
|
|
|
Left (Left err) -> do
|
|
|
|
logError $ display (tshow err <> ": " <> pier)
|
|
|
|
Left (Right ()) -> do
|
|
|
|
logError $ display (pier <> " exited on it's own. Why?")
|
|
|
|
Right () -> do
|
|
|
|
logTrace $ display (pier <> " shutdown requested")
|
|
|
|
race_ (wait tid) $ do
|
|
|
|
threadDelay 5_000_000
|
|
|
|
logTrace $ display (pier <> " not down after 5s, killing with fire.")
|
|
|
|
cancel tid
|
|
|
|
logTrace $ display (pier <> " terminated.")
|
2020-05-02 00:30:54 +03:00
|
|
|
|
2020-05-13 22:35:57 +03:00
|
|
|
runShips :: CLI.KingOpts -> [(CLI.Run, CLI.Opts, Bool)] -> RIO KingEnv ()
|
2020-05-11 23:42:30 +03:00
|
|
|
runShips CLI.KingOpts {..} ships = do
|
|
|
|
let meConf = MultiEyreConf
|
|
|
|
{ mecHttpPort = fromIntegral <$> koSharedHttpPort
|
|
|
|
, mecHttpsPort = fromIntegral <$> koSharedHttpsPort
|
|
|
|
, mecLocalhostOnly = False -- TODO Localhost-only needs to be
|
|
|
|
-- a king-wide option.
|
|
|
|
}
|
|
|
|
|
2020-05-12 22:45:39 +03:00
|
|
|
multi <- multiEyre meConf
|
2020-05-11 23:42:30 +03:00
|
|
|
|
2020-08-07 19:42:00 +03:00
|
|
|
ports <- buildPortHandler koUseNATPMP
|
2020-08-05 22:33:37 +03:00
|
|
|
|
|
|
|
runRunningEnv multi ports (go ships)
|
2020-05-11 23:42:30 +03:00
|
|
|
where
|
2020-08-04 23:52:40 +03:00
|
|
|
go :: [(CLI.Run, CLI.Opts, Bool)] -> RIO RunningEnv ()
|
|
|
|
go = \case
|
2020-06-05 00:10:33 +03:00
|
|
|
[] -> pure ()
|
2020-08-04 23:52:40 +03:00
|
|
|
[rod] -> runSingleShip rod
|
|
|
|
ships -> runMultipleShips (ships <&> \(r, o, _) -> (r, o))
|
2020-06-05 00:10:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- TODO Duplicated logic.
|
2020-08-04 23:52:40 +03:00
|
|
|
runSingleShip :: (CLI.Run, CLI.Opts, Bool) -> RIO RunningEnv ()
|
|
|
|
runSingleShip (r, o, d) = do
|
|
|
|
shipThread <- async (runShipNoRestart r o d)
|
2020-06-05 00:10:33 +03:00
|
|
|
|
|
|
|
{-
|
2020-06-07 00:33:15 +03:00
|
|
|
Wait for the ship to go down.
|
2020-06-05 00:10:33 +03:00
|
|
|
|
2020-06-07 00:33:15 +03:00
|
|
|
Since `waitCatch` will never throw an exception, the `onException`
|
|
|
|
block will only happen if this thread is killed with an async
|
|
|
|
exception. The one we expect is `UserInterrupt` which will be raised
|
|
|
|
on this thread upon SIGKILL or SIGTERM.
|
|
|
|
|
|
|
|
If this thread is killed, we first ask the ship to go down, wait
|
|
|
|
for the ship to actually go down, and then go down ourselves.
|
2020-06-05 00:10:33 +03:00
|
|
|
-}
|
2020-06-07 00:33:15 +03:00
|
|
|
onException (void $ waitCatch shipThread) $ do
|
2020-06-05 00:10:33 +03:00
|
|
|
logTrace "KING IS GOING DOWN"
|
2020-06-07 02:03:20 +03:00
|
|
|
atomically =<< view killKingActionL
|
|
|
|
waitCatch shipThread
|
|
|
|
pure ()
|
2020-06-05 00:10:33 +03:00
|
|
|
|
2020-05-11 23:42:30 +03:00
|
|
|
|
2020-08-04 23:52:40 +03:00
|
|
|
runMultipleShips :: [(CLI.Run, CLI.Opts)] -> RIO RunningEnv ()
|
|
|
|
runMultipleShips ships = do
|
2020-05-03 19:29:00 +03:00
|
|
|
shipThreads <- for ships $ \(r, o) -> do
|
2020-08-04 23:52:40 +03:00
|
|
|
async (runShipRestarting r o)
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-05-03 19:29:00 +03:00
|
|
|
{-
|
|
|
|
Since `spin` never returns, this will run until the main
|
|
|
|
thread is killed with an async exception. The one we expect is
|
|
|
|
`UserInterrupt` which will be raised on this thread upon SIGKILL
|
|
|
|
or SIGTERM.
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-06-07 02:03:20 +03:00
|
|
|
Once that happens, we send a shutdown signal which will cause all
|
|
|
|
ships to be shut down, and then we `wait` for them to finish before
|
|
|
|
returning.
|
2020-06-07 00:33:15 +03:00
|
|
|
|
|
|
|
This is different than the single-ship flow, because ships never
|
|
|
|
go down on their own in this flow. If they go down, they just bring
|
|
|
|
themselves back up.
|
2020-05-03 19:29:00 +03:00
|
|
|
-}
|
|
|
|
let spin = forever (threadDelay maxBound)
|
|
|
|
finally spin $ do
|
2020-05-13 22:06:32 +03:00
|
|
|
logTrace "KING IS GOING DOWN"
|
2020-06-07 02:03:20 +03:00
|
|
|
view killKingActionL >>= atomically
|
2020-05-03 19:29:00 +03:00
|
|
|
for_ shipThreads waitCatch
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
connTerm :: ∀e. HasLogFunc e => FilePath -> RIO e ()
|
2020-05-13 21:29:50 +03:00
|
|
|
connTerm = Term.runTerminalClient
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-06-07 02:03:20 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
checkFx :: HasLogFunc e
|
|
|
|
=> FilePath -> Word64 -> Word64 -> RIO e ()
|
|
|
|
checkFx pierPath first last =
|
|
|
|
rwith (Log.existing logPath) $ \log ->
|
|
|
|
runConduit $ streamFX log first last
|
|
|
|
.| tryParseFXStream
|
|
|
|
where
|
|
|
|
logPath = pierPath <> "/.urb/log"
|
|
|
|
|
|
|
|
streamFX :: HasLogFunc e
|
|
|
|
=> Log.EventLog -> Word64 -> Word64
|
|
|
|
-> ConduitT () ByteString (RIO e) ()
|
|
|
|
streamFX log first last = do
|
|
|
|
Log.streamEffectsRows log first .| loop
|
|
|
|
where
|
|
|
|
loop = await >>= \case Nothing -> pure ()
|
|
|
|
Just (eId, bs) | eId > last -> pure ()
|
|
|
|
Just (eId, bs) -> yield bs >> loop
|
|
|
|
|
|
|
|
tryParseFXStream :: HasLogFunc e => ConduitT ByteString Void (RIO e) ()
|
|
|
|
tryParseFXStream = loop
|
|
|
|
where
|
|
|
|
loop = await >>= \case
|
|
|
|
Nothing -> pure ()
|
|
|
|
Just bs -> do
|
|
|
|
n <- liftIO (cueBSExn bs)
|
|
|
|
fromNounErr n & either (logError . displayShow) pure
|
|
|
|
loop
|
|
|
|
|
|
|
|
|
|
|
|
{-
|
|
|
|
tryCopyLog :: IO ()
|
|
|
|
tryCopyLog = do
|
|
|
|
let logPath = "/Users/erg/src/urbit/zod/.urb/falselog/"
|
|
|
|
falselogPath = "/Users/erg/src/urbit/zod/.urb/falselog2/"
|
|
|
|
|
|
|
|
persistQ <- newTQueueIO
|
|
|
|
releaseQ <- newTQueueIO
|
|
|
|
(ident, nextEv, events) <-
|
|
|
|
with (do { log <- Log.existing logPath
|
|
|
|
; Pier.runPersist log persistQ (writeTQueue releaseQ)
|
|
|
|
; pure log
|
|
|
|
})
|
|
|
|
\log -> do
|
|
|
|
ident <- pure $ Log.identity log
|
|
|
|
events <- runConduit (Log.streamEvents log 1 .| consume)
|
|
|
|
nextEv <- Log.nextEv log
|
|
|
|
pure (ident, nextEv, events)
|
|
|
|
|
|
|
|
print ident
|
|
|
|
print nextEv
|
|
|
|
print (length events)
|
|
|
|
|
|
|
|
persistQ2 <- newTQueueIO
|
|
|
|
releaseQ2 <- newTQueueIO
|
|
|
|
with (do { log <- Log.new falselogPath ident
|
|
|
|
; Pier.runPersist log persistQ2 (writeTQueue releaseQ2)
|
|
|
|
; pure log
|
|
|
|
})
|
|
|
|
$ \log2 -> do
|
|
|
|
let writs = zip [1..] events <&> \(id, a) ->
|
|
|
|
(Writ id Nothing a, [])
|
|
|
|
|
|
|
|
print "About to write"
|
|
|
|
|
|
|
|
for_ writs $ \w ->
|
|
|
|
atomically (writeTQueue persistQ2 w)
|
|
|
|
|
|
|
|
print "About to wait"
|
|
|
|
|
|
|
|
replicateM_ 100 $ do
|
|
|
|
atomically $ readTQueue releaseQ2
|
|
|
|
|
|
|
|
print "Done"
|
|
|
|
-}
|