-
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.
cron/workerを分離
- Loading branch information
Showing
10 changed files
with
149 additions
and
86 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,46 @@ | ||
import { CronJob } from "cron"; | ||
import { ResWaitCountKey } from "./entities"; | ||
import { CheckDeadTopicJob } from "./jobs/CheckDeadTopicJob"; | ||
import { faktoryClient } from "./faktoryClient"; | ||
import { createJob } from "./jobs/JobType"; | ||
import { UserPointResetJob } from "./jobs/UserPointResetJob"; | ||
import { UserCountResetJob } from "./jobs/UserCountResetJob"; | ||
|
||
export function startCron() { | ||
const startUserCountResetCron = (cronTime: string, key: ResWaitCountKey) => { | ||
new CronJob({ | ||
cronTime, | ||
onTick: () => { | ||
void faktoryClient.push( | ||
createJob(faktoryClient, UserCountResetJob, { key }) | ||
); | ||
}, | ||
start: false, | ||
timeZone: "Asia/Tokyo", | ||
}).start(); | ||
}; | ||
startUserCountResetCron("00 00,10,20,30,40,50 * * * *", "m10"); | ||
startUserCountResetCron("00 00,30 * * * *", "m30"); | ||
startUserCountResetCron("00 00 * * * *", "h1"); | ||
startUserCountResetCron("00 00 00,06,12,18 * * *", "h6"); | ||
startUserCountResetCron("00 00 00,12 * * *", "h12"); | ||
startUserCountResetCron("00 00 00 * * *", "d1"); | ||
|
||
new CronJob({ | ||
cronTime: "00 00 00 * * *", | ||
onTick: () => { | ||
void faktoryClient.push(createJob(faktoryClient, UserPointResetJob, {})); | ||
}, | ||
start: false, | ||
timeZone: "Asia/Tokyo", | ||
}).start(); | ||
|
||
new CronJob({ | ||
cronTime: "00 00 * * * *", | ||
onTick: () => { | ||
void faktoryClient.push(createJob(faktoryClient, CheckDeadTopicJob, {})); | ||
}, | ||
start: false, | ||
timeZone: "Asia/Tokyo", | ||
}).start(); | ||
} |
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"; | ||
import { JobType } from "./JobType"; | ||
|
||
export const CheckDeadTopicJob = JobType({ | ||
type: "CheckDeadTopic", | ||
arg: z.object({}), | ||
}); |
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 { z } from "zod"; | ||
import { Job, Client, Worker } from "faktory-worker"; | ||
|
||
export interface JobType<A> { | ||
type: string; | ||
arg: z.ZodType<A>; | ||
} | ||
|
||
// 型推論のためのヘルパー関数 | ||
export function JobType<A>(value: JobType<A>): JobType<A> { | ||
return value; | ||
} | ||
|
||
export function createJob<A>(client: Client, type: JobType<A>, arg: A): Job { | ||
return client.job(type.type, arg); | ||
} | ||
|
||
export function registerWorker<A>( | ||
worker: Worker, | ||
type: JobType<A>, | ||
handler: (arg: A) => Promise<void> | ||
): void { | ||
worker.register(type.type, async (raw) => { | ||
const arg = type.arg.parse(raw); | ||
await handler(arg); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
import { z } from "zod"; | ||
import { JobType } from "./JobType"; | ||
|
||
export const SendPushNotificationJob = JobType({ | ||
type: "SendPushNotification", | ||
arg: z.object({ | ||
pushSubscription: z.object({ | ||
endpoint: z.string(), | ||
keys: z.object({ | ||
p256dh: z.string(), | ||
auth: z.string(), | ||
}), | ||
}), | ||
payload: z.string(), | ||
userId: z.string(), | ||
}), | ||
}); |
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,16 @@ | ||
import { JobType } from "./JobType"; | ||
import { z } from "zod"; | ||
|
||
export const UserCountResetJob = JobType({ | ||
type: "UserCountReset", | ||
arg: z.object({ | ||
key: z.union([ | ||
z.literal("m10"), | ||
z.literal("m30"), | ||
z.literal("h1"), | ||
z.literal("h6"), | ||
z.literal("h12"), | ||
z.literal("d1"), | ||
]), | ||
}), | ||
}); |
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"; | ||
import { JobType } from "./JobType"; | ||
|
||
export const UserPointResetJob = JobType({ | ||
type: "UserPointReset", | ||
arg: z.object({}), | ||
}); |
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,82 +1,38 @@ | ||
import { CronJob } from "cron"; | ||
import { Logger, TopicRepo, UserRepo } from "./adapters"; | ||
import { ResWaitCountKey } from "./entities"; | ||
import { prisma } from "./prisma-client"; | ||
import faktory from "faktory-worker"; | ||
import { Config } from "./config"; | ||
import * as SendPushNotificationJob from "./jobs/SendPushNotification"; | ||
import { SendPushNotificationJob } from "./jobs/SendPushNotificationJob"; | ||
import { CheckDeadTopicJob } from "./jobs/CheckDeadTopicJob"; | ||
import { createPorts, PortsConfig } from "./createPorts"; | ||
import { registerWorker } from "./jobs/JobType"; | ||
import { UserPointResetJob } from "./jobs/UserPointResetJob"; | ||
import { UserCountResetJob } from "./jobs/UserCountResetJob"; | ||
|
||
export async function runWorker(): Promise<void> { | ||
export async function startWorker(): Promise<void> { | ||
const worker = await faktory.work({ | ||
url: Config.faktory.url, | ||
}); | ||
|
||
worker.register(SendPushNotificationJob.JobType, async (raw) => { | ||
registerWorker(worker, SendPushNotificationJob, async (arg) => { | ||
const ports = createPorts(PortsConfig); | ||
const arg = SendPushNotificationJob.Arg.parse(raw); | ||
await ports.notificationSender.sendNotification( | ||
arg.userId, | ||
arg.pushSubscription, | ||
arg.payload | ||
); | ||
}); | ||
|
||
runTopicWorker(); | ||
runUserWorker(); | ||
} | ||
|
||
function runTopicWorker() { | ||
// 毎時間トピ落ちチェック | ||
new CronJob({ | ||
cronTime: "00 00 * * * *", | ||
onTick: () => { | ||
void (async () => { | ||
const logger = new Logger(); | ||
const topicRepo = new TopicRepo(prisma); | ||
|
||
logger.info("TopicCron"); | ||
await topicRepo.cronTopicCheck(new Date()); | ||
})(); | ||
}, | ||
start: false, | ||
timeZone: "Asia/Tokyo", | ||
}).start(); | ||
} | ||
|
||
function runUserWorker() { | ||
const start = (cronTime: string, key: ResWaitCountKey) => { | ||
new CronJob({ | ||
cronTime, | ||
onTick: () => { | ||
void (async () => { | ||
const logger = new Logger(); | ||
const userRepo = new UserRepo(prisma); | ||
registerWorker(worker, CheckDeadTopicJob, async (_arg) => { | ||
const ports = createPorts(PortsConfig); | ||
await ports.topicRepo.cronTopicCheck(ports.clock.now()); | ||
}); | ||
|
||
logger.info(`UserCron ${key}`); | ||
await userRepo.cronCountReset(key); | ||
})(); | ||
}, | ||
start: false, | ||
timeZone: "Asia/Tokyo", | ||
}).start(); | ||
}; | ||
registerWorker(worker, UserPointResetJob, async (_arg) => { | ||
const ports = createPorts(PortsConfig); | ||
await ports.userRepo.cronPointReset(); | ||
}); | ||
|
||
start("00 00,10,20,30,40,50 * * * *", "m10"); | ||
start("00 00,30 * * * *", "m30"); | ||
start("00 00 * * * *", "h1"); | ||
start("00 00 00,06,12,18 * * *", "h6"); | ||
start("00 00 00,12 * * *", "h12"); | ||
start("00 00 00 * * *", "d1"); | ||
new CronJob({ | ||
cronTime: "00 00 00 * * *", | ||
onTick: () => { | ||
void (async () => { | ||
const userRepo = new UserRepo(prisma); | ||
await userRepo.cronPointReset(); | ||
})(); | ||
}, | ||
start: false, | ||
timeZone: "Asia/Tokyo", | ||
}).start(); | ||
registerWorker(worker, UserCountResetJob, async (arg) => { | ||
const ports = createPorts(PortsConfig); | ||
await ports.userRepo.cronCountReset(arg.key); | ||
}); | ||
} |