Skip to content

Commit

Permalink
fix: reuse PGO connection across the codebase
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <[email protected]>
  • Loading branch information
ghivert committed May 15, 2024
1 parent 55127fc commit 104dfcc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
15 changes: 6 additions & 9 deletions apps/backend/src/backend.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import backend/config.{type Config}
import backend/config.{type Context}
import backend/postgres/postgres
import backend/router
import dot_env
Expand Down Expand Up @@ -30,7 +30,7 @@ pub fn main() {

let assert Ok(_) =
supervisor.start(fn(children) {
let assert Ok(_) = start_hex_sync(cnf, children)
let assert Ok(_) = start_hex_sync(ctx, children)
children
})

Expand All @@ -45,14 +45,11 @@ fn supervise(start: fn() -> _) {
})
}

fn sync_hex(cnf: Config, children: supervisor.Children(Nil)) {
hex.sync_new_gleam_releases(cnf, children)
fn sync_hex(ctx: Context, children: supervisor.Children(Nil)) {
hex.sync_new_gleam_releases(ctx, children)
}

fn start_hex_sync(cnf: Config, children: supervisor.Children(Nil)) {
fn start_hex_sync(ctx: Context, children: supervisor.Children(Nil)) {
use <- supervise()
periodic.periodically(
do: fn() { sync_hex(cnf, children) },
waiting: 60 * 1000,
)
periodic.periodically(do: fn() { sync_hex(ctx, children) }, waiting: 6 * 1000)
}
9 changes: 3 additions & 6 deletions apps/backend/src/tasks/hex.gleam
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import api/hex as api
import api/hex_repo
import api/signatures
import backend/config.{type Config, type Context}
import backend/config.{type Context}
import backend/data/hex_read.{type HexRead}
import backend/error.{type Error}
import backend/gleam/context
import backend/postgres/postgres
import backend/postgres/queries
import birl.{type Time}
import birl/duration
Expand Down Expand Up @@ -34,18 +33,17 @@ type State {
}

pub fn sync_new_gleam_releases(
cnf: Config,
ctx: Context,
children: supervisor.Children(Nil),
) -> Result(HexRead, Error) {
let ctx = postgres.connect(cnf)
wisp.log_info("Syncing new releases from Hex")
use limit <- result.try(queries.get_last_hex_date(ctx.db))
use latest <- result.try(sync_packages(
State(
page: 1,
limit: limit,
newest: limit,
hex_api_key: cnf.hex_api_key,
hex_api_key: ctx.hex_api_key,
last_logged: birl.now(),
db: ctx.db,
),
Expand All @@ -54,7 +52,6 @@ pub fn sync_new_gleam_releases(
let latest = queries.upsert_most_recent_hex_timestamp(ctx.db, latest)
wisp.log_info("")
wisp.log_info("Up to date!")
pgo.disconnect(ctx.db)
latest
}

Expand Down

0 comments on commit 104dfcc

Please sign in to comment.