Created
January 10, 2023 02:46
-
-
Save matrixcloud/e0588fe25f7e7c94d13846ecb7565460 to your computer and use it in GitHub Desktop.
javascript to get pageable data by generator
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 axios from "axios" | |
const BASE_API = 'http://localhost:9090/actuator' | |
async function scheduleJob(jobId) { | |
try { | |
await axios.put(`${BASE_API}/jobs/${jobId}/schedule`) | |
} catch(e) { | |
console.error(`failed to schedule job ${jobId} due to ${e.message}`) | |
} | |
} | |
async function* fetchJobs() { | |
try { | |
const res = await axios.get(`${BASE_API}/jobs`) | |
const { content, totalPages } = res.data | |
yield content | |
for (let i = 1; i < totalPages; i++) { | |
const res = await axios.get(`${BASE_API}/jobs?page=${i}`) | |
yield res.data.content | |
} | |
} catch (e) { | |
console.error(`failed to fetch jobs due to ${e.message}`) | |
} | |
} | |
(async function main() { | |
const jobs = [] | |
for await (const val of fetchJobs()) { | |
jobs.push([...val]) | |
} | |
jobs | |
.filter(job => job.status !== 'CREATED') | |
.forEach(async (job) => { | |
console.log(`start to schedule job: ${job.name}`) | |
await scheduleJob(job.id) | |
console.log(`finish to schedule job: ${job.name}`) | |
}) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment