refactor: Add jobId hook to BaseTask for deduplicating queued jobs (#13890)

* refactor: Add jobId hook to BaseTask for deduplicating queued jobs

* fix: Drop jobId from APIImportTask, it re-schedules itself while active
This commit is contained in:
Tom Moor authored and GitHub committed 2026-09-25 23:44:16 +00:00
1 parent ed4aaf274d
commit bc992fb22a
15 files changed
+71 -17

No files matched your search

@@ -8,6 +8,13 @@ type Props = {
};
export default class GitHubWebhookTask extends BaseTask<Props> {
protected jobId({ headers }: Props) {
const deliveryId = headers["x-github-delivery"];
return typeof deliveryId === "string"
? `github-webhook:${deliveryId}`
: undefined;
}
public async perform({ headers, payload }: Props): Promise<void> {
const plugins = PluginManager.getHooks(Hook.IssueProvider);
const plugin = plugins.find(
@@ -8,6 +8,10 @@ type Props = {
};
export default class CacheIssueSourcesTask extends BaseTask<Props> {
protected jobId({ integrationId }: Props) {
return `cache-issue-sources:${integrationId}`;
}
async perform({ integrationId }: Props) {
const integration = await Integration.findByPk(integrationId);
if (!integration) {
@@ -8,6 +8,10 @@ type Props = {
};
export default class CleanupDeletedTeamTask extends BaseTask<Props> {
protected jobId({ teamId }: Props) {
return `cleanup-team:${teamId}`;
}
public async perform({ teamId }: Props) {
const team = await Team.findByPk(teamId, {
paranoid: false,
@@ -27,14 +27,9 @@ export default class CleanupDeletedTeamsTask extends CronTask {
});
for (const team of teams) {
await new CleanupDeletedTeamTask().schedule(
{
teamId: team.id,
},
{
jobId: `cleanup-team:${team.id}`,
}
);
await new CleanupDeletedTeamTask().schedule({
teamId: team.id,
});
}
}
@@ -13,6 +13,10 @@ type Props = {
* currently this is done by destroying associated Api Keys and disabling webhooks.
*/
export default class CleanupDemotedUserTask extends BaseTask<Props> {
protected jobId({ userId }: Props) {
return `cleanup-demoted-user:${userId}`;
}
public async perform(props: Props) {
const user = await User.scope("withTeam").findByPk(props.userId);
@@ -7,6 +7,10 @@ type Props = {
};
export default class DeleteAttachmentTask extends BaseTask<Props> {
protected jobId({ attachmentId }: Props) {
return `delete-attachment:${attachmentId}`;
}
public async perform({ attachmentId, teamId }: Props) {
const attachment = await Attachment.findOne({
where: {
@@ -12,6 +12,10 @@ type Props = {
};
export default class DetachDraftsFromCollectionTask extends BaseTask<Props> {
protected jobId({ collectionId }: Props) {
return `detach-drafts:${collectionId}`;
}
async perform(props: Props) {
const [collection, actor] = await Promise.all([
Collection.findByPk(props.collectionId, {
@@ -18,6 +18,10 @@ type Props = {
};
export default class DuplicateCollectionDocumentsTask extends BaseTask<Props> {
protected jobId({ collectionId }: Props) {
return `duplicate-collection:${collectionId}`;
}
async perform(props: Props) {
const [collection, original, actor] = await Promise.all([
Collection.findByPk(props.collectionId),
+4
View File
@@ -32,6 +32,10 @@ type Props = {
};
export default abstract class ExportTask extends BaseTask<Props> {
protected jobId({ fileOperationId }: Props) {
return `export:${fileOperationId}`;
}
/**
* Transforms the data to be exported, uploads, and notifies user.
*
@@ -16,6 +16,10 @@ type Props = {
* dynamically registered OAuth clients in a team.
*/
export default class RevokeDynamicOAuthClientsTask extends BaseTask<Props> {
protected jobId({ teamId }: Props) {
return `revoke-dynamic-oauth-clients:${teamId}`;
}
public async perform({ teamId }: Props) {
const clients = await OAuthClient.findAll({
attributes: ["id"],
@@ -28,6 +28,10 @@ type Props = RevokeScope & {
* read, so that the record – which embeds the document title and text – does not outlive access.
*/
export default class RevokeUserNotificationsTask extends BaseTask<Props> {
protected jobId({ userId, documentId, collectionId }: Props) {
return `revoke-notifications:${userId}:${documentId ?? collectionId ?? "all"}`;
}
public async perform({ userId, documentId, collectionId }: Props) {
const user = await User.findByPk(userId);
@@ -11,6 +11,10 @@ type Props = {
* A task that updates the team stats.
*/
export default class UpdateTeamAttachmentsSizeTask extends BaseTask<Props> {
protected jobId({ teamId }: Props) {
return `update-team-attachments-size:${teamId}`;
}
public async perform({ teamId }: Props) {
const sizeInBytes = await Attachment.getTotalSizeForTeam(
sequelizeReadOnly,
@@ -9,6 +9,10 @@ type Props = {
};
export default class ValidateSSOAccessTask extends BaseTask<Props> {
protected jobId({ userId }: Props) {
return `validate-sso:${userId}`;
}
public async perform({ userId }: Props) {
await MutexLock.using(
`validateSSO:${userId}`,
+14 -1
View File
@@ -17,12 +17,13 @@ export abstract class BaseTask<T extends object> {
* @returns A promise that resolves once the job is placed on the task queue
*/
public schedule(props: T, options?: JobOptions): Promise<Job> {
const jobId = this.jobId(props) ?? options?.jobId;
return taskQueue().add(
{
name: this.constructor.name,
props,
},
{ ...options, ...this.options }
{ ...options, jobId, ...this.options }
);
}
@@ -45,6 +46,18 @@ export abstract class BaseTask<T extends object> {
return Promise.resolve();
}
/**
* A stable ID for the job. When defined, a job with the same ID that is
* already queued or active prevents a duplicate from being added.
*
* @param props Properties to be used by the task
* @returns The job ID, or undefined to let the queue assign one.
*/
// oxlint-disable-next-line @typescript-eslint/no-unused-vars
protected jobId(props: T): string | undefined {
return undefined;
}
/**
* Job options such as priority and retry strategy.
*/
+3 -8
View File
@@ -148,14 +148,9 @@ router.post("auth.info", auth(), async (ctx: APIContext<T.AuthInfoReq>) => {
user.lastSignedInAt < subHours(new Date(), 1)
) {
await new ValidateSSOAccessTask()
.schedule(
{
userId: user.id,
},
{
jobId: `validate-sso:${user.id}`,
}
)
.schedule({
userId: user.id,
})
.catch(() => {
// Ignore errors from duplicate jobId when a validation is already queued
});