Skip to content

Commit

Permalink
feat: ignore already existing packages
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <[email protected]>
  • Loading branch information
ghivert committed May 12, 2024
1 parent d370658 commit 6643bb7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
5 changes: 5 additions & 0 deletions apps/backend/src/backend/postgres/queries.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ pub fn lookup_release(
),
)
|> result.map_error(error.DatabaseError)
|> result.try(fn(res) {
res.rows
|> list.first()
|> result.replace_error(error.UnknownError(""))
})
}

pub fn add_package_gleam_constraint(
Expand Down
31 changes: 21 additions & 10 deletions apps/backend/src/tasks/hex.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import backend/postgres/postgres
import backend/postgres/queries
import birl.{type Time}
import birl/duration
import gleam/function
import gleam/hexpm.{type Package}
import gleam/int
import gleam/list
Expand Down Expand Up @@ -51,7 +52,8 @@ pub fn sync_new_gleam_releases(
children,
))
let latest = queries.upsert_most_recent_hex_timestamp(ctx.db, latest)
wisp.log_info("\nUp to date!")
wisp.log_info("")
wisp.log_info("Up to date!")
latest
}

Expand Down Expand Up @@ -81,7 +83,7 @@ fn sync_packages(
use state <- result.try(list.try_fold(
new_packages,
state,
do_sync_package(Some(children)),
do_sync_package(Some(children), force: False),
))
case list.length(all_packages) == list.length(new_packages) {
_ if all_packages == [] -> Ok(state.newest)
Expand All @@ -90,7 +92,10 @@ fn sync_packages(
}
}

fn do_sync_package(children: Option(supervisor.Children(Nil))) {
fn do_sync_package(
children: Option(supervisor.Children(Nil)),
force force_old_release_update: Bool,
) {
fn(state: State, package: hexpm.Package) -> Result(State, Error) {
let secret = state.hex_api_key
use releases <- result.try(lookup_gleam_releases(package, secret: secret))
Expand All @@ -102,6 +107,7 @@ fn do_sync_package(children: Option(supervisor.Children(Nil))) {
releases,
state,
children,
force_old_release_update,
))
State(..state, last_logged: birl.now())
}
Expand All @@ -110,7 +116,7 @@ fn do_sync_package(children: Option(supervisor.Children(Nil))) {
}

pub fn sync_package(ctx: Context, package: hexpm.Package) {
do_sync_package(None)(
do_sync_package(None, force: True)(
State(
page: -1,
limit: birl.now(),
Expand Down Expand Up @@ -145,12 +151,7 @@ fn extract_release_interfaces_from_db(
id: Int,
release: hexpm.Release,
) {
use _ <- result.try_recover({
use res <- result.try(queries.lookup_release(state.db, id, release))
res.rows
|> list.first()
|> result.replace_error(error.UnknownError(""))
})
use _ <- result.try_recover(queries.lookup_release(state.db, id, release))
queries.upsert_release(state.db, id, release, None, None)
|> result.replace(#(None, None))
}
Expand Down Expand Up @@ -194,6 +195,7 @@ fn insert_package_and_releases(
releases: List(hexpm.Release),
state: State,
children: Option(supervisor.Children(Nil)),
force_old_release_update: Bool,
) {
let secret = state.hex_api_key
let versions =
Expand All @@ -208,6 +210,15 @@ fn insert_package_and_releases(
wisp.log_debug("Saving releases for " <> package.name)
list.try_each(releases, fn(r) {
let release = package.name <> " v" <> r.version
use _ <- result.try_recover({
queries.lookup_release(state.db, id, r)
|> result.replace(Nil)
|> case force_old_release_update {
True -> result.try(_, fn(_) { Error(error.EmptyError) })
False -> function.identity
}
})
wisp.log_debug("Handling release " <> r.version)
use interfaces <- result.map({
extract_release_interfaces_from_db(state, id, r)
})
Expand Down

0 comments on commit 6643bb7

Please sign in to comment.