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>
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.springframework.data.jpa.repository.JpaSpecificationExecutor
org.springframework.data.jpa.repository.JpaSpecificationExecutor.SpecificationFluentQuery<T> -
Method Summary
Modifier and TypeMethodDescriptionlongcountByStatusIn(Collection<JobStatus> statuses) Counts jobs in the selected lifecycle states.booleanexistsByConcurrencyKeyAndStatusIn(String concurrencyKey, Collection<JobStatus> statuses) Checks for active work using the same concurrency key.booleanexistsByStatusInAndType(Collection<JobStatus> statuses, JobType jobType) Returns whether a job of this type currently occupies any of the given lifecycle states.Finds and locks a job for a lifecycle transition.findByJobIdForUpdate(String jobId) Finds and locks a job by its public identifier.findJobByIdempotencyKey(String idempotencyKey) Finds a job by an idempotency key.findJobByJobId(String jobId) Finds aJobby its job is.findJobsByRootJobIdOrderByAttemptNumberAsc(String rootJobId) Finds all attempts for a root job in attempt order.findJobsByStatusInAndCompletionTimeBefore(Collection<JobStatus> statuses, LocalDateTime completionTimeBefore) Returns attempts in the given lifecycle states whose completion time is before the supplied date.findJobsByStatusInAndExecutionTimeBefore(Collection<JobStatus> statuses, LocalDateTime executionTimeBefore) Returns attempts in the given lifecycle states whose execution started before the supplied date.findLatestAttemptNumbers(Collection<String> rootJobIds) Resolves latest attempt numbers for a set of lineages in one bounded query.findLatestCompletionTime(JobStatus status, JobType jobType) Finds when a job of this type last reached the given status.findOldestQueuedAt(Collection<JobStatus> statuses) Finds the oldest queued time among selected lifecycle states.lockDueJobIds(LocalDateTime now, int limit) Locks due jobs without waiting for jobs already claimed by another dispatcher.lockExpiredJobIds(LocalDateTime now, int limit) Finds expired running attempts for recovery.intrenewLease(Long id, String workerId, long fencingToken, LocalDateTime leaseExpiresAt) Renews a lease only while the caller still owns the current fencing token.Methods inherited from interface org.springframework.data.repository.CrudRepository
count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, saveMethods inherited from interface org.springframework.data.jpa.repository.JpaRepository
deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlushMethods inherited from interface org.springframework.data.jpa.repository.JpaSpecificationExecutor
count, count, delete, delete, exists, exists, findAll, findAll, findAll, findAll, findAll, findBy, findBy, findOne, findOne, updateMethods inherited from interface org.springframework.data.repository.ListCrudRepository
findAll, findAllById, saveAllMethods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.PagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
count, exists, findAll, findBy, findOne
-
Method Details
-
findJobByJobId
-
findByJobIdForUpdate
-
findJobByIdempotencyKey
-
findByIdForUpdate
-
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 timelimit- 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 timelimit- 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 identifierworkerId- worker identifierfencingToken- ownership versionleaseExpiresAt- new lease expiry- Returns:
- affected row count
-
existsByConcurrencyKeyAndStatusIn
Checks for active work using the same concurrency key.- Parameters:
concurrencyKey- concurrency keystatuses- active statuses- Returns:
- true when matching work exists
-
findJobsByRootJobIdOrderByAttemptNumberAsc
-
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
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. -
existsByStatusInAndType
Returns whether a job of this type currently occupies any of the given lifecycle states.- Parameters:
statuses- lifecycle statesjobType-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-CollectionofJobStatuscompletionTimeBefore- lookback periodLocalDateTime- Returns:
ListofJob
-
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-CollectionofJobStatusexecutionTimeBefore- lookback periodLocalDateTime- Returns:
ListofJob
-