All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rexsheng.springboot.faster.system.job.adapter.JobController Maven / Gradle / Ivy

The newest version!
package com.github.rexsheng.springboot.faster.system.job.adapter;

import com.github.rexsheng.springboot.faster.common.constant.CommonConstant;
import com.github.rexsheng.springboot.faster.common.domain.Result;
import com.github.rexsheng.springboot.faster.logging.RequestLog;
import com.github.rexsheng.springboot.faster.request.repeat.annotation.RequestRepeat;
import com.github.rexsheng.springboot.faster.system.job.application.JobService;
import com.github.rexsheng.springboot.faster.system.job.application.TriggerService;
import com.github.rexsheng.springboot.faster.system.job.application.dto.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.quartz.core.QuartzScheduler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/sys/job")
@Tag(name = "JobController",description = "定时任务管理")
@ConditionalOnProperty(prefix = "app.module.job",name = "controller",havingValue = "true",matchIfMissing = true)
@ConditionalOnClass(QuartzScheduler.class)
public class JobController {

    @Resource
    private JobService jobService;

    @Resource
    private TriggerService triggerService;

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_LIST)")
    @GetMapping
    public Result queryJobPagedList(@RequestParam(required = false) String name,
                                     @RequestParam Long pageIndex,
                                     @RequestParam Long pageSize){
        QueryJobRequest request=new QueryJobRequest();
        request.setName(name);
        request.setPageIndex(pageIndex);
        request.setPageSize(pageSize);
        return Result.success(jobService.paginateJob(request));
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_ADD)")
    @PostMapping
    @RequestLog("新增任务")
    public Result addJob(@RequestBody @Validated AddJobRequest request){
        jobService.addJob(request);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_EDIT)")
    @GetMapping("/{id}")
    public Result getJob(@PathVariable Long id){
        return Result.success(jobService.getJob(id));
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_EDIT)")
    @PutMapping("/{id}")
    @RequestLog("修改任务")
    public Result updatePost(@PathVariable Long id, @RequestBody @Validated UpdateJobRequest request){
        request.setJobId(id);
        jobService.update(request);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_EDIT)")
    @PutMapping("/{ids}/status/{status}")
    @RequestLog("修改任务状态")
    public Result changeJobValid(@PathVariable Long[] ids,@PathVariable Boolean status){
        jobService.updateStatus(UpdateJobRequest.of(ids,status? CommonConstant.STATUS_RUNNING:CommonConstant.STATUS_STOP));
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_DEL)")
    @DeleteMapping
    @RequestLog("删除任务")
    public Result deleteJob(@RequestBody Long[] ids){
        jobService.delete(Arrays.asList(ids));
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @PostMapping("/{id}/exec")
    @RequestLog("执行一次任务")
    public Result triggerJob(@PathVariable Long id){
        jobService.executeOnce(id);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_LIST)")
    @GetMapping("/{jobId}/trigger")
    public Result queryJobTriggerList(@PathVariable Long jobId){
        QueryTriggerRequest request=new QueryTriggerRequest();
        request.setJobId(jobId);
        return Result.success(triggerService.queryTriggers(request));
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @PostMapping("/{jobId}/trigger/restart")
    @RequestLog("重启任务下调度器")
    public Result restartJobTriggers(@PathVariable Long jobId){
        triggerService.restartTriggersByJobId(jobId);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @PostMapping("/trigger")
    @RequestLog("新增调度器")
    public Result addTrigger(@RequestBody @Validated AddTriggerRequest request){
        triggerService.addTrigger(request);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @GetMapping("/trigger/{id}")
    public Result getTrigger(@PathVariable Long id){
        return Result.success(triggerService.getTrigger(id));
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @PutMapping("/trigger/{id}")
    @RequestLog("修改调度器")
    public Result updateTrigger(@PathVariable Long id, @RequestBody @Validated UpdateTriggerRequest request){
        request.setTriggerId(id);
        triggerService.updateTrigger(request);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @DeleteMapping("/trigger")
    @RequestLog("删除调度器")
    public Result deleteTrigger(@RequestBody Long[] ids){
        triggerService.deleteTrigger(DeleteTriggerRequest.ofTriggers(Arrays.asList(ids),true));
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @PostMapping("/trigger/{id}/start")
    @RequestLog("启动调度器")
    public Result startJob(@PathVariable Long id){
        triggerService.start(id);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @PostMapping("/trigger/{id}/stop")
    @RequestLog("停止调度")
    public Result stopJob(@PathVariable Long id){
        triggerService.stop(id);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @PostMapping("/trigger/{id}/pause")
    @RequestLog("暂停调度器")
    public Result pauseJob(@PathVariable Long id){
        triggerService.pause(id);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @PostMapping("/trigger/{id}/resume")
    @RequestLog("继续调度器")
    public Result resumeJob(@PathVariable Long id){
        triggerService.resume(id);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @PostMapping("/trigger/{id}/restart")
    @RequestLog("重启调度器")
    public Result restartTrigger(@PathVariable Long id){
        triggerService.restart(id);
        return Result.success();
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_TRIGGER)")
    @PutMapping("/trigger/cron/valid")
    @RequestRepeat(enabled = false)
    public Result validateCronExpression(@RequestParam(name = "cron") String cronExpression){
        return Result.success(triggerService.validateCronExpression(cronExpression));
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_LIST)")
    @GetMapping("/{jobId}/log")
    public Result queryJobLogPagedList( @PathVariable Long jobId,
                                        @RequestParam(required = false) Integer state,
                                        @RequestParam(required = false) Long triggerId,
                                        @RequestParam(required = false) String startTime,
                                        @RequestParam(required = false) String endTime,
                                        @RequestParam Long pageIndex,
                                        @RequestParam Long pageSize){
        QueryJobLogRequest request=new QueryJobLogRequest();
        request.setJobId(jobId);
        request.setTriggerId(triggerId);
        request.setState(state);
        request.setStartTime(startTime);
        request.setEndTime(endTime);
        request.setPageIndex(pageIndex);
        request.setPageSize(pageSize);
        return Result.success(jobService.paginateJobLog(request));
    }

    @PreAuthorize("hasAuthority(T(com.github.rexsheng.springboot.faster.common.constant.AuthCodeConstant).JOB_LIST)")
    @GetMapping("/log/{jobLogId}/detail")
    public Result queryJobLogDetailPagedList(@PathVariable Long jobLogId,
                                             @RequestParam Long pageIndex,
                                             @RequestParam Long pageSize){
        QueryJobLogDetailRequest request=new QueryJobLogDetailRequest();
        request.setJobLogId(jobLogId);
        request.setPageIndex(pageIndex);
        request.setPageSize(pageSize);
        return Result.success(jobService.paginateJobLogDetail(request));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy