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

cn.ponfee.disjob.supervisor.dao.xml.SchedInstanceMapper.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.SchedInstanceMapper">

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

  <sql id="Base_Column_List">
    instance_id, rnstance_id, pnstance_id, wnstance_id, job_id,
    trigger_time, run_type, is_retrying AS retrying, run_state,
    run_start_time, run_end_time, retried_count, next_scan_time,
    workflow_cur_node, version
  </sql>

  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="cn.ponfee.disjob.supervisor.model.SchedInstance" useGeneratedKeys="true">
    INSERT INTO <include refid="Table_Name" /> (
      instance_id,
      rnstance_id,
      pnstance_id,
      wnstance_id,
      job_id,
      trigger_time,
      run_type,
      unique_flag,
      run_state,
      run_start_time,
      retried_count,
      workflow_cur_node
    ) VALUES (
      #{instanceId,jdbcType=BIGINT},
      #{rnstanceId,jdbcType=BIGINT},
      #{pnstanceId,jdbcType=BIGINT},
      #{wnstanceId,jdbcType=BIGINT},
      #{jobId,jdbcType=BIGINT},
      #{triggerTime,jdbcType=BIGINT},
      #{runType,jdbcType=TINYINT},
      #{uniqueFlag,jdbcType=BIGINT},
      #{runState,jdbcType=TINYINT},
      #{runStartTime,jdbcType=TIMESTAMP},
      #{retriedCount,jdbcType=TINYINT},
      #{workflowCurNode,jdbcType=VARCHAR}
    )
  </insert>

  <select id="get" parameterType="_long" resultType="cn.ponfee.disjob.supervisor.model.SchedInstance">
    SELECT <include refid="Base_Column_List" />
    FROM <include refid="Table_Name" />
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
  </select>

  <select id="getJobId" parameterType="_long" resultType="long">
    SELECT job_id
    FROM <include refid="Table_Name" />
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
  </select>

  <select id="getByUniqueKey" resultType="cn.ponfee.disjob.supervisor.model.SchedInstance">
    SELECT <include refid="Base_Column_List"/>
    FROM <include refid="Table_Name" />
    WHERE job_id = #{jobId,jdbcType=BIGINT}
      AND trigger_time = #{triggerTime,jdbcType=BIGINT}
      AND run_type = #{runType,jdbcType=TINYINT}
      AND unique_flag = #{uniqueFlag,jdbcType=BIGINT}
  </select>

  <select id="getWnstanceId" parameterType="_long" resultType="long">
    SELECT wnstance_id
    FROM <include refid="Table_Name" />
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
  </select>

  <select id="findChildren" resultType="cn.ponfee.disjob.supervisor.model.SchedInstance">
    SELECT <include refid="Base_Column_List"/>
    FROM <include refid="Table_Name" />
    WHERE pnstance_id = #{pnstanceId}
        <if test="runType != null">
          AND run_type = #{runType}
        </if>
  </select>

  <!-- use `IFNULL` function reason: possibly paused task re-execute -->
  <update id="start">
    UPDATE <include refid="Table_Name" />
    SET run_state = 20,
        run_start_time = IFNULL(run_start_time, #{runStartTime,jdbcType=TIMESTAMP}),
        version = version+1
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
      AND run_state = 10
  </update>

  <!--
   SELECT ROUND(TIMESTAMPDIFF(MICROSECOND, '2000-01-01 00:00:00.987', '2000-01-01 00:00:00.999') / 1000)
   SELECT ROUND((UNIX_TIMESTAMP('2000-01-01 00:00:00.999') - UNIX_TIMESTAMP('2000-01-01 00:00:00.987')) * 1000)
  -->
  <update id="terminate">
    UPDATE <include refid="Table_Name" />
    SET run_state = #{toState,jdbcType=TINYINT},
      <if test="runEndTime != null">
        run_end_time = IF(run_start_time IS NULL, NULL, #{runEndTime,jdbcType=TIMESTAMP}),
      </if>
        version = version+1
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
      AND run_state IN (<foreach collection="fromStateList" separator="," item="state">#{state,jdbcType=TINYINT}</foreach>)
  </update>

  <update id="updateState">
    UPDATE <include refid="Table_Name" />
    SET run_state = #{toState,jdbcType=TINYINT},
        version = version+1
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
      AND run_state = #{fromState,jdbcType=TINYINT}
  </update>

  <update id="updateRetrying">
    UPDATE <include refid="Table_Name" />
    SET is_retrying = #{retrying,javaType=boolean,jdbcType=TINYINT},
        run_state = #{toState,jdbcType=TINYINT},
        version = version+1
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
      AND is_retrying = !#{retrying,javaType=boolean,jdbcType=TINYINT}
      AND run_state = #{fromState,jdbcType=TINYINT}
  </update>

  <select id="findExpireState" resultType="cn.ponfee.disjob.supervisor.model.SchedInstance">
    SELECT <include refid="Base_Column_List"/>
    FROM <include refid="Table_Name" />
    WHERE run_state = #{runState,jdbcType=TINYINT}
      AND trigger_time &lt; #{expireTime.time,jdbcType=BIGINT}
      AND next_scan_time &lt; #{expireTime,jdbcType=TIMESTAMP}
      AND (wnstance_id IS NULL OR instance_id != wnstance_id)
    ORDER BY trigger_time
    LIMIT #{size}
  </select>

  <!-- 在同一个时间点,只会存在一个重试实例(没有加唯一索引限制,只能由代码逻辑来保证) -->
  <select id="getRetrying" parameterType="_long" resultType="cn.ponfee.disjob.supervisor.model.SchedInstance">
    SELECT <include refid="Base_Column_List"/>
    FROM <include refid="Table_Name" />
    WHERE pnstance_id = #{instanceId,jdbcType=BIGINT}
      AND run_type = 3
      AND run_state &lt; 40
  </select>

  <select id="findRunRetry" parameterType="_long" resultType="cn.ponfee.disjob.supervisor.model.SchedInstance">
    SELECT <include refid="Base_Column_List"/>
    FROM <include refid="Table_Name" />
    WHERE pnstance_id = #{instanceId,jdbcType=BIGINT}
      AND run_type = 3
  </select>

  <select id="findWorkflowNode" parameterType="_long" resultType="cn.ponfee.disjob.supervisor.model.SchedInstance">
    SELECT <include refid="Base_Column_List"/>
    FROM <include refid="Table_Name" />
    WHERE wnstance_id = #{wnstanceId,jdbcType=BIGINT}
      AND wnstance_id != instance_id
  </select>

  <update id="updateNextScanTime">
    UPDATE <include refid="Table_Name" />
    SET next_scan_time = #{nextScanTime,jdbcType=TIMESTAMP},
        version = version+1
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
      AND version = #{version,jdbcType=INTEGER}
  </update>

  <select id="lock" parameterType="_long" resultType="cn.ponfee.disjob.supervisor.model.SchedInstance">
    SELECT <include refid="Base_Column_List" />
    FROM <include refid="Table_Name" />
    WHERE instance_id = #{instanceId,jdbcType=BIGINT}
    FOR UPDATE
  </select>

  <delete id="deleteByInstanceId" parameterType="_long">
    DELETE FROM <include refid="Table_Name" />
    WHERE instance_id = #{instanceId,jdbcType=BIGINT} AND run_state >= 40
  </delete>

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

  <!-- ********************page query******************** -->

  <select id="queryPageCount" resultType="_long">
    SELECT COUNT(*) cnt
    FROM <include refid="Table_Name" />
    <include refid="Query_Page_Condition" />
  </select>

  <select id="queryPageRecords" resultType="cn.ponfee.disjob.supervisor.model.SchedInstance">
    SELECT <include refid="Base_Column_List" />
    FROM <include refid="Table_Name" />
    <include refid="Query_Page_Condition" />
    ORDER BY trigger_time DESC
    LIMIT #{offset}, #{pageSize}
  </select>

  <select id="queryByPnstanceId" parameterType="_long" resultType="cn.ponfee.disjob.supervisor.model.SchedInstance">
    SELECT <include refid="Base_Column_List" />
    FROM <include refid="Table_Name" />
    WHERE pnstance_id = #{pnstanceId}
  </select>

  <select id="queryChildCount" parameterType="collection" resultType="map">
    SELECT pnstance_id AS pnstanceId, count(*) count
    FROM <include refid="Table_Name" />
    WHERE pnstance_id IN (<foreach collection="collection" separator="," item="jobId">#{jobId,jdbcType=BIGINT}</foreach>)
    GROUP BY pnstance_id
  </select>

  <sql id="Query_Page_Condition">
    <where>
      <if test="jobId != null">
        AND job_id = #{jobId}
      </if>
      <if test="instanceId != null">
        AND instance_id = #{instanceId}
      </if>
      <if test="root and instanceId == null">
        AND rnstance_id IS NULL
      </if>
      <if test="runType != null">
        AND run_type = #{runType}
      </if>
      <if test="runState != null">
        AND run_state = #{runState}
      </if>
      <if test="startTime != null">
        AND trigger_time >= #{startTime.time,javaType=_long}
      </if>
      <if test="endTime != null">
        AND trigger_time &lt;= #{endTime.time,javaType=_long}
      </if>
    </where>
  </sql>

</mapper>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy