是否可以使用 CronTrigger 作业 ID 来确定作业是否已中止或无效?

问题描述

我有一个函数作为重新安排逻辑的一部分在实现 System.Schedulable 的 Apex 类中被调用

@TestVisible private void reschedule(crontrigger me,ISchedulable item,MySchedulableContext myCtx) {
        try {
            System.abortJob(me.Id);
        } catch(Exception e) {
            System.debug('Unable to abort the job');
        }
        String newTrigger = item.getNextTriggerString(myCtx);
        if (newTrigger != null && newTrigger.length() > 0) {
            try {
                System.schedule(me.CronJobDetail.Name+ '-' + System.currentTimeMillis(),newTrigger,this);
            } catch(Exception e) {
                System.debug('Unable to start the job');
            }

        }
    }

我如何知道我即将中止的工作是否尚未中止,或者我即将开始的工作是否尚未开始?是的,try/catch 会处理错误,但我想更好地理解这一点

解决方法

您可以查询 AsyncApexJob 对象以获取作业的状态。

你可以这样做:

SELECT Id,Status,JobItemsProcessed,TotalJobItems,NumberOfErrors
FROM AsyncApexJob WHERE ID = :jobId

如果需要,请检查状态。可用状态如下:

  • 持有
  • 排队
  • 准备
  • 处理
  • 中止
  • 已完成
  • 失败

因此,如果 Status 为 Aborted,则作业已经中止。