fix(analytics): restrict publishing to every week (#5028)
This commit is contained in:
1 parent
24560cb93f
commit
8c4be2fbe8
5 files changed
+34
-10
No files matched your search
@@ -117,6 +117,8 @@ const createColumns = (
|
|||||||
header: tTasks("field.interval.label"),
|
header: tTasks("field.interval.label"),
|
||||||
size: 200,
|
size: 200,
|
||||||
Cell({ row }) {
|
Cell({ row }) {
|
||||||
|
if (row.original.preventCustomInterval) return null;
|
||||||
|
|
||||||
const handleIntervalChange = (newCron: string | null) => {
|
const handleIntervalChange = (newCron: string | null) => {
|
||||||
if (!newCron || newCron === row.original.cron) return;
|
if (!newCron || newCron === row.original.cron) return;
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ export class JobManager implements IJobManager {
|
|||||||
if (!validateCron(cron)) {
|
if (!validateCron(cron)) {
|
||||||
throw new Error(`Invalid cron expression: ${cron}`);
|
throw new Error(`Invalid cron expression: ${cron}`);
|
||||||
}
|
}
|
||||||
|
if (job.preventCustomInterval && cron !== job.cronExpression) {
|
||||||
|
throw new Error(`Custom cron expressions are not allowed for job ${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
await this.updateConfigurationAsync(name, { cronExpression: cron });
|
await this.updateConfigurationAsync(name, { cronExpression: cron });
|
||||||
await this.jobGroup.getTask(name)?.destroy();
|
await this.jobGroup.getTask(name)?.destroy();
|
||||||
|
|
||||||
@@ -100,7 +104,13 @@ export class JobManager implements IJobManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getAllAsync(): Promise<
|
public async getAllAsync(): Promise<
|
||||||
{ name: JobGroupKeys; cron: string; preventManualExecution: boolean; isEnabled: boolean }[]
|
{
|
||||||
|
name: JobGroupKeys;
|
||||||
|
cron: string;
|
||||||
|
preventManualExecution: boolean;
|
||||||
|
preventCustomInterval: boolean;
|
||||||
|
isEnabled: boolean;
|
||||||
|
}[]
|
||||||
> {
|
> {
|
||||||
const configurations = await this.db.query.cronJobConfigurations.findMany();
|
const configurations = await this.db.query.cronJobConfigurations.findMany();
|
||||||
|
|
||||||
@@ -110,6 +120,7 @@ export class JobManager implements IJobManager {
|
|||||||
name,
|
name,
|
||||||
cron: config?.cronExpression ?? job.cronExpression,
|
cron: config?.cronExpression ?? job.cronExpression,
|
||||||
preventManualExecution: job.preventManualExecution,
|
preventManualExecution: job.preventManualExecution,
|
||||||
|
preventCustomInterval: job.preventCustomInterval,
|
||||||
isEnabled: config?.isEnabled ?? true,
|
isEnabled: config?.isEnabled ?? true,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,7 +17,15 @@ export interface IJobManager {
|
|||||||
updateIntervalAsync(name: JobGroupKeys, cron: string): Promise<void>;
|
updateIntervalAsync(name: JobGroupKeys, cron: string): Promise<void>;
|
||||||
disableAsync(name: JobGroupKeys): Promise<void>;
|
disableAsync(name: JobGroupKeys): Promise<void>;
|
||||||
enableAsync(name: JobGroupKeys): Promise<void>;
|
enableAsync(name: JobGroupKeys): Promise<void>;
|
||||||
getAllAsync(): Promise<{ name: JobGroupKeys; cron: string; preventManualExecution: boolean; isEnabled: boolean }[]>;
|
getAllAsync(): Promise<
|
||||||
|
{
|
||||||
|
name: JobGroupKeys;
|
||||||
|
cron: string;
|
||||||
|
preventManualExecution: boolean;
|
||||||
|
preventCustomInterval: boolean;
|
||||||
|
isEnabled: boolean;
|
||||||
|
}[]
|
||||||
|
>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const t = initTRPC
|
const t = initTRPC
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ export interface CreateCronJobCreatorOptions<TAllowedNames extends string> {
|
|||||||
interface CreateCronJobOptions {
|
interface CreateCronJobOptions {
|
||||||
runOnStart?: boolean;
|
runOnStart?: boolean;
|
||||||
preventManualExecution?: boolean;
|
preventManualExecution?: boolean;
|
||||||
|
preventCustomInterval?: boolean;
|
||||||
expectedMaximumDurationInMillis?: number;
|
expectedMaximumDurationInMillis?: number;
|
||||||
beforeStart?: () => MaybePromise<void>;
|
beforeStart?: () => MaybePromise<void>;
|
||||||
}
|
}
|
||||||
@@ -78,14 +79,14 @@ const createCallback = <TAllowedNames extends string, TName extends TAllowedName
|
|||||||
where: (cronJobConfigurations, { eq }) => eq(cronJobConfigurations.name, name),
|
where: (cronJobConfigurations, { eq }) => eq(cronJobConfigurations.name, name),
|
||||||
});
|
});
|
||||||
|
|
||||||
const scheduledTask = createTask(
|
const cronExpression = options.preventCustomInterval
|
||||||
configuration?.cronExpression ?? defaultCronExpression,
|
? defaultCronExpression
|
||||||
() => void catchingCallbackAsync(),
|
: (configuration?.cronExpression ?? defaultCronExpression);
|
||||||
{
|
|
||||||
name,
|
const scheduledTask = createTask(cronExpression, () => void catchingCallbackAsync(), {
|
||||||
timezone: creatorOptions.timezone,
|
name,
|
||||||
},
|
timezone: creatorOptions.timezone,
|
||||||
);
|
});
|
||||||
creatorOptions.logger.logDebug("The scheduled task for cron job was created", {
|
creatorOptions.logger.logDebug("The scheduled task for cron job was created", {
|
||||||
name,
|
name,
|
||||||
cronExpression: defaultCronExpression,
|
cronExpression: defaultCronExpression,
|
||||||
@@ -114,6 +115,7 @@ const createCallback = <TAllowedNames extends string, TName extends TAllowedName
|
|||||||
await catchingCallbackAsync();
|
await catchingCallbackAsync();
|
||||||
},
|
},
|
||||||
preventManualExecution: options.preventManualExecution ?? false,
|
preventManualExecution: options.preventManualExecution ?? false,
|
||||||
|
preventCustomInterval: options.preventCustomInterval ?? false,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { createCronJob } from "../lib";
|
|||||||
export const analyticsJob = createCronJob("analytics", EVERY_WEEK, {
|
export const analyticsJob = createCronJob("analytics", EVERY_WEEK, {
|
||||||
runOnStart: true,
|
runOnStart: true,
|
||||||
preventManualExecution: true,
|
preventManualExecution: true,
|
||||||
|
preventCustomInterval: true,
|
||||||
}).withCallback(async () => {
|
}).withCallback(async () => {
|
||||||
if (env.NO_EXTERNAL_CONNECTION) return;
|
if (env.NO_EXTERNAL_CONNECTION) return;
|
||||||
const analyticSetting = await getServerSettingByKeyAsync(db, "analytics");
|
const analyticSetting = await getServerSettingByKeyAsync(db, "analytics");
|
||||||
|
|||||||
Reference in new issue
Block a user