Skip to content

Commit 603cc1f

Browse files
authored
Fix error when a not yet inserted job is updated (#7196)
* Fix error when a not yet inserted job is updated * Add entry to changelog * revert the upsert change and fix the test * Revert the change so job execute a single time * Fix other tests with potential similar problem
1 parent 9c100cf commit 603cc1f

File tree

2 files changed

+80
-72
lines changed

2 files changed

+80
-72
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ___
2727
- IMPROVE: Parse Server is from now on continuously tested against all recent Node.js versions that have not reached their end-of-life support date. [7161](https://github.com/parse-community/parse-server/pull/7177). Thanks to [Manuel Trezza](https://github.com/mtrezza).
2828
- IMPROVE: Optimize queries on classes with pointer permissions. [#7061](https://github.com/parse-community/parse-server/pull/7061). Thanks to [Pedro Diaz](https://github.com/pdiaz)
2929
- IMPROVE: Parse Server will from now on be continuously tested against all relevant Postgres versions (minor versions). Added Postgres compatibility table to Parse Server docs. [#7176](https://github.com/parse-community/parse-server/pull/7176). Thanks to [Corey Baker](https://github.com/cbaker6).
30+
- FIX: Fix error when a not yet inserted job is updated [#7196](https://github.com/parse-community/parse-server/pull/7196). Thanks to [Antonio Davi Macedo Coelho de Castro](https://github.com/davimacedo).
3031
- FIX: request.context for afterFind triggers. [#7078](https://github.com/parse-community/parse-server/pull/7078). Thanks to [dblythy](https://github.com/dblythy)
3132
- FIX: Winston Logger interpolating stdout to console [#7114](https://github.com/parse-community/parse-server/pull/7114). Thanks to [dplewis](https://github.com/dplewis)
3233

spec/CloudCode.spec.js

+79-72
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,9 @@ describe('Cloud Code', () => {
15491549
describe('cloud jobs', () => {
15501550
it('should define a job', done => {
15511551
expect(() => {
1552-
Parse.Cloud.job('myJob', () => {});
1552+
Parse.Cloud.job('myJob', ({ message }) => {
1553+
message('Hello, world!!!');
1554+
});
15531555
}).not.toThrow();
15541556

15551557
request({
@@ -1559,15 +1561,19 @@ describe('Cloud Code', () => {
15591561
'X-Parse-Application-Id': Parse.applicationId,
15601562
'X-Parse-Master-Key': Parse.masterKey,
15611563
},
1562-
}).then(
1563-
() => {
1564-
done();
1565-
},
1566-
err => {
1567-
fail(err);
1568-
done();
1569-
}
1570-
);
1564+
})
1565+
.then(async response => {
1566+
const jobStatusId = response.headers['x-parse-job-status-id'];
1567+
const checkJobStatus = async () => {
1568+
const jobStatus = await getJobStatus(jobStatusId);
1569+
return jobStatus.get('finishedAt') && jobStatus.get('message') === 'Hello, world!!!';
1570+
};
1571+
while (!(await checkJobStatus())) {
1572+
await new Promise(resolve => setTimeout(resolve, 100));
1573+
}
1574+
})
1575+
.then(done)
1576+
.catch(done.fail);
15711577
});
15721578

15731579
it('should not run without master key', done => {
@@ -1602,7 +1608,6 @@ describe('Cloud Code', () => {
16021608
expect(typeof req.jobId).toBe('string');
16031609
expect(typeof req.message).toBe('function');
16041610
expect(typeof res).toBe('undefined');
1605-
done();
16061611
});
16071612
}).not.toThrow();
16081613

@@ -1613,13 +1618,19 @@ describe('Cloud Code', () => {
16131618
'X-Parse-Application-Id': Parse.applicationId,
16141619
'X-Parse-Master-Key': Parse.masterKey,
16151620
},
1616-
}).then(
1617-
() => {},
1618-
err => {
1619-
fail(err);
1620-
done();
1621-
}
1622-
);
1621+
})
1622+
.then(async response => {
1623+
const jobStatusId = response.headers['x-parse-job-status-id'];
1624+
const checkJobStatus = async () => {
1625+
const jobStatus = await getJobStatus(jobStatusId);
1626+
return jobStatus.get('finishedAt');
1627+
};
1628+
while (!(await checkJobStatus())) {
1629+
await new Promise(resolve => setTimeout(resolve, 100));
1630+
}
1631+
})
1632+
.then(done)
1633+
.catch(done.fail);
16231634
});
16241635

16251636
it('should run with master key basic auth', done => {
@@ -1630,25 +1641,30 @@ describe('Cloud Code', () => {
16301641
expect(typeof req.jobId).toBe('string');
16311642
expect(typeof req.message).toBe('function');
16321643
expect(typeof res).toBe('undefined');
1633-
done();
16341644
});
16351645
}).not.toThrow();
16361646

16371647
request({
16381648
method: 'POST',
16391649
url: `http://${Parse.applicationId}:${Parse.masterKey}@localhost:8378/1/jobs/myJob`,
1640-
}).then(
1641-
() => {},
1642-
err => {
1643-
fail(err);
1644-
done();
1645-
}
1646-
);
1650+
})
1651+
.then(async response => {
1652+
const jobStatusId = response.headers['x-parse-job-status-id'];
1653+
const checkJobStatus = async () => {
1654+
const jobStatus = await getJobStatus(jobStatusId);
1655+
return jobStatus.get('finishedAt');
1656+
};
1657+
while (!(await checkJobStatus())) {
1658+
await new Promise(resolve => setTimeout(resolve, 100));
1659+
}
1660+
})
1661+
.then(done)
1662+
.catch(done.fail);
16471663
});
16481664

16491665
it('should set the message / success on the job', done => {
16501666
Parse.Cloud.job('myJob', req => {
1651-
const promise = req
1667+
return req
16521668
.message('hello')
16531669
.then(() => {
16541670
return getJobStatus(req.jobId);
@@ -1657,21 +1673,6 @@ describe('Cloud Code', () => {
16571673
expect(jobStatus.get('message')).toEqual('hello');
16581674
expect(jobStatus.get('status')).toEqual('running');
16591675
});
1660-
promise
1661-
.then(() => {
1662-
return getJobStatus(req.jobId);
1663-
})
1664-
.then(jobStatus => {
1665-
expect(jobStatus.get('message')).toEqual('hello');
1666-
expect(jobStatus.get('status')).toEqual('succeeded');
1667-
done();
1668-
})
1669-
.catch(err => {
1670-
console.error(err);
1671-
jfail(err);
1672-
done();
1673-
});
1674-
return promise;
16751676
});
16761677

16771678
request({
@@ -1681,32 +1682,28 @@ describe('Cloud Code', () => {
16811682
'X-Parse-Application-Id': Parse.applicationId,
16821683
'X-Parse-Master-Key': Parse.masterKey,
16831684
},
1684-
}).then(
1685-
() => {},
1686-
err => {
1687-
fail(err);
1688-
done();
1689-
}
1690-
);
1685+
})
1686+
.then(async response => {
1687+
const jobStatusId = response.headers['x-parse-job-status-id'];
1688+
const checkJobStatus = async () => {
1689+
const jobStatus = await getJobStatus(jobStatusId);
1690+
return (
1691+
jobStatus.get('finishedAt') &&
1692+
jobStatus.get('message') === 'hello' &&
1693+
jobStatus.get('status') === 'succeeded'
1694+
);
1695+
};
1696+
while (!(await checkJobStatus())) {
1697+
await new Promise(resolve => setTimeout(resolve, 100));
1698+
}
1699+
})
1700+
.then(done)
1701+
.catch(done.fail);
16911702
});
16921703

16931704
it('should set the failure on the job', done => {
1694-
Parse.Cloud.job('myJob', req => {
1695-
const promise = Promise.reject('Something went wrong');
1696-
new Promise(resolve => setTimeout(resolve, 200))
1697-
.then(() => {
1698-
return getJobStatus(req.jobId);
1699-
})
1700-
.then(jobStatus => {
1701-
expect(jobStatus.get('message')).toEqual('Something went wrong');
1702-
expect(jobStatus.get('status')).toEqual('failed');
1703-
done();
1704-
})
1705-
.catch(err => {
1706-
jfail(err);
1707-
done();
1708-
});
1709-
return promise;
1705+
Parse.Cloud.job('myJob', () => {
1706+
return Promise.reject('Something went wrong');
17101707
});
17111708

17121709
request({
@@ -1716,13 +1713,23 @@ describe('Cloud Code', () => {
17161713
'X-Parse-Application-Id': Parse.applicationId,
17171714
'X-Parse-Master-Key': Parse.masterKey,
17181715
},
1719-
}).then(
1720-
() => {},
1721-
err => {
1722-
fail(err);
1723-
done();
1724-
}
1725-
);
1716+
})
1717+
.then(async response => {
1718+
const jobStatusId = response.headers['x-parse-job-status-id'];
1719+
const checkJobStatus = async () => {
1720+
const jobStatus = await getJobStatus(jobStatusId);
1721+
return (
1722+
jobStatus.get('finishedAt') &&
1723+
jobStatus.get('message') === 'Something went wrong' &&
1724+
jobStatus.get('status') === 'failed'
1725+
);
1726+
};
1727+
while (!(await checkJobStatus())) {
1728+
await new Promise(resolve => setTimeout(resolve, 100));
1729+
}
1730+
})
1731+
.then(done)
1732+
.catch(done.fail);
17261733
});
17271734

17281735
it('should set the failure message on the job error', async () => {

0 commit comments

Comments
 (0)