Interface JobRepository

All Superinterfaces:
org.springframework.data.repository.CrudRepository<Job,Long>, org.springframework.data.jpa.repository.JpaRepository<Job,Long>, org.springframework.data.jpa.repository.JpaSpecificationExecutor<Job>, org.springframework.data.repository.ListCrudRepository<Job,Long>, org.springframework.data.repository.ListPagingAndSortingRepository<Job,Long>, org.springframework.data.repository.PagingAndSortingRepository<Job,Long>, org.springframework.data.repository.query.QueryByExampleExecutor<Job>, org.springframework.data.repository.Repository<Job,Long>

@Repository public interface JobRepository extends org.springframework.data.jpa.repository.JpaRepository<Job,Long>, org.springframework.data.jpa.repository.JpaSpecificationExecutor<Job>
Data-access layer for Job.
Version:
3.0.6
Author:
Stephen Prizio
  • Method Details

    • findJobByJobId

      Job findJobByJobId(String jobId)
      Finds a Job by its job is.
      Parameters:
      jobId - job id
      Returns:
      Job
    • findByJobIdForUpdate

      @Lock(PESSIMISTIC_WRITE) @Query("SELECT j FROM Job j WHERE j.jobId = :jobId") Optional<Job> findByJobIdForUpdate(@Param("jobId") String jobId)
      Finds and locks a job by its public identifier.
      Parameters:
      jobId - public job identifier
      Returns:
      locked job
    • findJobByIdempotencyKey

      Optional<Job> findJobByIdempotencyKey(String idempotencyKey)
      Finds a job by an idempotency key.
      Parameters:
      idempotencyKey - idempotency key
      Returns:
      matching job
    • findByIdForUpdate

      @Lock(PESSIMISTIC_WRITE) @Query("SELECT j FROM Job j WHERE j.id = :id") Optional<Job> findByIdForUpdate(@Param("id") Long id)
      Finds and locks a job for a lifecycle transition.
      Parameters:
      id - database identifier
      Returns:
      locked job
    • lockDueJobIds

      @Query(value="SELECT id FROM jobs\nWHERE status IN ('QUEUED', 'RETRY_PENDING')\n AND (not_before IS NULL OR not_before <= :now)\nORDER BY priority DESC, queued_at ASC, id ASC\nLIMIT :limit\nFOR UPDATE SKIP LOCKED\n", nativeQuery=true) List<Long> lockDueJobIds(@Param("now") LocalDateTime now, @Param("limit") int limit)
      Locks due jobs without waiting for jobs already claimed by another dispatcher.
      Parameters:
      now - current time
      limit - maximum claims
      Returns:
      locked database identifiers
    • lockExpiredJobIds

      @Query(value="SELECT id FROM jobs\nWHERE status IN ('RUNNING', 'CANCEL_REQUESTED')\n AND lease_expires_at < :now\nORDER BY lease_expires_at ASC, id ASC\nLIMIT :limit\nFOR UPDATE SKIP LOCKED\n", nativeQuery=true) List<Long> lockExpiredJobIds(@Param("now") LocalDateTime now, @Param("limit") int limit)
      Finds expired running attempts for recovery.
      Parameters:
      now - current time
      limit - maximum results
      Returns:
      locked expired job identifiers
    • renewLease

      @Modifying @Query("UPDATE Job j SET j.leaseExpiresAt = :leaseExpiresAt WHERE j.id = :id AND j.workerId = :workerId AND j.fencingToken = :fencingToken AND j.status IN ('RUNNING', 'CANCEL_REQUESTED')") int renewLease(@Param("id") Long id, @Param("workerId") String workerId, @Param("fencingToken") long fencingToken, @Param("leaseExpiresAt") LocalDateTime leaseExpiresAt)
      Renews a lease only while the caller still owns the current fencing token.
      Parameters:
      id - database identifier
      workerId - worker identifier
      fencingToken - ownership version
      leaseExpiresAt - new lease expiry
      Returns:
      affected row count
    • existsByConcurrencyKeyAndStatusIn

      boolean existsByConcurrencyKeyAndStatusIn(String concurrencyKey, Collection<JobStatus> statuses)
      Checks for active work using the same concurrency key.
      Parameters:
      concurrencyKey - concurrency key
      statuses - active statuses
      Returns:
      true when matching work exists
    • findJobsByRootJobIdOrderByAttemptNumberAsc

      List<Job> findJobsByRootJobIdOrderByAttemptNumberAsc(String rootJobId)
      Finds all attempts for a root job in attempt order.
      Parameters:
      rootJobId - public root identifier
      Returns:
      ordered attempts
    • findLatestAttemptNumbers

      @Query("SELECT j.rootJobId, MAX(j.attemptNumber) FROM Job j WHERE j.rootJobId IN :rootJobIds GROUP BY j.rootJobId") List<Object[]> findLatestAttemptNumbers(@Param("rootJobIds") Collection<String> rootJobIds)
      Resolves latest attempt numbers for a set of lineages in one bounded query.
      Parameters:
      rootJobIds - root lineage identifiers
      Returns:
      root identifier and maximum attempt rows
    • countByStatusIn

      long countByStatusIn(Collection<JobStatus> statuses)
      Counts jobs in the selected lifecycle states.
      Parameters:
      statuses - lifecycle states
      Returns:
      matching row count
    • findOldestQueuedAt

      @Query("SELECT MIN(j.queuedAt) FROM Job j WHERE j.status IN :statuses") LocalDateTime findOldestQueuedAt(@Param("statuses") Collection<JobStatus> statuses)
      Finds the oldest queued time among selected lifecycle states.
      Parameters:
      statuses - lifecycle states
      Returns:
      oldest queue time, or null
    • findLatestCompletionTime

      @Query("SELECT MAX(j.completionTime) FROM Job j WHERE j.status = :status AND j.type = :jobType") LocalDateTime findLatestCompletionTime(@Param("status") JobStatus status, @Param("jobType") JobType jobType)
      Finds when a job of this type last reached the given status. Answered by the database rather than by loading every matching job, because this is asked on every scheduled tick and the matching set grows for the lifetime of the retention window.
      Parameters:
      status - JobStatus
      jobType - JobType
      Returns:
      latest completion time, or null when none exists
    • existsByStatusInAndType

      boolean existsByStatusInAndType(Collection<JobStatus> statuses, JobType jobType)
      Returns whether a job of this type currently occupies any of the given lifecycle states.
      Parameters:
      statuses - lifecycle states
      jobType - JobType
      Returns:
      true when at least one matches
    • findJobsByStatusInAndCompletionTimeBefore

      List<Job> findJobsByStatusInAndCompletionTimeBefore(Collection<JobStatus> statuses, LocalDateTime completionTimeBefore)
      Returns attempts in the given lifecycle states whose completion time is before the supplied date.
      Parameters:
      statuses - Collection of JobStatus
      completionTimeBefore - lookback period LocalDateTime
      Returns:
      List of Job
    • findJobsByStatusInAndExecutionTimeBefore

      List<Job> findJobsByStatusInAndExecutionTimeBefore(Collection<JobStatus> statuses, LocalDateTime executionTimeBefore)
      Returns attempts in the given lifecycle states whose execution started before the supplied date.
      Parameters:
      statuses - Collection of JobStatus
      executionTimeBefore - lookback period LocalDateTime
      Returns:
      List of Job