signal-desktop/ts/jobs/readSyncJobQueue.ts

54 lines
1.5 KiB
TypeScript

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable class-methods-use-this */
import * as z from 'zod';
import * as moment from 'moment';
import { exponentialBackoffMaxAttempts } from '../util/exponentialBackoff';
import { runReadOrViewSyncJob } from './helpers/runReadOrViewSyncJob';
import { JobQueue } from './JobQueue';
import { jobQueueDatabaseStore } from './JobQueueDatabaseStore';
const MAX_RETRY_TIME = moment.duration(1, 'day').asMilliseconds();
const readSyncJobDataSchema = z.object({
readSyncs: z.array(
z.object({
messageId: z.string().optional(),
senderE164: z.string().optional(),
senderUuid: z.string().optional(),
timestamp: z.number(),
})
),
});
export type ReadSyncJobData = z.infer<typeof readSyncJobDataSchema>;
export class ReadSyncJobQueue extends JobQueue<ReadSyncJobData> {
protected parseData(data: unknown): ReadSyncJobData {
return readSyncJobDataSchema.parse(data);
}
protected async run(
{ data, timestamp }: Readonly<{ data: ReadSyncJobData; timestamp: number }>,
{ attempt }: Readonly<{ attempt: number }>
): Promise<void> {
await runReadOrViewSyncJob({
attempt,
isView: false,
maxRetryTime: MAX_RETRY_TIME,
syncs: data.readSyncs,
timestamp,
});
}
}
export const readSyncJobQueue = new ReadSyncJobQueue({
store: jobQueueDatabaseStore,
queueType: 'read sync',
maxAttempts: exponentialBackoffMaxAttempts(MAX_RETRY_TIME),
});