chore(api): Retry on fail
This commit is contained in:
parent
1f569b1457
commit
4f2d61ffe6
3 changed files with 48 additions and 15 deletions
|
@ -13,7 +13,7 @@ app.use(bodyParser.json());
|
||||||
|
|
||||||
|
|
||||||
app.get(api + '/queue', (req, res) => {
|
app.get(api + '/queue', (req, res) => {
|
||||||
res.json(qm.getQueue());
|
res.json(qm.getQueue().map(qi => qi.album.href));
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post(api + '/queue', (req, res) => {
|
app.post(api + '/queue', (req, res) => {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import {firefox, Page, BrowserContext, Download, Browser, Locator} from 'playwright';
|
import {firefox, Page, BrowserContext, Download, Browser, Locator} from 'playwright';
|
||||||
|
|
||||||
export async function lucida(album: URL, baseTimeout: number, context: BrowserContext): Promise<void> {
|
export async function lucida(album: URL, baseTimeout: number, context: BrowserContext): Promise<boolean | string> {
|
||||||
const page: Page = await context.newPage();
|
const page: Page = await context.newPage();
|
||||||
|
|
||||||
await page.goto('/');
|
await page.goto('/');
|
||||||
|
@ -28,14 +28,21 @@ export async function lucida(album: URL, baseTimeout: number, context: BrowserCo
|
||||||
// Start download
|
// Start download
|
||||||
await page.getByText('download full album').click();
|
await page.getByText('download full album').click();
|
||||||
|
|
||||||
// Save the download to the Downloads folder
|
try {
|
||||||
const download: Download = await page.waitForEvent('download', { timeout: baseTimeout * trackCount });
|
// Save the download to the Downloads folder
|
||||||
// TODO Set path (configurable)
|
const download: Download = await page.waitForEvent('download', { timeout: baseTimeout * trackCount });
|
||||||
await download.saveAs('/home/tdpeuter/Downloads/lucida/' + download.suggestedFilename());
|
// TODO Set path (configurable)
|
||||||
|
await download.saveAs('/home/tdpeuter/Downloads/lucida/' + download.suggestedFilename());
|
||||||
|
} catch {
|
||||||
|
await page.close();
|
||||||
|
return 'Download timed out';
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Check if booklet is available.
|
// TODO Check if booklet is available.
|
||||||
|
|
||||||
await page.close();
|
await page.close();
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function booklet(album: URL, context: BrowserContext): Promise<void> {
|
async function booklet(album: URL, context: BrowserContext): Promise<void> {
|
||||||
|
|
|
@ -1,16 +1,25 @@
|
||||||
import {firefox, Browser, BrowserContext, Page} from "playwright";
|
import {firefox, Browser, BrowserContext, Page} from "playwright";
|
||||||
import {lucida} from "./lucida";
|
import {lucida} from "./lucida";
|
||||||
|
|
||||||
|
type QueueItem = {
|
||||||
|
album: URL,
|
||||||
|
timeout: number,
|
||||||
|
retries?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const DEFAULT_TIMEOUT: number = 60000;
|
||||||
|
const MAX_RETRIES: number = 3;
|
||||||
|
|
||||||
class QueueManager {
|
class QueueManager {
|
||||||
private browser: Browser | null;
|
private browser: Browser | null;
|
||||||
private queue: URL[];
|
private queue: QueueItem[];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.browser = null;
|
this.browser = null;
|
||||||
this.queue = [];
|
this.queue = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
getQueue(): URL[] {
|
getQueue(): QueueItem[] {
|
||||||
return this.queue;
|
return this.queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,13 +27,17 @@ class QueueManager {
|
||||||
// Check if the URL is a valid URL
|
// Check if the URL is a valid URL
|
||||||
try {
|
try {
|
||||||
const url: URL = new URL(link);
|
const url: URL = new URL(link);
|
||||||
|
const qi: QueueItem = {
|
||||||
|
album: url,
|
||||||
|
timeout: DEFAULT_TIMEOUT
|
||||||
|
};
|
||||||
|
|
||||||
// Check if the URL was already added to the queue
|
// Check if the URL was already added to the queue
|
||||||
if (this.queue.includes(url)) {
|
if (this.queue.some(q => q.album.href === qi.album.href)) {
|
||||||
return "Album already in queue";
|
return "Album already in queue";
|
||||||
}
|
}
|
||||||
|
|
||||||
this.queue.push(url);
|
this.queue.push(qi);
|
||||||
this.processQueue();
|
this.processQueue();
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -66,15 +79,28 @@ class QueueManager {
|
||||||
});
|
});
|
||||||
|
|
||||||
while (this.queue.length > 0) {
|
while (this.queue.length > 0) {
|
||||||
const album: URL | undefined = this.queue.shift();
|
const qi: QueueItem | undefined = this.queue.shift();
|
||||||
if (album === undefined) {
|
if (qi === undefined) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
const album: URL = qi.album;
|
||||||
console.log(`Processing ${album.href}`);
|
console.log(`Processing ${album.href}`);
|
||||||
await lucida(album, 60000, context);
|
const result: boolean | string = await lucida(album, qi.timeout, context);
|
||||||
console.log(`Finished processing ${album.href}`);
|
if (result === true) {
|
||||||
|
console.log(`Finished processing ${album.href}`);
|
||||||
|
} else {
|
||||||
|
if (qi.retries === undefined) {
|
||||||
|
qi.retries = 0;
|
||||||
|
}
|
||||||
|
qi.retries++;
|
||||||
|
qi.timeout *= 2;
|
||||||
|
if (qi.retries < MAX_RETRIES) {
|
||||||
|
this.queue.push(qi);
|
||||||
|
} else {
|
||||||
|
console.error(`Failed to process ${album.href} after 3 retries: ${result}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Shutting down browser');
|
console.log('Shutting down browser');
|
||||||
|
|
Reference in a new issue