Skip to content

Commit ca6b526

Browse files
author
Deathgiver
committed
feat(worker): refresh zalo and tiktok tokens twice daily
Zalo and TikTok tokens only live ~24-25h, so the single 02:00 cron left a ~1h margin: one missed or late run and every channel expired mid-day. The refreshChannelTokens job now accepts an optional channel filter and a second scheduler runs the two short-lived channels again at 14:00, keeping them at most ~12h from a refresh. Long-lived Meta/WhatsApp tokens stay on the daily run.
1 parent dd80571 commit ca6b526

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

apps/worker/src/schedule/handlers/refresh-channel-tokens.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ const refreshTokenAdapter: Record<
2929
telegram: undefined,
3030
}
3131

32-
export async function refreshChannelTokens(): Promise<void> {
32+
export async function refreshChannelTokens(
33+
channels?: ChannelType[],
34+
): Promise<void> {
3335
const entries = Object.entries(refreshTokenAdapter).filter(
3436
(entry): entry is [ChannelType, () => Promise<void>] =>
35-
entry[1] !== undefined,
37+
entry[1] !== undefined &&
38+
(!channels || channels.includes(entry[0] as ChannelType)),
3639
)
3740

3841
const results = await Promise.allSettled(

apps/worker/src/schedule/handlers/register-schedules.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { channelTypes } from "@chatbotx.io/database/partials"
12
import {
23
PURGE_WORKSPACES_INTERVAL_MINUTES,
34
ScheduleJobData,
@@ -294,6 +295,26 @@ export const registerSchedules = async () => {
294295
},
295296
)
296297

298+
// Zalo and TikTok tokens only live ~24-25h, so the single 02:00 run leaves
299+
// almost no margin: one missed run and they expire mid-day. This extra
300+
// midday run keeps them at most ~12h from a refresh; the long-lived
301+
// Meta/WhatsApp tokens stay on the daily run above.
302+
await scheduleQueue.upsertJobScheduler(
303+
"refreshShortLivedChannelTokens",
304+
{
305+
pattern: "0 14 * * *",
306+
},
307+
{
308+
name: ScheduleJobData.refreshChannelTokens,
309+
data: {
310+
type: ScheduleJobData.refreshChannelTokens,
311+
data: {
312+
channels: [channelTypes.enum.zalo, channelTypes.enum.tiktok],
313+
},
314+
},
315+
},
316+
)
317+
297318
if (isCloud) {
298319
await scheduleQueue.upsertJobScheduler(
299320
ScheduleJobData.unsubscribeExpiredTrials,

apps/worker/src/schedule/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ async function startScheduleWorker() {
141141
return
142142

143143
case ScheduleJobData.refreshChannelTokens:
144-
await refreshChannelTokens()
144+
await refreshChannelTokens(job.data.data.channels)
145145
return
146146

147147
case ScheduleJobData.unsubscribeExpiredTrials:

packages/worker-config/src/queues/schedule/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ChannelType } from "@chatbotx.io/database/partials"
12
import { Queue } from "bullmq"
23
import { z } from "zod"
34
import {
@@ -168,7 +169,9 @@ export type ScheduleJobPurgeAutomationThrottle = {
168169

169170
export type ScheduleJobRefreshChannelTokens = {
170171
type: typeof ScheduleJobData.refreshChannelTokens
171-
data: Record<string, never>
172+
// No `channels` = refresh every channel. The short-lived scheduler passes
173+
// ["zalo", "tiktok"] for the extra midday run (see register-schedules.ts).
174+
data: { channels?: ChannelType[] }
172175
}
173176

174177
export type ScheduleJobUnsubscribeExpiredTrials = {

0 commit comments

Comments
 (0)