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

Add healthchecks to server #654

Merged
merged 4 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ GITHUB_API_URL="https://api.github.com"
S3_API_URL="https://packages.registry.purescript.org"
S3_BUCKET_URL="https://ams3.digitaloceanspaces.com"
PURSUIT_API_URL="https://pursuit.purescript.org"
HEALTHCHECKS_URL="https://hc-ping.com/uuid-from-healthchecks"
9 changes: 8 additions & 1 deletion app/src/App/Effect/Env.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ResourceEnv =
, s3BucketUrl :: URL
, githubApiUrl :: URL
, pursuitApiUrl :: URL
, healthchecksUrl :: URL
}

-- | An effect for various external resources (files, databases, API endpoints,
Expand All @@ -49,13 +50,15 @@ lookupResourceEnv = do
s3BucketUrlEnv <- lookupWithDefault s3BucketUrl defaultS3BucketUrl
githubApiUrlEnv <- lookupWithDefault githubApiUrl defaultGitHubApiUrl
pursuitApiUrlEnv <- lookupWithDefault pursuitApiUrl defaultPursuitApiUrl
healthchecksUrlEnv <- lookupRequired healthchecksUrl
pure
{ dhallTypes: dhallTypesEnv
, databaseUrl: databaseUrlEnv
, s3ApiUrl: s3ApiUrlEnv
, s3BucketUrl: s3BucketUrlEnv
, githubApiUrl: githubApiUrlEnv
, pursuitApiUrl: pursuitApiUrlEnv
, healthchecksUrl: healthchecksUrlEnv
}

-- | Environment fields available in the GitHub Event environment, namely
Expand Down Expand Up @@ -202,13 +205,17 @@ githubApiUrl = EnvKey { key: "GITHUB_API_URL", decode: pure }
defaultGitHubApiUrl :: URL
defaultGitHubApiUrl = "https://api.github.com"

-- | The base URL of the GitHub API
-- | The base URL of the Pursuit API
pursuitApiUrl :: EnvKey URL
pursuitApiUrl = EnvKey { key: "PURSUIT_API_URL", decode: pure }

defaultPursuitApiUrl :: URL
defaultPursuitApiUrl = "https://pursuit.purescript.org"

-- | The URL of the health checks endpoint
healthchecksUrl :: EnvKey URL
healthchecksUrl = EnvKey { key: "HEALTHCHECKS_URL", decode: pure }

-- | A GitHub token for the @pacchettibotti user at the PACCHETTIBOTTI_TOKEN key.
pacchettibottiToken :: EnvKey GitHubToken
pacchettibottiToken = EnvKey { key: "PACCHETTIBOTTI_TOKEN", decode: decodeGitHubToken }
Expand Down
24 changes: 23 additions & 1 deletion app/src/App/Server.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Registry.App.Server where

import Registry.App.Prelude hiding ((/))

import Affjax.Node as Affjax
import Affjax.ResponseFormat as ResponseFormat
import Affjax.StatusCode (StatusCode(..))
import Control.Monad.Cont (ContT)
import Data.Codec.Argonaut as CA
import Data.Formatter.DateTime as Formatter.DateTime
Expand All @@ -12,6 +15,7 @@ import Effect.Aff as Aff
import Effect.Class.Console as Console
import HTTPurple (JsonDecoder(..), JsonEncoder(..), Method(..), Request, Response)
import HTTPurple as HTTPurple
import HTTPurple.Status as Status
import Node.Path as Path
import Node.Process as Process
import Record as Record
Expand Down Expand Up @@ -102,7 +106,14 @@ router env { route, method, body } = HTTPurple.usingCont case route, method of
Right job -> do
jsonOk V1.jobCodec (Record.insert (Proxy :: _ "logs") logs job)

_, _ -> HTTPurple.notFound
Status, Get ->
HTTPurple.emptyResponse Status.ok

Status, Head ->
HTTPurple.emptyResponse Status.ok

_, _ ->
HTTPurple.notFound
where
forkPipelineJob :: PackageName -> String -> JobType -> (JobId -> Run _ Unit) -> ContT Response (Run _) Response
forkPipelineJob packageName ref jobType action = do
Expand Down Expand Up @@ -224,6 +235,17 @@ main = do
Console.log $ "Failed to start server: " <> Aff.message error
Process.exit 1
Right env -> do
_healthcheck <- Aff.launchAff do
let
loop = do
Affjax.get ResponseFormat.ignore env.vars.resourceEnv.healthchecksUrl >>= case _ of
Left _ -> pure unit
Right { status: StatusCode status } | status /= 200 -> pure unit
Right _ -> do
Aff.delay (Aff.Milliseconds (1000.0 * 60.0 * 5.0))
loop
loop

_close <- HTTPurple.serve
{ hostname: "0.0.0.0"
, port: 8080
Expand Down
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@
cp spago.yaml spago.lock $WORKDIR
cp -a app foreign lib scripts types $WORKDIR
ln -s ${pkgs.registry.package-lock}/js/node_modules $WORKDIR/node_modules

pushd $WORKDIR
export HEALTHCHECKS_URL=${defaultEnv.HEALTHCHECKS_URL}
${pkgs.spago-unstable}/bin/spago test
popd
'';
Expand Down
7 changes: 3 additions & 4 deletions lib/src/API/V1.purs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ data Route
| Unpublish
| Transfer
| Jobs
| Job JobId
{ level :: Maybe LogLevel
, since :: Maybe DateTime
}
| Job JobId { level :: Maybe LogLevel, since :: Maybe DateTime }
| Status

derive instance Generic Route _

Expand All @@ -48,6 +46,7 @@ routes = Routing.root $ Routing.prefix "api" $ Routing.prefix "v1" $ RoutingG.su
, since: Routing.optional <<< timestampP <<< Routing.string
}
)
, "Status": "status" / RoutingG.noArgs
}

jobIdS :: RouteDuplex' JobId
Expand Down
Loading