2021-08-17 15:43:26 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
import type { LoggerType } from '../../types/Logging';
|
2021-08-17 15:43:26 +00:00
|
|
|
import { waitForOnline } from '../../util/waitForOnline';
|
|
|
|
import { exponentialBackoffSleepTime } from '../../util/exponentialBackoff';
|
|
|
|
import { isDone as isDeviceLinked } from '../../util/registration';
|
2023-02-24 19:03:17 +00:00
|
|
|
import { sleeper } from '../../util/sleeper';
|
2021-08-17 15:43:26 +00:00
|
|
|
|
|
|
|
export async function commonShouldJobContinue({
|
|
|
|
attempt,
|
|
|
|
log,
|
2021-09-02 22:31:21 +00:00
|
|
|
timeRemaining,
|
2022-04-12 19:41:12 +00:00
|
|
|
skipWait,
|
2021-08-17 15:43:26 +00:00
|
|
|
}: Readonly<{
|
|
|
|
attempt: number;
|
|
|
|
log: LoggerType;
|
2021-09-02 22:31:21 +00:00
|
|
|
timeRemaining: number;
|
2022-04-12 19:41:12 +00:00
|
|
|
skipWait: boolean;
|
2021-08-17 15:43:26 +00:00
|
|
|
}>): Promise<boolean> {
|
|
|
|
if (timeRemaining <= 0) {
|
|
|
|
log.info("giving up because it's been too long");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-03-18 21:48:00 +00:00
|
|
|
await waitForOnline({ timeout: timeRemaining });
|
2021-08-17 15:43:26 +00:00
|
|
|
} catch (err: unknown) {
|
|
|
|
log.info("didn't come online in time, giving up");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
window.storage.onready(resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!isDeviceLinked()) {
|
|
|
|
log.info("skipping this job because we're unlinked");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:41:12 +00:00
|
|
|
if (skipWait) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-17 15:43:26 +00:00
|
|
|
const sleepTime = exponentialBackoffSleepTime(attempt);
|
|
|
|
log.info(`sleeping for ${sleepTime}`);
|
2023-02-24 19:03:17 +00:00
|
|
|
await sleeper.sleep(
|
|
|
|
sleepTime,
|
|
|
|
`commonShouldJobContinue: attempt ${attempt}, skipWait ${skipWait}`
|
|
|
|
);
|
2021-08-17 15:43:26 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|