diff --git a/Dockerfile.Server b/Dockerfile.Server index cd7d960b..81f93578 100644 --- a/Dockerfile.Server +++ b/Dockerfile.Server @@ -1,6 +1,6 @@ FROM node:20.10.0 -ENV PORT=3000 +ENV HOST=0.0.0.0 PORT=3000 CRON=false EXPOSE 3000 diff --git a/packages/server/Dockerfile b/packages/server/Dockerfile index 135a19c5..6f9bdd75 100644 --- a/packages/server/Dockerfile +++ b/packages/server/Dockerfile @@ -56,7 +56,7 @@ FROM base COPY --from=build /app /app # Listen on 3000 -ENV HOST=:: PORT=3000 +ENV HOST=:: PORT=3000 CRON=true # Start the server by default, this can be overwritten at runtime EXPOSE 3000 diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index bdc52859..4e10e4c7 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -19,6 +19,7 @@ const query = registerQuery(); const hostname = process.env.HOST ?? process.env.host ?? '0.0.0.0'; const port = process.env.PORT ? +process.env.PORT : process.env.port ? +process.env.port : 3000; +const isCron = ['true', 'on'].includes(process.env.CRON ?? process.env.cron ?? ''); serve( { @@ -40,88 +41,90 @@ serve( } ); -Cron(`*/5 * * * *`, { timezone: 'Asia/Shanghai', protect: true }, async () => { - const result = await Promise.all([ - (async () => { - try { - logger.info(`cron`, `Fetch dmhy resources`); - const req = new Request(`https://api.zeabur.internal/admin/resources/dmhy`, { - method: 'POST' - }); - const resp = await app.fetch(req); - return (await resp.json()) as { count: number }; - } catch (error) { - console.error(error); - } - })(), - (async () => { - try { - logger.info(`cron`, `Fetch moe resources`); - const req = new Request(`https://api.zeabur.internal/admin/resources/moe`, { - method: 'POST' - }); - const resp = await app.fetch(req); - return (await resp.json()) as { count: number }; - } catch (error) { - console.error(error); - } - })(), - (async () => { - try { - logger.info(`cron`, `Fetch ani resources`); - const req = new Request(`https://api.zeabur.internal/admin/resources/ani`, { - method: 'POST' - }); - const resp = await app.fetch(req); - return (await resp.json()) as { count: number }; - } catch (error) { - console.error(error); - } - })() - ]); +if (isCron) { + Cron(`*/5 * * * *`, { timezone: 'Asia/Shanghai', protect: true }, async () => { + const result = await Promise.all([ + (async () => { + try { + logger.info(`cron`, `Fetch dmhy resources`); + const req = new Request(`https://api.zeabur.internal/admin/resources/dmhy`, { + method: 'POST' + }); + const resp = await app.fetch(req); + return (await resp.json()) as { count: number }; + } catch (error) { + console.error(error); + } + })(), + (async () => { + try { + logger.info(`cron`, `Fetch moe resources`); + const req = new Request(`https://api.zeabur.internal/admin/resources/moe`, { + method: 'POST' + }); + const resp = await app.fetch(req); + return (await resp.json()) as { count: number }; + } catch (error) { + console.error(error); + } + })(), + (async () => { + try { + logger.info(`cron`, `Fetch ani resources`); + const req = new Request(`https://api.zeabur.internal/admin/resources/ani`, { + method: 'POST' + }); + const resp = await app.fetch(req); + return (await resp.json()) as { count: number }; + } catch (error) { + console.error(error); + } + })() + ]); - if ((result[0]?.count ?? 0 + (result[1]?.count ?? 0)) > 0) { - await updateRefreshTimestamp(storage).catch(() => {}); - } -}); + if ((result[0]?.count ?? 0 + (result[1]?.count ?? 0)) > 0) { + await updateRefreshTimestamp(storage).catch(() => {}); + } + }); -Cron(`0 * * * *`, { timezone: 'Asia/Shanghai', protect: true }, async () => { - await Promise.all([ - (async () => { - try { - const req = new Request(`https://api.zeabur.internal/admin/resources/dmhy/sync`, { - method: 'POST' - }); - logger.info(`cron`, `Sync dmhy resources`); - const resp = await app.fetch(req); - logger.info(`cron`, JSON.stringify(await resp.json())); - } catch (error) { - console.error(error); - } - })(), - (async () => { - try { - const req = new Request(`https://api.zeabur.internal/admin/resources/moe/sync`, { - method: 'POST' - }); - logger.info(`cron`, `Sync moe resources`); - const resp = await app.fetch(req); - logger.info(`cron`, JSON.stringify(await resp.json())); - } catch (error) { - console.error(error); - } - })(), - (async () => { - try { - const req = new Request(`https://api.zeabur.internal/admin/resources/ani/sync`, { - method: 'POST' - }); - logger.info(`cron`, `Sync ani resources`); - const resp = await app.fetch(req); - logger.info(`cron`, JSON.stringify(await resp.json())); - } catch (error) { - console.error(error); - } - })() - ]); -}); + Cron(`0 * * * *`, { timezone: 'Asia/Shanghai', protect: true }, async () => { + await Promise.all([ + (async () => { + try { + const req = new Request(`https://api.zeabur.internal/admin/resources/dmhy/sync`, { + method: 'POST' + }); + logger.info(`cron`, `Sync dmhy resources`); + const resp = await app.fetch(req); + logger.info(`cron`, JSON.stringify(await resp.json())); + } catch (error) { + console.error(error); + } + })(), + (async () => { + try { + const req = new Request(`https://api.zeabur.internal/admin/resources/moe/sync`, { + method: 'POST' + }); + logger.info(`cron`, `Sync moe resources`); + const resp = await app.fetch(req); + logger.info(`cron`, JSON.stringify(await resp.json())); + } catch (error) { + console.error(error); + } + })(), + (async () => { + try { + const req = new Request(`https://api.zeabur.internal/admin/resources/ani/sync`, { + method: 'POST' + }); + logger.info(`cron`, `Sync ani resources`); + const resp = await app.fetch(req); + logger.info(`cron`, JSON.stringify(await resp.json())); + } catch (error) { + console.error(error); + } + })() + ]); + }); +}