Search in sources :

Example 1 with ScheduleJob

use of top.longmarch.job.entity.ScheduleJob in project longmarch by yuyueqty.

the class ScheduleJobController method reset.

@OperationLog
@ApiOperation(value = "重置任务")
@RequiresPermissions("job:schedule:reset")
@PostMapping("/reset")
public Result<Object> reset(@RequestBody Long[] jobIds) {
    ScheduleJob scheduleJob = new ScheduleJob();
    scheduleJob.setCount(0);
    scheduleJobService.update(scheduleJob, new LambdaUpdateWrapper<ScheduleJob>().in(ScheduleJob::getId, ListUtil.of(jobIds)));
    return Result.ok();
}
Also used : LambdaUpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper) ScheduleJob(top.longmarch.job.entity.ScheduleJob) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) ApiOperation(io.swagger.annotations.ApiOperation) OperationLog(top.longmarch.lmcore.annotation.OperationLog)

Example 2 with ScheduleJob

use of top.longmarch.job.entity.ScheduleJob in project longmarch by yuyueqty.

the class ScheduleJobUtil method executeInternal.

@Override
protected void executeInternal(JobExecutionContext context) {
    Object object = context.getMergedJobDataMap().get(ScheduleJob.JOB_PARAM_KEY);
    ScheduleJob scheduleJob = JSONUtil.toBean(JSONUtil.toJsonStr(object), ScheduleJob.class);
    ScheduleJob scheduleJobDb = scheduleJobService.getById(scheduleJob);
    if (scheduleJobDb.getCount() >= MAX_ERR_NUM) {
        Long[] jobIds = { scheduleJob.getId() };
        scheduleJobService.pause(jobIds);
        logger.info("任务失败尝试次数已达上限({}),任务ID:{}", 3, scheduleJob.getId());
        return;
    }
    // 数据库保存执行记录
    ScheduleJobLog log = new ScheduleJobLog();
    log.setJobId(scheduleJob.getId());
    log.setBeanName(scheduleJob.getBeanName());
    log.setMethodName(scheduleJob.getMethodName());
    log.setParams(scheduleJob.getParams());
    log.setStartTime(new Date());
    // 任务开始时间
    long startTime = System.currentTimeMillis();
    try {
        // 执行任务
        logger.info("任务开始执行,任务ID:" + scheduleJob.getId());
        ScheduleRunnable task = new ScheduleRunnable(scheduleJob.getBeanName(), scheduleJob.getMethodName(), scheduleJob.getParams());
        Future<?> future = executorService.submit(task);
        Object o = future.get();
        logger.info("执行结果:" + JSONUtil.toJsonStr(o));
        // 任务执行总时长
        long times = System.currentTimeMillis() - startTime;
        log.setExecuteTime(times);
        // 任务状态 1:成功 2:失败
        log.setStatus(JobStatus.OK_RUN.getCode());
        logger.info("任务执行完毕,任务ID:" + scheduleJob.getId() + "  耗时:" + times + "毫秒");
    } catch (Exception e) {
        logger.error("任务执行失败,任务ID:" + scheduleJob.getId(), e);
        // 任务执行总时长
        long times = System.currentTimeMillis() - startTime;
        log.setExecuteTime(times);
        // 任务状态 0:成功 1:失败
        log.setStatus(JobStatus.ERR_RUN.getCode());
        log.setError(StrUtil.sub(e.toString(), 0, 2000));
    } finally {
        log.setEndTime(new Date());
        scheduleJobLogService.save(log);
    }
}
Also used : ScheduleJob(top.longmarch.job.entity.ScheduleJob) ScheduleJobLog(top.longmarch.job.entity.ScheduleJobLog) Date(java.util.Date)

Aggregations

ScheduleJob (top.longmarch.job.entity.ScheduleJob)2 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)1 ApiOperation (io.swagger.annotations.ApiOperation)1 Date (java.util.Date)1 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)1 ScheduleJobLog (top.longmarch.job.entity.ScheduleJobLog)1 OperationLog (top.longmarch.lmcore.annotation.OperationLog)1