|
| 1 | +import {Temporal} from '@js-temporal/polyfill'; |
| 2 | +import {PackageInfo} from '@webcomponents/catalog-api/lib/schema.js'; |
| 3 | +import type Koa from 'koa'; |
| 4 | +import type {Catalog} from '../../catalog.js'; |
| 5 | + |
| 6 | +// Google Cloud Run default request timeout is 5 minutes, so to do longer |
| 7 | +// imports we need to configure the timeout. |
| 8 | +const maxImportDuration = Temporal.Duration.from({minutes: 5}); |
| 9 | + |
| 10 | +export const makeUpdatePackagesRoute = |
| 11 | + (catalog: Catalog) => async (context: Koa.Context) => { |
| 12 | + const startInstant = Temporal.Now.instant(); |
| 13 | + // If the `force` query parameter is present we force updating of all |
| 14 | + // packages by setting the `notUpdatedSince` parameter to `startInstant` so |
| 15 | + // that we get all packages last updated before now. We calculate the |
| 16 | + // `notUpdatedSince` time once before updates so that we don't retrieve |
| 17 | + // packages that we update in this operation. |
| 18 | + // `force`` is useful for development and testing as we may be trying to |
| 19 | + // update packages that were just imported. |
| 20 | + // TODO (justinfagnani): check a DEV mode also so this isn't available |
| 21 | + // in production? |
| 22 | + const force = 'force' in context.query; |
| 23 | + const notUpdatedSince = force ? startInstant : undefined; |
| 24 | + |
| 25 | + // If `force` is true, override the default packageUpdateInterval |
| 26 | + // TODO: how do we make an actually 0 duration? |
| 27 | + const packageUpdateInterval = force |
| 28 | + ? Temporal.Duration.from({microseconds: 1}) |
| 29 | + : undefined; |
| 30 | + |
| 31 | + console.log('Starting package update at', startInstant, `force: ${force}`); |
| 32 | + |
| 33 | + let packagesToUpdate!: Array<PackageInfo>; |
| 34 | + let packagesUpdated = 0; |
| 35 | + let iteration = 0; |
| 36 | + |
| 37 | + // Loop through batches of packages to update. |
| 38 | + // We batch here so that we can pause and check that we're still within the |
| 39 | + // maxImportDuration, and use small enough batches so that we can ensure at |
| 40 | + // least one batch in that time. |
| 41 | + do { |
| 42 | + // getPackagesToUpdate() queries the first N (default 100) packages that |
| 43 | + // have not been updated since the update interval (default 6 hours). |
| 44 | + // When a package is imported it's lastUpdate date will be updated and the |
| 45 | + // next call to getPackagesToUpdate() will return the next 100 packages. |
| 46 | + // This way we don't need a DB cursor to make progress through the |
| 47 | + // package list. |
| 48 | + packagesToUpdate = await catalog.getPackagesToUpdate(notUpdatedSince); |
| 49 | + |
| 50 | + if (packagesToUpdate.length === 0) { |
| 51 | + // No more packages to update |
| 52 | + if (iteration === 0) { |
| 53 | + console.log('No packages to update'); |
| 54 | + } |
| 55 | + break; |
| 56 | + } |
| 57 | + |
| 58 | + await Promise.allSettled( |
| 59 | + packagesToUpdate.map(async (pkg) => { |
| 60 | + try { |
| 61 | + return await catalog.importPackage(pkg.name, packageUpdateInterval); |
| 62 | + } catch (e) { |
| 63 | + console.error(e); |
| 64 | + throw e; |
| 65 | + } |
| 66 | + }) |
| 67 | + ); |
| 68 | + packagesUpdated += packagesToUpdate.length; |
| 69 | + |
| 70 | + const now = Temporal.Now.instant(); |
| 71 | + const timeSinceStart = now.since(startInstant); |
| 72 | + // If the time since the update started is not less than that max import |
| 73 | + // duration, stop. |
| 74 | + // TODO (justinfagnani): we need a way to test this |
| 75 | + if (Temporal.Duration.compare(timeSinceStart, maxImportDuration) !== -1) { |
| 76 | + break; |
| 77 | + } |
| 78 | + } while (true); |
| 79 | + console.log(`Updated ${packagesUpdated} packages`); |
| 80 | + |
| 81 | + if (packagesToUpdate.length > 0) { |
| 82 | + // TODO (justinfagnani): kick off new update request |
| 83 | + console.log(`Not all packages were updated (${packagesToUpdate.length})`); |
| 84 | + } |
| 85 | + |
| 86 | + context.status = 200; |
| 87 | + context.type = 'html'; |
| 88 | + context.body = ` |
| 89 | + <h1>Update Results</h1> |
| 90 | + <p>Updated ${packagesUpdated} package</p> |
| 91 | + `; |
| 92 | + }; |
0 commit comments