Skip to content
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

Job is being marked as Completed even though there are errors in the logs #2717

Open
vinayak2112 opened this issue Apr 2, 2024 · 1 comment

Comments

@vinayak2112
Copy link

vinayak2112 commented Apr 2, 2024

engtest

`jobs.process('englishTestSFUpdates', async (job, callback) => {
let {email, action} = job.data
//section = Started / Aborted / Regenerated / Completed
//console.log('englishTestSFUpdates Job Started: ', job.data, job.id)
job.log('Job Started', action)

try {
	job.log('Finding User In Mongo: ' + email)
	let user = await UserModel.getFullUserByEmail(email)
	if (!user) {
		job.progress(100)
		job.log('User Not Found In Mongo')
		return callback('User Not Found In Mongo')
	}

	job.log('User Record Found in Mongo: ' + user.email)
	job.progress(15)

	let data = {}

	data.testStatus = user.onlinetest.testStatus

	//if (data.testStatus === 'Completed') data.testStatus = 'Complete'
	//console.log('englishTestSFUpdates: Updating SF as per test Status: ' +data.testStatus)
	job.log('Updating SF as per test Status: ' + data.testStatus)
	const salesforceId = user.salesforceIds.SF_Id
	const frId = user.salesforceIds.FR_Id

	/**Mapping of onlinetes.testStatus to value of AMCAT_Status__c on SF**/
	const AMCAT_Status__c = {
		Started: 'In Progress',
		Paused: 'In Progress',
		Resumed: 'In Progress',
		Aborted: 'Aborted',
		Regenerated: 'Not Started',
		Completed: 'Completed',
	}
	/**Mapping of onlinetes.rescheduled to value of AMCAT_Test_Creation_Status__c SF**/
	const AMCAT_Test_Creation_Status__c = user.onlinetest.rescheduled
		? 'Regenerated'
		: 'Created'

	switch (data.testStatus) {
		//case 'Not Started':

		//Test is started - Set SF fields for start-test
		case 'Regenerated':
		case 'Started': {
			//console.log('englishTestSFUpdates: updating SF for Started/Regenerated')
			//if (['In Progress',"Not Started"].includes(user.formSections.onlinetest)) {
			//	console.log('englishTestSFUpdates: formsection os in progress/not started ')
			job.log('Updating Contact Record: ' + salesforceId)
			await SalesforceService.upsertContactRecord({
				Id: user.salesforceIds.SF_Id,
				AMCAT_Status__c: AMCAT_Status__c[data.testStatus],
				AMCAT_Test_Creation_Status__c: user.onlinetest.rescheduled
					? 'Regenerated'
					: 'Created',
			})
			job.log('Updated Contact Record: ' + salesforceId)
			job.log('Updating FR Record: ' + frId)
			SalesforceService.updateFirstReview(
				user,
				{
					Start_Time__c: user.onlinetest.startTime,
				},
				(err, firstReviewId) => {
					if (err) {
						job.log(
							'Error Updating First Review Record: ' +
								JSON.stringify(err)
						)
						job.progress(100)
					} else {
						job.log(
							'First Review Record Updated: ' + firstReviewId
						)
						job.progress(90)
					}
				}
			)
			job.log(
				'Updated SF to Start test as per testStatus  ' +
					data.testStatus
			)
			job.progress(100)
			callback()
			break
		}
		case 'Aborted':
		case 'Completed': {
			//console.log('englishTestSFUpdates: test Status case Aborted/Completed')
			const onlinetest = user.onlinetest
			const testResults = onlinetest.testResults
			const proctoringData = onlinetest.proctoringData
			const separator = ','
			let SF_DATA = {
				Grammar_Q_IDs__c: _.join(
					testResults.grammarQIds,
					separator
				),
				RC_Q_IDs__c:
					testResults.rcQIds.length > 0
						? _.join(testResults.rcQIds, separator)
						: '',

				Correct_Grammar_Qs__c:
					testResults.correctGrammarQueIds.length > 0
						? _.join(
								testResults.correctGrammarQueIds,
								separator
						  )
						: '',

				Correct_RC_Qs__c:
					testResults.correctRCQueIds.length > 0
						? _.join(testResults.correctRCQueIds, separator)
						: '',

				Grammar_Correct_Ans_Count__c:
					testResults.grammarCorrectAnsCount,
				RC_Correct_Ans_Count__c: testResults.rcCorrectAnsCount,
				Proctoring_Concerns__c: proctoringData.proctoringConcerns,
				Proctoring_Warning_Count__c: proctoringData.tabCount,
			}

			await SalesforceService.upsertContactRecord({
				Id: user.salesforceIds.SF_Id,
				AMCAT_Status__c: AMCAT_Status__c[data.testStatus],
				AMCAT_Test_Creation_Status__c:
					AMCAT_Test_Creation_Status__c,
			}).then(
				SalesforceService.updateFirstReview(
					user,
					{
						Start_Time__c: onlinetest.startTime,
						End_Time__c: onlinetest.endTime,
						...SF_DATA,
					},
					(err, firstReviewId) => {
						if (err) {
							job.log(
								'Error Updating First Review Record: ' +
									JSON.stringify(err)
							)
							job.progress(100)
						} else {
							job.log(
								'First Review Record Updated: ' +
									firstReviewId
							)
							job.progress(90)
						}
					}
				)
			)
			break
		}
	}
	job.progress(100)
	callback()
} catch (err) {
	job.log('Error Processing Job')
	job.progress(100)
	handleError('queue', err, email)
	return callback(err)
}

})`
here is the code snippet which is responsible for sending jobs in failed section / completed section

@vinayak2112 vinayak2112 changed the title Job is being marked as Completed even though there are error in the logs Job is being marked as Completed even though there are errors in the logs Apr 2, 2024
@manast
Copy link
Member

manast commented Apr 2, 2024

You catching the error yourself, you should let the error propagate up so that Bull can catch it and mark the job as failed.
Btw, a friendly hint, refactor your code in small functions, it is way too much code in a single function and thus easier to make mistakes and much harder to understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants