Skip to content

Commit 3e0145e

Browse files
committed
tested retry
1 parent 410bebe commit 3e0145e

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

spec/DefinedSchemas.spec.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,11 @@ describe('DefinedSchemas', () => {
501501
expect(before).toEqual(true);
502502
expect(server).toBeDefined();
503503
});
504-
it('should use logger in case of error', async () => {
504+
it('should use logger in case of error after 3 retries', async () => {
505505
const server = await reconfigureServer({ schemas: [{ className: '_User' }] });
506-
507506
const error = new Error('A test error');
508507
const logger = require('../lib/logger').logger;
508+
spyOn(DefinedSchemas.prototype, 'wait').and.resolveTo();
509509
spyOn(logger, 'error').and.callThrough();
510510
spyOn(Parse.Schema, 'all').and.callFake(async () => {
511511
throw error;
@@ -517,5 +517,10 @@ describe('DefinedSchemas', () => {
517517
).execute();
518518

519519
expect(logger.error).toHaveBeenCalledWith(error);
520+
expect(DefinedSchemas.prototype.wait).toHaveBeenCalledTimes(3);
521+
const calls = DefinedSchemas.prototype.wait.calls.all();
522+
expect(calls[0].args[0]).toEqual(1000);
523+
expect(calls[1].args[0]).toEqual(2000);
524+
expect(calls[2].args[0]).toEqual(3000);
520525
});
521526
});

src/DefinedSchemas.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ export class DefinedSchemas {
4545
}
4646

4747
async execute() {
48+
let timeout;
4849
try {
4950
// Set up a time out in production
5051
// if we fail to get schema
5152
// pm2 or K8s and many other process managers will try to restart the process
5253
// after the exit
53-
const timeout = setTimeout(() => {
54+
timeout = setTimeout(() => {
5455
if (process.env.NODE_ENV === 'production') process.exit(1);
5556
}, 20000);
5657
// Hack to force session schema to be created
@@ -60,13 +61,14 @@ export class DefinedSchemas {
6061
await Promise.all(this.localSchemas.map(async localSchema => this.saveOrUpdate(localSchema)));
6162
await this.enforceCLPForNonProvidedClass();
6263
} catch (e) {
63-
if (this.retries <= this.maxRetries) {
64+
if (timeout) clearTimeout(timeout);
65+
if (this.retries < this.maxRetries) {
6466
this.retries++;
6567
// first retry 1sec, 2sec, 3sec total 6sec retry sequence
6668
// retry will only happen in case of deploying multi parse server instance
6769
// at the same time
6870
// modern systems like k8 avoid this by doing rolling updates
69-
await new Promise(resolve => setTimeout(resolve, 1000 * this.retries));
71+
await this.wait(1000 * this.retries);
7072
await this.execute();
7173
} else {
7274
logger.error(e);
@@ -75,6 +77,11 @@ export class DefinedSchemas {
7577
}
7678
}
7779

80+
// Required for testing purpose
81+
async wait(time) {
82+
await new Promise(resolve => setTimeout(resolve, time));
83+
}
84+
7885
async enforceCLPForNonProvidedClass() {
7986
const nonProvidedClasses = this.allCloudSchemas.filter(
8087
cloudSchema =>

0 commit comments

Comments
 (0)