-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e16bb37
commit 4a0a13e
Showing
28 changed files
with
5,211 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import _ from "lodash"; | ||
import { isPresent } from "ts-is-present"; | ||
|
||
import { eq, inArray, lte } from "@ctrlplane/db"; | ||
import { db } from "@ctrlplane/db/client"; | ||
import * as SCHEMA from "@ctrlplane/db/schema"; | ||
import { logger } from "@ctrlplane/logger"; | ||
|
||
type QueryRow = { | ||
environment: SCHEMA.Environment; | ||
deployment: SCHEMA.Deployment; | ||
}; | ||
|
||
const groupByEnvironment = (rows: QueryRow[]) => | ||
_.chain(rows) | ||
.groupBy((e) => e.environment.id) | ||
.map((env) => ({ | ||
...env[0]!.environment, | ||
deployments: env.map((e) => e.deployment), | ||
})) | ||
.value(); | ||
|
||
export const run = async () => { | ||
const expiredEnvironments = await db | ||
.select() | ||
.from(SCHEMA.environment) | ||
.innerJoin( | ||
SCHEMA.deployment, | ||
eq(SCHEMA.deployment.systemId, SCHEMA.environment.systemId), | ||
) | ||
.where(lte(SCHEMA.environment.expiresAt, new Date())) | ||
.then(groupByEnvironment); | ||
if (expiredEnvironments.length === 0) return; | ||
|
||
const targetPromises = expiredEnvironments | ||
.filter((env) => isPresent(env.targetFilter)) | ||
.map(async (env) => { | ||
const targets = await db | ||
.select() | ||
.from(SCHEMA.target) | ||
.where(SCHEMA.targetMatchesMetadata(db, env.targetFilter)); | ||
|
||
return { environmentId: env.id, targets }; | ||
}); | ||
const associatedTargets = await Promise.all(targetPromises); | ||
|
||
for (const { environmentId, targets } of associatedTargets) | ||
logger.info( | ||
`[${targets.length}] targets are associated with expired environment [${environmentId}]`, | ||
); | ||
|
||
const envIds = expiredEnvironments.map((env) => env.id); | ||
await db | ||
.delete(SCHEMA.environment) | ||
.where(inArray(SCHEMA.environment.id, envIds)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.