Merge pull request #108 from chreekat/env-options-for-background-tasks

Env options for background tasks
This commit is contained in:
Christopher Reichert 2015-06-29 17:01:36 -05:00
commit 1bcc651118
3 changed files with 25 additions and 3 deletions

View File

@ -267,7 +267,7 @@ launchWebApp :: AppStartConfig
-> IO a
launchWebApp AppStartConfig {..} aid BundleConfig {..} mdir rlog WebAppConfig {..} f = do
otherEnv <- pluginsGetEnv ascPlugins name bconfigPlugins
systemEnv <- getEnvironment
forwardedEnv <- getForwardedEnv waconfigForwardEnv
let httpPort = kconfigExternalHttpPort ascKeterConfig
httpsPort = kconfigExternalHttpsPort ascKeterConfig
(scheme, extport) =
@ -278,7 +278,7 @@ launchWebApp AppStartConfig {..} aid BundleConfig {..} mdir rlog WebAppConfig {.
-- Ordering chosen specifically to precedence rules: app specific,
-- plugins, global, and then auto-set Keter variables.
[ waconfigEnvironment
, Map.filterWithKey (\k _ -> Set.member k waconfigForwardEnv) $ Map.fromList $ map (pack *** pack) systemEnv
, forwardedEnv
, Map.fromList otherEnv
, kconfigEnvironment ascKeterConfig
, Map.singleton "PORT" $ pack $ show waconfigPort
@ -355,7 +355,14 @@ launchBackgroundApp :: AppStartConfig
-> IO a
launchBackgroundApp AppStartConfig {..} aid BundleConfig {..} mdir rlog BackgroundConfig {..} f = do
otherEnv <- pluginsGetEnv ascPlugins name bconfigPlugins
let env = Map.toList bgconfigEnvironment ++ otherEnv
forwardedEnv <- getForwardedEnv bgconfigForwardEnv
let env = Map.toList $ Map.unions
-- Order matters as in launchWebApp
[ bgconfigEnvironment
, forwardedEnv
, Map.fromList otherEnv
, kconfigEnvironment ascKeterConfig
]
exec <- canonicalizePath bgconfigExec
let delay = threadDelay $ fromIntegral $ bgconfigRestartDelaySeconds * 1000 * 1000
@ -592,6 +599,16 @@ getTimestamp = readTVar . appModTime
pluginsGetEnv :: Plugins -> Appname -> Object -> IO [(Text, Text)]
pluginsGetEnv ps app o = fmap concat $ mapM (\p -> pluginGetEnv p app o) ps
-- | For the forward-env option. From a Set of desired variables, create a
-- Map pulled from the system environment.
getForwardedEnv :: Set Text -> IO (Map Text Text)
getForwardedEnv vars = filterEnv <$> getEnvironment
where
filterEnv = Map.filterWithKey (\k _ -> Set.member k vars)
. Map.fromList
. map (pack *** pack)
{- FIXME handle static stanzas
let staticReverse r = do
HostMan.addEntry hostman (ReverseProxy.reversingHost r)

View File

@ -409,6 +409,7 @@ data BackgroundConfig = BackgroundConfig
, bgconfigEnvironment :: !(Map Text Text)
, bgconfigRestartCount :: !RestartCount
, bgconfigRestartDelaySeconds :: !Word
, bgconfigForwardEnv :: !(Set Text)
}
deriving Show
@ -426,6 +427,7 @@ instance ParseYamlFile BackgroundConfig where
<*> o .:? "env" .!= Map.empty
<*> o .:? "restart-count" .!= UnlimitedRestarts
<*> o .:? "restart-delay-seconds" .!= 5
<*> o .:? "forward-env" .!= Set.empty
instance ToJSON BackgroundConfig where
toJSON BackgroundConfig {..} = object $ catMaybes
@ -436,4 +438,5 @@ instance ToJSON BackgroundConfig where
UnlimitedRestarts -> Nothing
LimitedRestarts count -> Just $ "restart-count" .= count
, Just $ "restart-delay-seconds" .= bgconfigRestartDelaySeconds
, Just $ "forward-env" .= bgconfigForwardEnv
]

View File

@ -20,6 +20,8 @@ stanzas:
- Keter Background Worker
env:
FROM_KETER_CONFIG: foo bar baz
forward-env:
- ENV_VAR_FOR_BACKGROUND
restart-count: 10
restart-delay-seconds: 6