Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
zrosenbauer committed Jan 31, 2024
1 parent d6d76af commit 869f997
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
7 changes: 6 additions & 1 deletion packages/cleaner/src/bin/www.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { TimeRange } from "../lib/cloudRun";
import runJob from "../job";
import logger from '../lib/logger';

Expand All @@ -8,7 +9,11 @@ import logger from '../lib/logger';
throw new Error('GCP_PROJECT_ID is required');
}

const serviceNamesToDelete = await runJob(process.env.GCP_PROJECT_ID, process.env.DRY_RUN === 'true')
const serviceNamesToDelete = await runJob({
projectId: process.env.GCP_PROJECT_ID,
dryRun: process.env.DRY_RUN === 'true',
timeRange: process.env.TIME_RANGE as TimeRange,
})
if (serviceNamesToDelete.length === 0) {
logger.warn('Skipping: No services to delete');
} else {
Expand Down
14 changes: 12 additions & 2 deletions packages/cleaner/src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ import logger from './lib/logger';
* @param dryRun If true, the job will not delete any services
* @returns The service names that were deleted
*/
async function runJob(projectId: string, dryRun: boolean = false) {
async function runJob(payload: {
projectId: string,
dryRun: boolean,
timeRange?: cloudRun.TimeRange,
}) {
const {
projectId,
dryRun,
timeRange = '30 days'
} = payload;

const services = await cloudRun.listServices(
cloudRun.buildParent(projectId),
[
Expand All @@ -21,7 +31,7 @@ async function runJob(projectId: string, dryRun: boolean = false) {
{
field: 'age',
operation: 'greaterThanOrEqualTo',
value: '30 days',
value: timeRange,
}
]
);
Expand Down
10 changes: 6 additions & 4 deletions packages/cleaner/src/lib/cloudRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ export type FilterOperation =
| 'lessThan'
| 'equalTo';

export interface FilterCreatedAt {
export type TimeRange = `${number} ${'days' | 'hours' | 'min' | 'seconds'}`;

export interface FilterAge {
field: 'age';
operation: FilterOperation;
value: `${number} ${'days' | 'hours' | 'min' | 'seconds'}`;
value: TimeRange;
}

export interface FilterName {
Expand All @@ -127,7 +129,7 @@ export interface FilterLabels {
}

export type Filter =
| FilterCreatedAt
| FilterAge
| FilterName
| FilterLabels;

Expand All @@ -148,7 +150,7 @@ export interface Service {
* @param value A filter value
* @returns A age value in milliseconds
*/
export function getCreationAgeValue(value: FilterCreatedAt['value']): number {
export function getCreationAgeValue(value: FilterAge['value']): number {
const [duration, unit] = value.split(' ');
const multiplier = {
days: 86400000,
Expand Down

0 comments on commit 869f997

Please sign in to comment.