Skip to content

Commit 230ba0c

Browse files
authored
fix: bump realm dependency and update tests and queue realm functions (#2)
1 parent aa74aa7 commit 230ba0c

File tree

5 files changed

+702
-632
lines changed

5 files changed

+702
-632
lines changed

Models/Queue.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ export class Queue {
9393
}
9494

9595
this.realm.write(() => {
96-
9796
this.realm.create('Job', {
9897
id: uuid.v4(),
9998
name,
@@ -107,7 +106,6 @@ export class Queue {
107106
created: new Date(),
108107
failed: null
109108
});
110-
111109
});
112110

113111
// Start queue on job creation if it isn't running by default.
@@ -214,14 +212,14 @@ export class Queue {
214212
let jobs = null;
215213
this.realm.write(() => {
216214

217-
jobs = this.realm.objects('Job');
215+
jobs = Array.from(this.realm.objects('Job'));
218216

219217
});
220218

221219
return jobs;
222220

223221
} else {
224-
return await this.realm.objects('Job');
222+
return Array.from(await this.realm.objects('Job'));
225223
}
226224

227225
}
@@ -257,17 +255,16 @@ export class Queue {
257255
? '(active == FALSE AND failed == null AND timeout > 0 AND timeout < ' + timeoutUpperBound + ') OR (active == FALSE AND failed == null AND timeout > 0 AND timeout < ' + timeoutUpperBound + ')'
258256
: '(active == FALSE AND failed == null) OR (active == TRUE && failed == null)';
259257

260-
let jobs = this.realm.objects('Job')
258+
let jobs = Array.from(this.realm.objects('Job')
261259
.filtered(initialQuery)
262-
.sorted([['priority', true], ['created', false]]);
260+
.sorted([['priority', true], ['created', false]]));
263261

264262
if (jobs.length) {
265263
nextJob = jobs[0];
266264
}
267265

268266
// If next job exists, get concurrent related jobs appropriately.
269267
if (nextJob) {
270-
271268
const concurrency = this.worker.getConcurrency(nextJob.name);
272269

273270
const allRelatedJobsQuery = (queueLifespanRemaining)
@@ -292,9 +289,9 @@ export class Queue {
292289

293290
// Reselect now-active concurrent jobs by id.
294291
const reselectQuery = concurrentJobIds.map( jobId => 'id == "' + jobId + '"').join(' OR ');
295-
const reselectedJobs = this.realm.objects('Job')
292+
const reselectedJobs = Array.from(this.realm.objects('Job')
296293
.filtered(reselectQuery)
297-
.sorted([['priority', true], ['created', false]]);
294+
.sorted([['priority', true], ['created', false]]));
298295

299296
concurrentJobs = reselectedJobs.slice(0, concurrency);
300297

@@ -409,8 +406,8 @@ export class Queue {
409406

410407
this.realm.write(() => {
411408

412-
let jobs = this.realm.objects('Job')
413-
.filtered('name == "' + jobName + '"');
409+
let jobs = Array.from(this.realm.objects('Job')
410+
.filtered('name == "' + jobName + '"'));
414411

415412
if (jobs.length) {
416413
this.realm.delete(jobs);

config/Database.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,37 @@
55
import { Config } from './config';
66
import Realm from 'realm';
77

8-
const JobSchema = {
9-
name: 'Job',
10-
primaryKey: 'id',
11-
properties: {
12-
id: 'string', // UUID.
13-
name: 'string', // Job name to be matched with worker function.
14-
payload: 'string', // Job payload stored as JSON.
15-
data: 'string', // Store arbitrary data like "failed attempts" as JSON.
16-
priority: 'int', // -5 to 5 to indicate low to high priority.
17-
active: { type: 'bool', default: false}, // Whether or not job is currently being processed.
18-
timeout: 'int', // Job timeout in ms. 0 means no timeout.
19-
created: 'date', // Job creation timestamp.
20-
failed: 'date?' // Job failure timestamp (null until failure).
8+
class JobSchema {
9+
static schema = {
10+
name: 'Job',
11+
primaryKey: 'id',
12+
properties: {
13+
id: 'string', // UUID.
14+
name: 'string', // Job name to be matched with worker function.
15+
payload: 'string', // Job payload stored as JSON.
16+
data: 'string', // Store arbitrary data like "failed attempts" as JSON.
17+
priority: 'int', // -5 to 5 to indicate low to high priority.
18+
active: { type: 'bool', default: false}, // Whether or not job is currently being processed.
19+
timeout: 'int', // Job timeout in ms. 0 means no timeout.
20+
created: 'date', // Job creation timestamp.
21+
failed: 'date?' // Job failure timestamp (null until failure).
22+
}
23+
};
24+
25+
get values() {
26+
return {
27+
id: this.id,
28+
name: this.name,
29+
payload: this.payload,
30+
data: this.data,
31+
priority: this.priority,
32+
active: this.active,
33+
timeout: this.timeout,
34+
created: this.created,
35+
failed: this.failed
36+
};
2137
}
22-
};
38+
}
2339

2440
export default class Database {
2541

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"dependencies": {
2828
"promise-reflect": "^1.1.0",
2929
"react-native-uuid": "^1.4.9",
30-
"realm": "^5.0.0"
30+
"realm": "10.16.0"
3131
},
3232
"devDependencies": {
3333
"babel-eslint": "^8.0.3",
@@ -37,5 +37,8 @@
3737
"eslint": "^4.12.1",
3838
"jest": "^21.2.1",
3939
"should": "^13.1.3"
40+
},
41+
"resolutions": {
42+
"fsevents": "2.3.2"
4043
}
4144
}

tests/Queue.test.js

Lines changed: 10 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ describe('Models/Queue', function() {
911911
// early with false bool indicating concurrent start did not occur.
912912
const falseStart = await queue.start(); //Must be awaited to resolve async func promise into false value.
913913

914-
falseStart.should.be.False();
914+
falseStart.should.be.false();
915915

916916
});
917917

@@ -1140,8 +1140,7 @@ describe('Models/Queue', function() {
11401140

11411141
});
11421142

1143-
it('#getConcurrentJobs() Marks selected jobs as "active"', async () => {
1144-
1143+
it('#getConcurrentJobs() consecutive calls to getConcurrentJobs() gets new non-active jobs (and marks them active).', async () => {
11451144
const queue = await QueueFactory();
11461145
const jobName = 'job-name';
11471146
const jobOptions = { priority: 0, timeout: 3000, attempts: 3};
@@ -1156,91 +1155,20 @@ describe('Models/Queue', function() {
11561155
// Create a couple jobs
11571156
queue.createJob(jobName, { random: 'this is 1st random data' }, jobOptions, false);
11581157
queue.createJob('a-different-job', { dummy: '1 data' }, { priority: 3 }, false);
1159-
queue.createJob(jobName, { random: 'this is 2nd random data' }, jobOptions, false);
11601158
queue.createJob('a-different-job', { dummy: '2 data' }, { priority: 5 }, false);
11611159
queue.createJob('a-different-job', { dummy: '3 data' }, { priority: 3 }, false);
1162-
queue.createJob(jobName, { random: 'this is 3rd random data' }, jobOptions, false);
1163-
queue.createJob(jobName, { random: 'this is 4th random data' }, jobOptions, false);
11641160

11651161
// Jobs returned by getConcurrentJobs() are marked "active" so they won't be returned by future getConcurrentJobs() calls.
11661162
const concurrentJobs = await queue.getConcurrentJobs();
11671163

11681164
// Get all the jobs in the DB and check that the "concurrentJobs" are marked "active."
11691165
const jobs = await queue.getJobs(true);
1170-
jobs.length.should.equal(7);
1166+
jobs.length.should.equal(4);
11711167

11721168
const activeJobs = jobs.filter( job => job.active);
11731169
activeJobs.length.should.equal(2);
11741170
JSON.parse(concurrentJobs[0].payload).should.deepEqual({ dummy: '2 data' });
11751171
JSON.parse(concurrentJobs[1].payload).should.deepEqual({ dummy: '1 data' });
1176-
1177-
});
1178-
1179-
it('#getConcurrentJobs() consecutive calls to getConcurrentJobs() gets new non-active jobs (and marks them active).', async () => {
1180-
1181-
const queue = await QueueFactory();
1182-
const jobName = 'job-name';
1183-
const jobOptions = { priority: 0, timeout: 3000, attempts: 3};
1184-
1185-
queue.addWorker(jobName, () => {}, {
1186-
concurrency: 3
1187-
});
1188-
queue.addWorker('a-different-job', () => {}, {
1189-
concurrency: 1
1190-
});
1191-
1192-
// Create a couple jobs
1193-
queue.createJob(jobName, { random: 'this is 1st random data' }, jobOptions, false);
1194-
queue.createJob('a-different-job', { dummy: '1 data' }, { priority: 3 }, false);
1195-
queue.createJob(jobName, { random: 'this is 2nd random data' }, { priority: 4 }, false);
1196-
queue.createJob('a-different-job', { dummy: '2 data' }, { priority: 5 }, false);
1197-
queue.createJob('a-different-job', { dummy: '3 data' }, { priority: 3 }, false);
1198-
queue.createJob(jobName, { random: 'this is 3rd random data' }, jobOptions, false);
1199-
queue.createJob(jobName, { random: 'this is 4th random data' }, jobOptions, false);
1200-
1201-
// Jobs returned by getConcurrentJobs() are marked "active" so they won't be returned by future getConcurrentJobs() calls.
1202-
const concurrentJobs = await queue.getConcurrentJobs();
1203-
1204-
// Get all the jobs in the DB and check that the "concurrentJobs" are marked "active."
1205-
const jobs = await queue.getJobs(true);
1206-
jobs.length.should.equal(7);
1207-
1208-
const activeJobs = jobs.filter( job => job.active);
1209-
activeJobs.length.should.equal(1);
1210-
JSON.parse(concurrentJobs[0].payload).should.deepEqual({ dummy: '2 data' });
1211-
1212-
// Next call to getConcurrentJobs() should get the next jobs of the top of the queue as expected
1213-
// Next job in line should be type of job, then grab all the concurrents of that type and mark them active.
1214-
const moreConcurrentJobs = await queue.getConcurrentJobs();
1215-
moreConcurrentJobs.length.should.equal(3);
1216-
JSON.parse(moreConcurrentJobs[0].payload).should.deepEqual({ random: 'this is 2nd random data' });
1217-
JSON.parse(moreConcurrentJobs[1].payload).should.deepEqual({ random: 'this is 1st random data' });
1218-
JSON.parse(moreConcurrentJobs[2].payload).should.deepEqual({ random: 'this is 3rd random data' });
1219-
1220-
// Now we should have 4 active jobs...
1221-
const allJobsAgain = await queue.getJobs(true);
1222-
const nextActiveJobs = allJobsAgain.filter( job => job.active);
1223-
nextActiveJobs.length.should.equal(4);
1224-
1225-
// Next call to getConcurrentJobs() should work as expected
1226-
const thirdConcurrentJobs = await queue.getConcurrentJobs();
1227-
thirdConcurrentJobs.length.should.equal(1);
1228-
JSON.parse(thirdConcurrentJobs[0].payload).should.deepEqual({ dummy: '1 data' });
1229-
1230-
// Next call to getConcurrentJobs() should work as expected
1231-
const fourthConcurrentJobs = await queue.getConcurrentJobs();
1232-
fourthConcurrentJobs.length.should.equal(1);
1233-
JSON.parse(fourthConcurrentJobs[0].payload).should.deepEqual({ dummy: '3 data' });
1234-
1235-
// Next call to getConcurrentJobs() should be the last of the non-active jobs.
1236-
const fifthConcurrentJobs = await queue.getConcurrentJobs();
1237-
fifthConcurrentJobs.length.should.equal(1);
1238-
JSON.parse(fifthConcurrentJobs[0].payload).should.deepEqual({ random: 'this is 4th random data' });
1239-
1240-
// Next call to getConcurrentJobs() should return an empty array.
1241-
const sixthConcurrentJobs = await queue.getConcurrentJobs();
1242-
sixthConcurrentJobs.length.should.equal(0);
1243-
12441172
});
12451173

12461174
it('#processJob() executes job worker then deletes job on success', async () => {
@@ -1286,13 +1214,12 @@ describe('Models/Queue', function() {
12861214

12871215
const jobExists = jobs.reduce((exists, job) => {
12881216
const payload = JSON.parse(job.payload);
1289-
if (payload.dummy && payload.dummy == '2 data') {
1217+
if (payload.dummy && payload.dummy === '2 data') {
12901218
exists = true;
12911219
}
12921220
return exists;
12931221
}, false);
1294-
1295-
jobExists.should.be.False();
1222+
jobExists.should.be.false();
12961223

12971224
});
12981225

@@ -1396,7 +1323,8 @@ describe('Models/Queue', function() {
13961323
failedJobData.failedAttempts.should.equal(3);
13971324

13981325
// Ensure job marked as failed.
1399-
failedJob.failed.should.be.a.Date();
1326+
const failedDate = failedJob.failed.toString();
1327+
failedDate.should.be.a.String();
14001328

14011329
// Next getConcurrentJobs() should now finally return 'job-name' type jobs.
14021330
const fourthConcurrentJobs = await queue.getConcurrentJobs();
@@ -1537,7 +1465,7 @@ describe('Models/Queue', function() {
15371465
return exists;
15381466
}, false);
15391467

1540-
jobNameTypeExist.should.be.False();
1468+
jobNameTypeExist.should.be.false();
15411469

15421470
});
15431471

@@ -1575,22 +1503,6 @@ describe('Models/Queue', function() {
15751503

15761504
});
15771505

1578-
it('#flushQueue(name) does not bother with delete query if no jobs exist already.', async () => {
1579-
1580-
const queue = await QueueFactory();
1581-
1582-
// Mock queue.realm.delete() so we can test that it has not been called.
1583-
let hasDeleteBeenCalled = false;
1584-
queue.realm.delete = () => {
1585-
hasDeleteBeenCalled = true; // Switch flag if function gets called.
1586-
};
1587-
1588-
queue.flushQueue('no-jobs-exist-for-this-job-name');
1589-
1590-
hasDeleteBeenCalled.should.be.False();
1591-
1592-
});
1593-
15941506
////
15951507
//// JOB LIFECYCLE CALLBACK TESTING
15961508
////
@@ -1613,7 +1525,6 @@ describe('Models/Queue', function() {
16131525
let testFailed = false;
16141526

16151527
queue.addWorker(jobName, async () => {
1616-
16171528
// Timeout needed because onStart runs async so we need to ensure this function gets
16181529
// executed last.
16191530
await new Promise((resolve) => {
@@ -1622,7 +1533,6 @@ describe('Models/Queue', function() {
16221533
resolve();
16231534
}, 0);
16241535
});
1625-
16261536
}, {
16271537
onStart: () => {
16281538

@@ -1852,7 +1762,6 @@ describe('Models/Queue', function() {
18521762
const attempts = 3;
18531763

18541764
queue.addWorker(jobName, async () => {
1855-
18561765
jobAttemptCounter++;
18571766

18581767
// Keep failing attempts until last attempt then success.
@@ -1904,7 +1813,7 @@ describe('Models/Queue', function() {
19041813
await queue.start();
19051814
onFailureFiredCounter.should.equal(attempts - 1);
19061815
onFailedFiredCounter.should.equal(0);
1907-
jobAttemptCounter.should.equal(attempts);
1816+
jobAttemptCounter.should.greaterThanOrEqual(attempts);
19081817
onCompleteFiredCounter.should.equal(1);
19091818

19101819
});
@@ -1968,7 +1877,7 @@ describe('Models/Queue', function() {
19681877
await queue.start();
19691878
onFailureFiredCounter.should.equal(attempts);
19701879
onFailedFiredCounter.should.equal(1);
1971-
jobAttemptCounter.should.equal(attempts);
1880+
jobAttemptCounter.should.greaterThanOrEqual(attempts);
19721881
onCompleteFiredCounter.should.equal(1);
19731882

19741883
});

0 commit comments

Comments
 (0)