-
Notifications
You must be signed in to change notification settings - Fork 0
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
Balint Dolla
committed
Nov 9, 2023
1 parent
16d211b
commit 35fa651
Showing
14 changed files
with
126 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { z } from "zod"; | ||
|
||
export const BaseEvent = z.object({ | ||
eventId: z.string().optional(), | ||
}); | ||
|
||
export type BaseEvent = z.infer<typeof BaseEvent>; |
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,4 @@ | ||
export const iceBreaker = "iceBreaker"; | ||
export const daily = "daily"; | ||
|
||
export type ScheduledEventType = typeof iceBreaker | typeof daily; |
4 changes: 1 addition & 3 deletions
4
packages/core/events/index.ts → packages/core/types/events/index.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,46 @@ | ||
import type { StackContext } from "sst/constructs"; | ||
import { Cron, use } from "sst/constructs"; | ||
import { Cron } from "sst/constructs"; | ||
|
||
import { ConfigStack } from "./ConfigStack"; | ||
import { daily, iceBreaker } from "@/types/cron"; | ||
|
||
export function CronStack({ stack }: StackContext) { | ||
const secrets = use(ConfigStack); | ||
import { getFunctionProps } from "./getFunctionProps"; | ||
|
||
const environment = { | ||
DB_URL: process.env.DB_URL || "", | ||
}; | ||
export function CronStack({ stack }: StackContext) { | ||
const functionProps = getFunctionProps(); | ||
|
||
const icebreakerCron = new Cron(stack, "Cron", { | ||
new Cron(stack, "IceBreakerCron", { | ||
job: { | ||
function: { | ||
environment, | ||
handler: "packages/functions/cron/iceBreakerQuestions.handler", | ||
runtime: "nodejs18.x", | ||
...functionProps, | ||
}, | ||
}, | ||
cdk: { | ||
rule: { | ||
eventPattern: { | ||
detailType: [iceBreaker], | ||
}, | ||
}, | ||
}, | ||
// Every first Tuesday of the month at 11:00 UTC | ||
schedule: "cron(0 11 ? * 3#1 *)", | ||
}); | ||
|
||
icebreakerCron.bind(secrets); | ||
|
||
const dailyCron = new Cron(stack, "DailyCron", { | ||
new Cron(stack, "DailyCron", { | ||
job: { | ||
function: { | ||
environment, | ||
handler: "packages/functions/cron/daily.handler", | ||
runtime: "nodejs18.x", | ||
...functionProps, | ||
}, | ||
}, | ||
cdk: { | ||
rule: { | ||
eventPattern: { | ||
detailType: [daily], | ||
}, | ||
}, | ||
}, | ||
// Every day at 11:00 UTC | ||
schedule: "cron(0 11 ? * * *)", | ||
}); | ||
|
||
dailyCron.bind(secrets); | ||
} |
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,13 @@ | ||
import { EventBus, type StackContext } from "sst/constructs"; | ||
|
||
export function EventBusStack({ stack }: StackContext) { | ||
const eventBus = new EventBus(stack, "EventBus", {}); | ||
|
||
stack.addOutputs({ | ||
eventBusName: eventBus.eventBusName, | ||
}); | ||
|
||
return { | ||
eventBus, | ||
}; | ||
} |
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,23 @@ | ||
import { use } from "sst/constructs"; | ||
|
||
import { ConfigStack } from "./ConfigStack"; | ||
import { EventBusStack } from "./EventBusStack"; | ||
import { StorageStack } from "./StorageStack"; | ||
|
||
export const getFunctionProps = () => { | ||
const secrets = use(ConfigStack); | ||
const { db } = use(StorageStack); | ||
const { eventBus } = use(EventBusStack); | ||
|
||
const bind = [...secrets, ...(db ? [db] : [])]; | ||
|
||
return { | ||
permissions: [eventBus], | ||
environment: { | ||
EVENT_BUS_NAME: eventBus.eventBusName, | ||
DB_URL: process.env.DB_URL || "", | ||
}, | ||
bind, | ||
runtime: "nodejs18.x" as const, | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
EventBridgeClient, | ||
PutEventsCommand, | ||
} from "@aws-sdk/client-eventbridge"; | ||
|
||
import type { ScheduledEventType } from "@/types/cron"; | ||
|
||
const eventBridge = new EventBridgeClient(); | ||
|
||
export const sendCronEvent = async ( | ||
type: ScheduledEventType, | ||
eventId: string, | ||
) => { | ||
await eventBridge.send( | ||
new PutEventsCommand({ | ||
Entries: [ | ||
{ | ||
Detail: JSON.stringify({ | ||
eventId, | ||
}), | ||
DetailType: type, | ||
Source: "sst", | ||
}, | ||
], | ||
}), | ||
); | ||
}; |
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