signal-desktop/ts/jobs/viewSyncJobQueue.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as durations from '../util/durations';
import type { LoggerType } from '../types/Logging';
import { exponentialBackoffMaxAttempts } from '../util/exponentialBackoff';
import type { SyncType } from './helpers/syncHelpers';
2021-08-30 22:59:34 +00:00
import {
SyncTypeList,
2021-08-30 22:59:34 +00:00
parseRawSyncDataArray,
runSyncJob,
} from './helpers/syncHelpers';
2021-08-30 22:59:34 +00:00
import { strictAssert } from '../util/assert';
import { isRecord } from '../util/isRecord';
import type { JOB_STATUS } from './JobQueue';
import { JobQueue } from './JobQueue';
import { jobQueueDatabaseStore } from './JobQueueDatabaseStore';
const MAX_RETRY_TIME = durations.DAY;
2021-08-30 22:59:34 +00:00
export type ViewSyncJobData = {
viewSyncs: Array<SyncType>;
};
export class ViewSyncJobQueue extends JobQueue<ViewSyncJobData> {
protected parseData(data: unknown): ViewSyncJobData {
2021-08-30 22:59:34 +00:00
strictAssert(isRecord(data), 'data is not an object');
return { viewSyncs: parseRawSyncDataArray(data.viewSyncs) };
}
protected async run(
{ data, timestamp }: Readonly<{ data: ViewSyncJobData; timestamp: number }>,
{ attempt, log }: Readonly<{ attempt: number; log: LoggerType }>
): Promise<typeof JOB_STATUS.NEEDS_RETRY | undefined> {
await runSyncJob({
attempt,
log,
maxRetryTime: MAX_RETRY_TIME,
syncs: data.viewSyncs,
timestamp,
type: SyncTypeList.View,
});
return undefined;
}
}
export const viewSyncJobQueue = new ViewSyncJobQueue({
store: jobQueueDatabaseStore,
queueType: 'view sync',
maxAttempts: exponentialBackoffMaxAttempts(MAX_RETRY_TIME),
});