private void handleJobExecuteFailed(JobDetail jobDetail, JobInstance instance, String appName, String jobClass, String cause) { try { if (instance == null){ return; } if (cause.length() > Constants.MAX_ERROR_LENGTH){ cause = cause.substring(0, Constants.MAX_ERROR_LENGTH); } // save the job instance jobService.failedJobInstance(instance.getId(), cause); // delete the instance jobSupport.deleteJobInstance(appName, jobClass, instance); // update the job state jobSupport.updateJobStateDirectly(appName, jobClass, JobState.WAITING); } catch (Exception e){ Logs.error("failed to handle the job(instance={}, appName={}, jobClass={}) execute failed, cause: {}", instance, appName, jobClass, Throwables.getStackTraceAsString(e)); } finally { // publish job failed event Long instanceId = instance == null ? null : instance.getId(); eventDispatcher.publish(new JobFailedEvent(jobDetail.getJob().getId(), instanceId, cause)); } }