Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup scheduler with trigger.dev #36

Merged
merged 3 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/trigger-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Deploy to Trigger.dev (Production)

on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
- "v[0-9]+.[0-9]+.[0-9]+-beta.[0-9]+"

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: "20.x"
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: 🚀 Deploy Trigger.dev
env:
TRIGGER_ACCESS_TOKEN: ${{ secrets.TRIGGER_ACCESS_TOKEN }}
run: |
pnpm trigger:deploy
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
# sst
.sst
sst-env.d.ts

.trigger
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
"dev": "sst dev --stage dev",
"test": "vitest",
"test:coverage": "vitest run --coverage",
"migrate": "drizzle-kit migrate"
"migrate": "drizzle-kit migrate",
"trigger:dev": "pnpm dlx trigger.dev dev",
"trigger:deploy": "pnpm dlx trigger.dev deploy"
},
"author": "Ru Chern Chong <[email protected]>",
"license": "MIT",
"dependencies": {
"@neondatabase/serverless": "^0.10.4",
"@trigger.dev/sdk": "^3.3.7",
"@upstash/redis": "^1.34.3",
"adm-zip": "^0.5.16",
"drizzle-orm": "^0.37.0",
Expand Down
1,228 changes: 1,101 additions & 127 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/config/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { Redis } from "@upstash/redis";
import { Resource } from "sst";

export const redis = new Redis({
url: Resource.UPSTASH_REDIS_REST_URL.value,
token: Resource.UPSTASH_REDIS_REST_TOKEN.value,
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
});
13 changes: 13 additions & 0 deletions src/config/schedulers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type SchedulerName = "cars" | "coe";

type ScheduleOptions =
| string
| {
pattern: string;
timezone?: string;
};

export const schedulers: Record<SchedulerName, ScheduleOptions> = {
cars: "*/60 0-10 * * 1-5",
coe: "*/60 0-10 * * 1-5",
};
2 changes: 1 addition & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import { Resource } from "sst";

const sql = neon(Resource.DATABASE_URL.value);
const sql = neon(process.env.DATABASE_URL);
export const db = drizzle({ client: sql });
13 changes: 9 additions & 4 deletions src/lib/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createUniqueKey } from "@/utils/createUniqueKey";
import { downloadFile } from "@/utils/downloadFile";
import { processCSV } from "@/utils/processCSV";
import { cacheChecksum, getCachedChecksum } from "@/utils/redisCache";
import { getTableName } from "drizzle-orm";
import type { PgTable } from "drizzle-orm/pg-core";

export interface UpdaterConfig<T extends PgTable> {
Expand All @@ -15,7 +16,8 @@ export interface UpdaterConfig<T extends PgTable> {
keyFields: string[];
}

export interface UpdaterResult {
export interface UpdaterResult<T extends PgTable> {
table: T["_"]["name"];
recordsProcessed: number;
message: string;
timestamp: string;
Expand All @@ -28,7 +30,9 @@ export const updater = async <T extends PgTable>({
zipFileName,
zipUrl,
keyFields,
}: UpdaterConfig<T>): Promise<UpdaterResult> => {
}: UpdaterConfig<T>): Promise<UpdaterResult<T>> => {
const tableName = getTableName(table);

// Download and extract file
const extractedFileName = await downloadFile(zipUrl);
const destinationPath = path.join(AWS_LAMBDA_TEMP_DIR, extractedFileName);
Expand All @@ -55,6 +59,7 @@ export const updater = async <T extends PgTable>({
`File have not been changed since the last update. Checksum: ${checksum}`,
);
return {
table: tableName,
recordsProcessed: 0,
message: `File have not been changed since the last update. Checksum: ${checksum}`,
timestamp: new Date().toISOString(),
Expand Down Expand Up @@ -86,7 +91,7 @@ export const updater = async <T extends PgTable>({
// Return early when there are no new records to be added to the database
if (newRecords.length === 0) {
return {
// table: table.$name,
table: tableName,
recordsProcessed: 0,
message:
"No new data to insert. The provided data matches the existing records.",
Expand All @@ -111,7 +116,7 @@ export const updater = async <T extends PgTable>({
await cacheChecksum(extractedFileName, checksum);

return {
// table: table.$name,
table: tableName,
recordsProcessed: totalInserted,
message: `${totalInserted} record(s) inserted in ${Math.round(end - start)}ms`,
timestamp: new Date().toISOString(),
Expand Down
26 changes: 26 additions & 0 deletions src/trigger/update-cars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { schedulers } from "@/config/schedulers";
import { updateCars } from "@/lib/updateCars";
import { logger, schedules } from "@trigger.dev/sdk/v3";

export const updateCarsTask = schedules.task({
id: "update-cars",
cron: schedulers.cars,
run: async (payload: any, { ctx }) => {
try {
logger.log("Starting Cars Update Task", { payload, ctx });

const response = await updateCars();

logger.log("Cars Update Completed", {
recordsProcessed: response.recordsProcessed,
message: response.message,
timestamp: response.timestamp,
});

return response;
} catch (error) {
logger.error("Cars Update Task Failed", { error });
throw error;
}
},
});
26 changes: 26 additions & 0 deletions src/trigger/update-coe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { schedulers } from "@/config/schedulers";
import { updateCOE } from "@/lib/updateCOE";
import { logger, schedules } from "@trigger.dev/sdk/v3";

export const updateCOETask = schedules.task({
id: "update-coe",
cron: schedulers.coe,
run: async (payload: any, { ctx }) => {
try {
logger.log("Starting COE Update Task", { payload, ctx });

const response = await updateCOE();

logger.log("COE Update Completed", {
recordsProcessed: response.recordsProcessed,
message: response.message,
timestamp: response.timestamp,
});

return response;
} catch (error) {
logger.error("COE Update Task Failed", { error });
throw error;
}
},
});
64 changes: 18 additions & 46 deletions sst.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/// <reference path="./.sst/platform/config.d.ts" />

import type { CronArgs } from "./.sst/platform/src/components/aws/cron";

export default $config({
app(input) {
return {
Expand All @@ -16,56 +14,30 @@ export default $config({
};
},
async run() {
const DATABASE_URL = new sst.Secret(
"DATABASE_URL",
process.env.DATABASE_URL,
);
const UPSTASH_REDIS_REST_URL = new sst.Secret(
"UPSTASH_REDIS_REST_URL",
process.env.UPSTASH_REDIS_REST_URL,
);
const UPSTASH_REDIS_REST_TOKEN = new sst.Secret(
"UPSTASH_REDIS_REST_TOKEN",
process.env.UPSTASH_REDIS_REST_TOKEN,
);

const updateCars = new sst.aws.Function("UpdateCars", {
link: [DATABASE_URL, UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN],
handler: "src/lib/updateCars.handler",
});

const updateCOE = new sst.aws.Function("UpdateCOE", {
link: [DATABASE_URL, UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN],
handler: "src/lib/updateCOE.handler",
});

const CRON_SCHEDULERS: Record<string, CronArgs> = {
cars: {
schedule: "cron(0/60 0-10 ? * MON-FRI *)",
job: updateCars.arn,
},
coe: {
schedule: "cron(0/60 0-10 ? * MON-FRI *)",
job: updateCOE.arn,
},
"coe-1st-bidding": {
schedule: "cron(0/10 8-10 ? * 4#1 *)",
job: updateCOE.arn,
},
"coe-2nd-bidding": {
schedule: "cron(0/10 8-10 ? * 4#3 *)",
job: updateCOE.arn,
},
const environment = {
DATABASE_URL: process.env.DATABASE_URL,
UPSTASH_REDIS_REST_URL: process.env.UPSTASH_REDIS_REST_URL,
UPSTASH_REDIS_REST_TOKEN: process.env.UPSTASH_REDIS_REST_TOKEN,
};

for (const [key, { schedule, job }] of Object.entries(CRON_SCHEDULERS)) {
new sst.aws.Cron(key, { job, schedule });
}
// TODO: Temporary remove Secrets
// const DATABASE_URL = new sst.Secret(
// "DATABASE_URL",
// environment.DATABASE_URL,
// );
// const UPSTASH_REDIS_REST_URL = new sst.Secret(
// "UPSTASH_REDIS_REST_URL",
// environment.UPSTASH_REDIS_REST_URL,
// );
// const UPSTASH_REDIS_REST_TOKEN = new sst.Secret(
// "UPSTASH_REDIS_REST_TOKEN",
// environment.UPSTASH_REDIS_REST_TOKEN,
// );

// TODO: Might create an API in the future
new sst.aws.Function("Updater", {
link: [DATABASE_URL, UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN],
handler: "src/index.handler",
environment,
url: true,
});
},
Expand Down
19 changes: 19 additions & 0 deletions trigger.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineConfig } from "@trigger.dev/sdk/v3";

export default defineConfig({
project: "proj_hdsezzyvvwvwzpyzlrat",
runtime: "node",
logLevel: "log",
maxDuration: 60,
retries: {
enabledInDev: true,
default: {
maxAttempts: 3,
minTimeoutInMs: 1000,
maxTimeoutInMs: 10000,
factor: 2,
randomize: true,
},
},
dirs: ["./src/trigger"],
});
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
},
"types": ["node"]
},
"include": ["src/**/*", "drizzle.config.ts", "sst.config.ts", "sst-env.d.ts"],
"include": [
"src/**/*",
"drizzle.config.ts",
"sst.config.ts",
"sst-env.d.ts",
"trigger.config.ts"
],
"exclude": ["node_modules", "dist"]
}
Loading