Created
November 8, 2023 14:14
-
-
Save hitenpratap/6cf42bf01d657bc35dec938020af89d8 to your computer and use it in GitHub Desktop.
A code snippet to requeue all the failed jobs in JobRunr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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