Skip to content

Commit

Permalink
feat(server): add options to disable cron
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Sep 21, 2024
1 parent 50a7a61 commit a55156d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.Server
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM node:20.10.0

ENV PORT=3000
ENV HOST=0.0.0.0 PORT=3000 CRON=false

EXPOSE 3000

Expand Down
2 changes: 1 addition & 1 deletion packages/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
169 changes: 86 additions & 83 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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);
}
})()
]);
});
}

0 comments on commit a55156d

Please sign in to comment.