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

cn.ponfee.disjob.supervisor.dao.xml.SchedTaskMapper.xml Maven / Gradle / Ivy

The newest version!
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.ponfee.disjob.supervisor.dao.mapper.SchedTaskMapper">

  <sql id="Table_Name">sched_task</sql>

  <sql id="Base_Column_List">
    task_id, instance_id, task_no, task_count, execute_state, worker
  </sql>

  <sql id="Large_Column_List">
    <include refid="Base_Column_List" />,
    execute_start_time, execute_end_time, task_param,
    execute_snapshot, dispatch_failed_count, error_msg
  </sql>

  <insert id="batchInsert" parameterType="collection" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
    INSERT INTO <include refid="Table_Name" /> (
      task_id,
      instance_id,
      task_no,
      task_count,
      task_param,
      execute_state,
      worker
    ) VALUES
    <foreach collection="collection" item="item" separator=",">
    (
      #{item.taskId,jdbcType=BIGINT},
      #{item.instanceId,jdbcType=BIGINT},
      #{item.taskNo,jdbcType=INTEGER},
      #{item.taskCount,jdbcType=INTEGER},
      #{item.taskParam,jdbcType=LONGVARCHAR},
      #{item.executeState,jdbcType=TINYINT},
      #{item.worker,jdbcType=VARCHAR}
    )
    </foreach>
  </insert>

  <select id="get" parameterType="_long" resultType="cn.ponfee.disjob.supervisor.model.SchedTask">
    SELECT <include refid="Large_Column_List" />
    FROM <include refid="Table_Name" />
    WHERE task_id = #{taskId,jdbcType=BIGINT}
  </select>

  <select id="findBaseByInstanceIdAndStates" resultType="cn.ponfee.disjob.supervisor.model.SchedTask">
    SELECT <include refid="Base_Column_List" />
    FROM <include refid="Table_Name" />
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
    <if test="states != null and states.size() != 0">
      AND execute_state IN (<foreach collection="states" separator="," item="state">#{state,jdbcType=TINYINT}</foreach>)
    </if>
  </select>

  <select id="findLargeByInstanceIdAndStates" resultType="cn.ponfee.disjob.supervisor.model.SchedTask">
    SELECT <include refid="Large_Column_List" />
    FROM <include refid="Table_Name" />
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
    <if test="states != null and states.size() != 0">
      AND execute_state IN (<foreach collection="states" separator="," item="state">#{state,jdbcType=TINYINT}</foreach>)
    </if>
  </select>

  <update id="incrementDispatchFailedCount">
    UPDATE <include refid="Table_Name" />
    SET dispatch_failed_count = (#{currentDispatchFailedCount,jdbcType=TINYINT} + 1)
    WHERE task_id = #{taskId,jdbcType=BIGINT}
      AND dispatch_failed_count = #{currentDispatchFailedCount,jdbcType=TINYINT}
      AND execute_state = 10
  </update>

  <!-- use `IFNULL` function reason: possibly re-execute the paused task -->
  <update id="start">
    UPDATE <include refid="Table_Name" />
    SET execute_state = 20,
        execute_start_time = IFNULL(execute_start_time, #{executeStartTime,jdbcType=TIMESTAMP}),
        worker = #{worker,jdbcType=VARCHAR},
        start_request_id = #{startRequestId,jdbcType=VARCHAR}
    WHERE task_id = #{taskId,jdbcType=BIGINT}
      AND execute_state = 10
  </update>

  <select id="checkStartIdempotent" resultType="_boolean">
    SELECT EXISTS (
      SELECT 1
      FROM <include refid="Table_Name" />
      WHERE task_id = #{taskId,jdbcType=BIGINT}
        AND execute_state = 20
        AND worker = #{worker,jdbcType=VARCHAR}
        AND start_request_id = #{startRequestId,jdbcType=VARCHAR}
    )
  </select>

  <update id="terminate">
    UPDATE <include refid="Table_Name" />
    SET <if test="executeEndTime != null">
          execute_end_time = IF(execute_start_time IS NULL, NULL, #{executeEndTime,jdbcType=TIMESTAMP}),
        </if>
        <if test="errorMsg!=null and errorMsg.length()>0">
          error_msg = #{errorMsg,jdbcType=VARCHAR},
        </if>
        execute_state = #{toState,jdbcType=TINYINT}
    WHERE task_id = #{taskId,jdbcType=BIGINT}
      <if test="worker != null and worker != ''">
        AND worker = #{worker,jdbcType=VARCHAR}
      </if>
      AND execute_state = #{fromState,jdbcType=TINYINT}
      AND execute_state != #{toState,jdbcType=TINYINT}
      AND execute_state IN (10, 20, 30)
  </update>

  <update id="updateStateByInstanceId">
    UPDATE <include refid="Table_Name" />
    SET <if test="executeEndTime != null">
          execute_end_time = IF(execute_start_time IS NULL, NULL, #{executeEndTime,jdbcType=TIMESTAMP}),
        </if>
        execute_state = #{toState,jdbcType=TINYINT}
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
      AND execute_state != #{toState,jdbcType=TINYINT}
      AND execute_state IN (<foreach collection="fromStateList" separator="," item="state">#{state,jdbcType=TINYINT}</foreach>)
  </update>

  <update id="forceChangeState">
    UPDATE <include refid="Table_Name" />
    SET execute_state = #{toState,jdbcType=TINYINT}
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
      AND execute_state != #{toState,jdbcType=TINYINT}
      AND execute_state != 20
      AND #{toState,jdbcType=TINYINT} != 20
  </update>

  <update id="savepoint">
    UPDATE <include refid="Table_Name" />
    SET execute_snapshot = #{executeSnapshot,jdbcType=LONGVARCHAR}
    WHERE task_id = #{taskId,jdbcType=BIGINT}
      AND execute_state = 20
      AND worker = #{worker,jdbcType=VARCHAR}
  </update>

  <delete id="deleteByInstanceId" parameterType="_long">
    DELETE FROM <include refid="Table_Name" />
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
      AND (SELECT cnt FROM (SELECT COUNT(*) cnt FROM <include refid="Table_Name" /> WHERE instance_id = #{instanceId,jdbcType=BIGINT} AND execute_state &lt; 40) t)=0
  </delete>

  <!-- 使用其它的批量方式需要添加jdbc参数:allowMultiQueries=true&rewriteBatchedStatements=true -->
  <!--
   | 当worker不相同时,可使用`CASE WHEN`语法批量UPDATE:
   | UPDATE sched_task SET worker =
   |   CASE
   |     WHEN task_id = 1 THEN 'a'
   |     WHEN task_id = 2 THEN 'b'
   |   END
   | WHERE id IN (1, 2)
  -->
  <update id="batchUpdateWorker">
    UPDATE <include refid="Table_Name" />
    SET worker = #{worker,jdbcType=VARCHAR}
    WHERE task_id IN (<foreach collection="taskIds" separator="," item="taskId">#{taskId,jdbcType=BIGINT}</foreach>)
      AND execute_state = 10
  </update>

</mapper>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy