Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid stale DAML_SDK_VERSION_LATEST in assistant #12493

Merged
merged 2 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion daml-assistant/exe/DA/Daml/Assistant.hs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ runCommand env@Env{..} = \case
let asstVersion = unwrapDamlAssistantSdkVersion <$> envDamlAssistantSdkVersion
envVersions = catMaybes
[ envSdkVersion
, envLatestStableSdkVersion
, guard vAssistant >> asstVersion
, projectVersionM
, defaultVersionM
Expand Down Expand Up @@ -241,6 +240,7 @@ runCommand env@Env{..} = \case

versions = nubSort . concat $
[ envVersions
, maybeToList latestVersionM
, fromRight [] installedVersionsE
, if vAll then fromRight [] availableVersionsE else []
, fromRight [] snapshotVersionsE
Expand Down
18 changes: 9 additions & 9 deletions daml-assistant/src/DA/Daml/Assistant/Cache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module DA.Daml.Assistant.Cache
( cacheAvailableSdkVersions
, saveAvailableSdkVersions
, CacheAge (..)
) where

import DA.Daml.Assistant.Types
Expand Down Expand Up @@ -53,15 +54,15 @@ cacheAvailableSdkVersions
:: DamlPath
-> CachePath
-> IO [SdkVersion]
-> IO [SdkVersion]
-> IO ([SdkVersion], CacheAge)
cacheAvailableSdkVersions damlPath cachePath getVersions = do
damlConfigE <- tryConfig $ readDamlConfig damlPath
let updateCheckM = join $ eitherToMaybe (queryDamlConfig ["update-check"] =<< damlConfigE)
defaultUpdateCheck = UpdateCheckEvery (CacheTimeout 86400)
case fromMaybe defaultUpdateCheck updateCheckM of
UpdateCheckNever -> do
valueAgeM <- loadFromCacheWith cachePath versionsKey (CacheTimeout 0) deserializeVersions
pure (maybe [] fst valueAgeM)
valueAgeM <- loadFromCacheWith cachePath versionsKey (CacheTimeout 86400) deserializeVersions
sofiafaro-da marked this conversation as resolved.
Show resolved Hide resolved
pure $ fromMaybe ([], Stale) valueAgeM

UpdateCheckEvery timeout ->
cacheWith cachePath versionsKey timeout
Expand All @@ -86,22 +87,22 @@ cacheWith
-> Serialize t
-> Deserialize t
-> IO t
-> IO t
-> IO (t, CacheAge)
cacheWith cachePath key timeout serialize deserialize getFresh = do
valueAgeM <- loadFromCacheWith cachePath key timeout deserialize
case valueAgeM of
Just (value, Fresh) -> pure value
Just (value, Fresh) -> pure (value, Fresh)
Just (value, Stale) -> do
valueE <- tryAny getFresh
case valueE of
Left _ -> pure value
Left _ -> pure (value, Stale)
Right value' -> do
saveToCacheWith cachePath key serialize value'
pure value'
pure (value', Fresh)
Nothing -> do
value <- getFresh
saveToCacheWith cachePath key serialize value
pure value
pure (value, Fresh)

-- | A representation of the age of a cache value. We only care if the value is stale or fresh.
data CacheAge
Expand Down Expand Up @@ -141,4 +142,3 @@ loadFromCacheWith cachePath key timeout deserialize = do
(valueStr, age) <- valueAgeM
value <- deserialize valueStr
Just (value, age)

8 changes: 5 additions & 3 deletions daml-assistant/src/DA/Daml/Assistant/Version.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ refreshAvailableSdkVersions cachePath = do

-- | Same as getAvailableSdkVersions, but result is cached based on the duration
-- of the update-check value in daml-config.yaml (defaults to 1 day).
getAvailableSdkVersionsCached :: DamlPath -> CachePath -> IO [SdkVersion]
getAvailableSdkVersionsCached :: DamlPath -> CachePath -> IO ([SdkVersion], CacheAge)
getAvailableSdkVersionsCached damlPath cachePath =
cacheAvailableSdkVersions damlPath cachePath getAvailableSdkVersions

Expand All @@ -181,8 +181,10 @@ getLatestSdkVersionCached :: DamlPath -> CachePath -> IO (Maybe SdkVersion)
getLatestSdkVersionCached damlPath cachePath = do
versionsE <- tryAssistant $ getAvailableSdkVersionsCached damlPath cachePath
pure $ do
versions <- eitherToMaybe versionsE
maximumMay versions
(versions, age) <- eitherToMaybe versionsE
case age of
Stale -> Nothing
Fresh -> maximumMay versions

-- | Get the latest snapshot SDK version.
getLatestSdkSnapshotVersion :: IO (Maybe SdkVersion)
Expand Down