Skip to content

Instantly share code, notes, and snippets.

@hitenpratap
Created November 8, 2023 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitenpratap/6cf42bf01d657bc35dec938020af89d8 to your computer and use it in GitHub Desktop.
Save hitenpratap/6cf42bf01d657bc35dec938020af89d8 to your computer and use it in GitHub Desktop.
A code snippet to requeue all the failed jobs in JobRunr
import org.jobrunr.jobs.states.StateName
import org.jobrunr.storage.PageRequest
import org.jobrunr.storage.StorageProvider
import org.springframework.stereotype.Service
@Service
class FailedJobEnqueuer(
private val storageProvider: StorageProvider,
) {
fun requeueFailedJobs(): List<String> {
val pageSize = 100 // adjust as necessary
val failedJobIds = mutableListOf<String>()
generateSequence(0L) { it + pageSize } // Generate a sequence of offsets
.map { offset ->
// Create a PageRequest with the current offset
PageRequest.ascOnUpdatedAt(offset, pageSize)
}
.map { pageRequest ->
// Fetch a page of failed jobs
storageProvider.getJobPage(StateName.FAILED, pageRequest)
}
.takeWhile { failedJobsPage ->
// Take pages while there are jobs to process
failedJobsPage.items.isNotEmpty()
}
.flatMap { failedJobsPage ->
// Flatten the sequence of pages to a sequence of jobs
failedJobsPage.items.asSequence()
}
.onEach { failedJob ->
failedJob.enqueue() // Enqueue the job
storageProvider.save(failedJob) // Save the job status
failedJobIds.add(failedJob.id.toString()) // Collect the job ID if successful
}
.toList() // Consume the sequence and perform the operations
return failedJobIds
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment