Browse Source

定时任务模块添加批量开始和批量关闭

zhifei 1 year ago
parent
commit
2bede02878

+ 23 - 1
ruoyi-quartz/src/main/java/com/ruoyi/quartz/controller/SysJobController.java

@@ -2,6 +2,8 @@ package com.ruoyi.quartz.controller;
 
 import java.util.List;
 import javax.servlet.http.HttpServletResponse;
+
+import com.ruoyi.common.constant.ScheduleConstants;
 import org.quartz.SchedulerException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -29,7 +31,7 @@ import com.ruoyi.quartz.util.ScheduleUtils;
 
 /**
  * 调度任务信息操作处理
- * 
+ *
  * @author ruoyi
  */
 @RestController
@@ -159,6 +161,26 @@ public class SysJobController extends BaseController
         return toAjax(jobService.changeStatus(newJob));
     }
 
+    /**
+     * 定时任务状态批量启动
+     */
+    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
+    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
+    @PutMapping("/start/{jobIds}")
+    public AjaxResult changeStatusToStart(@PathVariable Long[] jobIds) {
+        return toAjax(jobService.changeStatus(jobIds, ScheduleConstants.Status.NORMAL.getValue()));
+    }
+
+    /**
+     * 定时任务状态批量暂停
+     */
+    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
+    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
+    @PutMapping("/stop/{jobIds}")
+    public AjaxResult changeStatusToStop(@PathVariable Long[] jobIds) {
+        return toAjax(jobService.changeStatus(jobIds,ScheduleConstants.Status.PAUSE.getValue()));
+    }
+
     /**
      * 定时任务立即执行一次
      */

+ 17 - 8
ruoyi-quartz/src/main/java/com/ruoyi/quartz/mapper/SysJobMapper.java

@@ -2,17 +2,18 @@ package com.ruoyi.quartz.mapper;
 
 import java.util.List;
 import com.ruoyi.quartz.domain.SysJob;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * 调度任务信息 数据层
- * 
+ *
  * @author ruoyi
  */
 public interface SysJobMapper
 {
     /**
      * 查询调度任务日志集合
-     * 
+     *
      * @param job 调度信息
      * @return 操作日志集合
      */
@@ -20,14 +21,14 @@ public interface SysJobMapper
 
     /**
      * 查询所有调度任务
-     * 
+     *
      * @return 调度任务列表
      */
     public List<SysJob> selectJobAll();
 
     /**
      * 通过调度ID查询调度任务信息
-     * 
+     *
      * @param jobId 调度ID
      * @return 角色对象信息
      */
@@ -35,7 +36,7 @@ public interface SysJobMapper
 
     /**
      * 通过调度ID删除调度任务信息
-     * 
+     *
      * @param jobId 调度ID
      * @return 结果
      */
@@ -43,7 +44,7 @@ public interface SysJobMapper
 
     /**
      * 批量删除调度任务信息
-     * 
+     *
      * @param ids 需要删除的数据ID
      * @return 结果
      */
@@ -51,7 +52,7 @@ public interface SysJobMapper
 
     /**
      * 修改调度任务信息
-     * 
+     *
      * @param job 调度任务信息
      * @return 结果
      */
@@ -59,9 +60,17 @@ public interface SysJobMapper
 
     /**
      * 新增调度任务信息
-     * 
+     *
      * @param job 调度任务信息
      * @return 结果
      */
     public int insertJob(SysJob job);
+
+    /**
+     * 批量修改状态
+     * @return 结果
+     */
+    public int updateJobStatus(@Param("ids") Long[] ids,@Param("status") String status);
+
+    List<SysJob> selectJobListByIds(Long[] jobIds);
 }

+ 15 - 12
ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/ISysJobService.java

@@ -7,14 +7,14 @@ import com.ruoyi.quartz.domain.SysJob;
 
 /**
  * 定时任务调度信息信息 服务层
- * 
+ *
  * @author ruoyi
  */
 public interface ISysJobService
 {
     /**
      * 获取quartz调度器的计划任务
-     * 
+     *
      * @param job 调度信息
      * @return 调度任务集合
      */
@@ -22,7 +22,7 @@ public interface ISysJobService
 
     /**
      * 通过调度任务ID查询调度信息
-     * 
+     *
      * @param jobId 调度任务ID
      * @return 调度任务对象信息
      */
@@ -30,7 +30,7 @@ public interface ISysJobService
 
     /**
      * 暂停任务
-     * 
+     *
      * @param job 调度信息
      * @return 结果
      */
@@ -38,7 +38,7 @@ public interface ISysJobService
 
     /**
      * 恢复任务
-     * 
+     *
      * @param job 调度信息
      * @return 结果
      */
@@ -46,7 +46,7 @@ public interface ISysJobService
 
     /**
      * 删除任务后,所对应的trigger也将被删除
-     * 
+     *
      * @param job 调度信息
      * @return 结果
      */
@@ -54,7 +54,7 @@ public interface ISysJobService
 
     /**
      * 批量删除调度信息
-     * 
+     *
      * @param jobIds 需要删除的任务ID
      * @return 结果
      */
@@ -62,7 +62,7 @@ public interface ISysJobService
 
     /**
      * 任务调度状态修改
-     * 
+     *
      * @param job 调度信息
      * @return 结果
      */
@@ -70,7 +70,7 @@ public interface ISysJobService
 
     /**
      * 立即运行任务
-     * 
+     *
      * @param job 调度信息
      * @return 结果
      */
@@ -78,7 +78,7 @@ public interface ISysJobService
 
     /**
      * 新增任务
-     * 
+     *
      * @param job 调度信息
      * @return 结果
      */
@@ -86,7 +86,7 @@ public interface ISysJobService
 
     /**
      * 更新任务
-     * 
+     *
      * @param job 调度信息
      * @return 结果
      */
@@ -94,9 +94,12 @@ public interface ISysJobService
 
     /**
      * 校验cron表达式是否有效
-     * 
+     *
      * @param cronExpression 表达式
      * @return 结果
      */
     public boolean checkCronExpressionIsValid(String cronExpression);
+
+    int changeStatus(Long[] jobIds,String status);
+
 }

+ 38 - 13
ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/SysJobServiceImpl.java

@@ -19,7 +19,7 @@ import com.ruoyi.quartz.util.ScheduleUtils;
 
 /**
  * 定时任务调度信息 服务层
- * 
+ *
  * @author ruoyi
  */
 @Service
@@ -47,7 +47,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 获取quartz调度器的计划任务列表
-     * 
+     *
      * @param job 调度信息
      * @return
      */
@@ -59,7 +59,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 通过调度任务ID查询调度信息
-     * 
+     *
      * @param jobId 调度任务ID
      * @return 调度任务对象信息
      */
@@ -71,7 +71,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 暂停任务
-     * 
+     *
      * @param job 调度信息
      */
     @Override
@@ -91,7 +91,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 恢复任务
-     * 
+     *
      * @param job 调度信息
      */
     @Override
@@ -111,7 +111,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 删除任务后,所对应的trigger也将被删除
-     * 
+     *
      * @param job 调度信息
      */
     @Override
@@ -130,7 +130,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 批量删除调度信息
-     * 
+     *
      * @param jobIds 需要删除的任务ID
      * @return 结果
      */
@@ -147,7 +147,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 任务调度状态修改
-     * 
+     *
      * @param job 调度信息
      */
     @Override
@@ -169,7 +169,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 立即运行任务
-     * 
+     *
      * @param job 调度信息
      */
     @Override
@@ -187,7 +187,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 新增任务
-     * 
+     *
      * @param job 调度信息 调度信息
      */
     @Override
@@ -205,7 +205,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 更新任务的时间表达式
-     * 
+     *
      * @param job 调度信息
      */
     @Override
@@ -223,7 +223,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 更新任务
-     * 
+     *
      * @param job 任务对象
      * @param jobGroup 任务组名
      */
@@ -242,7 +242,7 @@ public class SysJobServiceImpl implements ISysJobService
 
     /**
      * 校验cron表达式是否有效
-     * 
+     *
      * @param cronExpression 表达式
      * @return 结果
      */
@@ -251,4 +251,29 @@ public class SysJobServiceImpl implements ISysJobService
     {
         return CronUtils.isValid(cronExpression);
     }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public int changeStatus(Long[] jobIds,String status) {
+        List<SysJob> sysJobs = jobMapper.selectJobListByIds(jobIds);
+        if (status.equals(ScheduleConstants.Status.PAUSE.getValue())){
+            for (SysJob sysJob : sysJobs) {
+                try {
+                    pauseJob(sysJob);
+                } catch (SchedulerException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+
+        }else {
+            for (SysJob sysJob : sysJobs) {
+                try {
+                    resumeJob(sysJob);
+                } catch (SchedulerException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        }
+        return jobMapper.updateJobStatus(jobIds,status);
+    }
 }

+ 31 - 12
ruoyi-quartz/src/main/resources/mapper/quartz/SysJobMapper.xml

@@ -19,12 +19,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		<result property="updateTime"     column="update_time"     />
 		<result property="remark"         column="remark"          />
 	</resultMap>
-	
+
 	<sql id="selectJobVo">
-        select job_id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status, create_by, create_time, remark 
+        select job_id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status, create_by, create_time, remark
 		from sys_job
     </sql>
-	
+
 	<select id="selectJobList" parameterType="SysJob" resultMap="SysJobResult">
 		<include refid="selectJobVo"/>
 		<where>
@@ -42,27 +42,34 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 			</if>
 		</where>
 	</select>
-	
+
 	<select id="selectJobAll" resultMap="SysJobResult">
 		<include refid="selectJobVo"/>
 	</select>
-	
+
 	<select id="selectJobById" parameterType="Long" resultMap="SysJobResult">
 		<include refid="selectJobVo"/>
 		where job_id = #{jobId}
 	</select>
-	
+	<select id="selectJobListByIds" resultMap="SysJobResult">
+		<include refid="selectJobVo"/>
+		where job_id in
+		<foreach collection="array" item="id" open="(" separator="," close=")">
+			#{id}
+		</foreach>
+	</select>
+
 	<delete id="deleteJobById" parameterType="Long">
  		delete from sys_job where job_id = #{jobId}
  	</delete>
- 	
- 	<delete id="deleteJobByIds" parameterType="Long">
+
+ 	<delete id="deleteJobByIds" parameterType="Long" >
  		delete from sys_job where job_id in
  		<foreach collection="array" item="jobId" open="(" separator="," close=")">
  			#{jobId}
-        </foreach> 
+        </foreach>
  	</delete>
- 	
+
  	<update id="updateJob" parameterType="SysJob">
  		update sys_job
  		<set>
@@ -79,7 +86,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  		</set>
  		where job_id = #{jobId}
 	</update>
- 	
+
  	<insert id="insertJob" parameterType="SysJob" useGeneratedKeys="true" keyProperty="jobId">
  		insert into sys_job(
  			<if test="jobId != null and jobId != 0">job_id,</if>
@@ -108,4 +115,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  		)
 	</insert>
 
-</mapper> 
+	<update id="updateJobStatus" parameterType="Long">
+		update sys_job
+		<set>
+			<if test="status !=null">status = #{status},</if>
+			update_time = sysdate()
+		</set>
+		where job_id in
+		<foreach collection="ids" item="id" open="(" separator="," close=")">
+			#{id}
+		</foreach>
+	</update>
+
+</mapper>

+ 16 - 1
ruoyi-ui/src/api/monitor/job.js

@@ -43,6 +43,21 @@ export function delJob(jobId) {
   })
 }
 
+// 删除定时任务调度
+export function startByList(jobId) {
+  return request({
+    url: '/monitor/job/start/' + jobId,
+    method: 'put'
+  })
+}
+// 删除定时任务调度
+export function stopByList(jobId) {
+  return request({
+    url: '/monitor/job/stop/' + jobId,
+    method: 'put'
+  })
+}
+
 // 任务状态修改
 export function changeJobStatus(jobId, status) {
   const data = {
@@ -68,4 +83,4 @@ export function runJob(jobId, jobGroup) {
     method: 'put',
     data: data
   })
-}
+}

+ 53 - 1
ruoyi-ui/src/views/monitor/job/index.vue

@@ -69,6 +69,28 @@
           v-hasPermi="['monitor:job:remove']"
         >删除</el-button>
       </el-col>
+      <el-col :span="1.5">
+        <el-button
+          type="danger"
+          plain
+          icon="el-icon-delete"
+          size="mini"
+          :disabled="multiple"
+          @click="handleStart"
+          v-hasPermi="['monitor:job:remove']"
+        >启动</el-button>
+      </el-col>
+      <el-col :span="1.5">
+        <el-button
+          type="danger"
+          plain
+          icon="el-icon-delete"
+          size="mini"
+          :disabled="multiple"
+          @click="handleStop"
+          v-hasPermi="['monitor:job:remove']"
+        >停止</el-button>
+      </el-col>
       <el-col :span="1.5">
         <el-button
           type="warning"
@@ -294,7 +316,17 @@
 </template>
 
 <script>
-import { listJob, getJob, delJob, addJob, updateJob, runJob, changeJobStatus } from "@/api/monitor/job";
+import {
+  listJob,
+  getJob,
+  delJob,
+  addJob,
+  updateJob,
+  runJob,
+  changeJobStatus,
+  startByList,
+  stopByList
+} from "@/api/monitor/job";
 import Crontab from '@/components/Crontab'
 
 export default {
@@ -510,6 +542,26 @@ export default {
       this.download('monitor/job/export', {
         ...this.queryParams
       }, `job_${new Date().getTime()}.xlsx`)
+    },
+    handleStart(row){
+      const jobIds = row.jobId || this.ids;
+      this.$modal.confirm('是否确认启动定时任务编号为"' + jobIds + '"的数据项?').then(function() {
+        return startByList(jobIds);
+      }).then(() => {
+        this.getList();
+        this.$modal.msgSuccess("启动成功");
+      }).catch(() => {});
+
+    },
+    handleStop(row){
+      const jobIds = row.jobId || this.ids;
+      this.$modal.confirm('是否确认停止定时任务编号为"' + jobIds + '"的数据项?').then(function() {
+        return stopByList(jobIds);
+      }).then(() => {
+        this.getList();
+        this.$modal.msgSuccess("停止成功");
+      }).catch(() => {});
+
     }
   }
 };