Skip to content

fix(jobs): Add Error Message to JobStatus Failure #6954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,16 @@ describe('Cloud Code', () => {
);
});

it('should set the failure message on the job error', async () => {
Parse.Cloud.job('myJobError', () => {
throw new Parse.Error(101, 'Something went wrong');
});
const job = await Parse.Cloud.startJob('myJobError');
const jobStatus = await Parse.Cloud.getJobStatus(job);
expect(jobStatus.get('message')).toEqual('Something went wrong');
expect(jobStatus.get('status')).toEqual('failed');
});

function getJobStatus(jobId) {
const q = new Parse.Query('_JobStatus');
return q.get(jobId, { useMasterKey: true });
Expand Down
25 changes: 14 additions & 11 deletions src/StatusHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Auth from './Auth';
const PUSH_STATUS_COLLECTION = '_PushStatus';
const JOB_STATUS_COLLECTION = '_JobStatus';

const incrementOp = function(object = {}, key, amount = 1) {
const incrementOp = function (object = {}, key, amount = 1) {
if (!object[key]) {
object[key] = { __op: 'Increment', amount: amount };
} else {
Expand Down Expand Up @@ -91,7 +91,7 @@ export function jobStatusHandler(config) {
const objectId = newObjectId(config.objectIdSize);
const database = config.database;
const handler = statusHandler(JOB_STATUS_COLLECTION, database);
const setRunning = function(jobName, params) {
const setRunning = function (jobName, params) {
const now = new Date();
jobStatus = {
objectId,
Expand All @@ -107,27 +107,30 @@ export function jobStatusHandler(config) {
return handler.create(jobStatus);
};

const setMessage = function(message) {
const setMessage = function (message) {
if (!message || typeof message !== 'string') {
return Promise.resolve();
}
return handler.update({ objectId }, { message });
};

const setSucceeded = function(message) {
const setSucceeded = function (message) {
return setFinalStatus('succeeded', message);
};

const setFailed = function(message) {
const setFailed = function (message) {
return setFinalStatus('failed', message);
};

const setFinalStatus = function(status, message = undefined) {
const setFinalStatus = function (status, message = undefined) {
const finishedAt = new Date();
const update = { status, finishedAt };
if (message && typeof message === 'string') {
update.message = message;
}
if (message instanceof Error && typeof message.message === 'string') {
update.message = message.message;
}
return handler.update({ objectId }, update);
};

Expand All @@ -144,7 +147,7 @@ export function pushStatusHandler(config, existingObjectId) {
const database = config.database;
const handler = restStatusHandler(PUSH_STATUS_COLLECTION, config);
let objectId = existingObjectId;
const setInitial = function(body = {}, where, options = { source: 'rest' }) {
const setInitial = function (body = {}, where, options = { source: 'rest' }) {
const now = new Date();
let pushTime = now.toISOString();
let status = 'pending';
Expand Down Expand Up @@ -193,7 +196,7 @@ export function pushStatusHandler(config, existingObjectId) {
});
};

const setRunning = function(batches) {
const setRunning = function (batches) {
logger.verbose(
`_PushStatus ${objectId}: sending push to installations with %d batches`,
batches
Expand All @@ -210,7 +213,7 @@ export function pushStatusHandler(config, existingObjectId) {
);
};

const trackSent = function(
const trackSent = function (
results,
UTCOffset,
cleanupInstallations = process.env
Expand Down Expand Up @@ -310,7 +313,7 @@ export function pushStatusHandler(config, existingObjectId) {
});
};

const complete = function() {
const complete = function () {
return handler.update(
{ objectId },
{
Expand All @@ -320,7 +323,7 @@ export function pushStatusHandler(config, existingObjectId) {
);
};

const fail = function(err) {
const fail = function (err) {
if (typeof err === 'string') {
err = { message: err };
}
Expand Down