Skip to content

Commit

Permalink
feat: the scheduled task scanning cycle has been changed to use a cro…
Browse files Browse the repository at this point in the history
…n expression, and the time interval variable for scheduled message deletion has been adjusted to EXPIRED_TIME.
  • Loading branch information
adolphnov committed Aug 17, 2024
1 parent 5ea58d5 commit 561ba79
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 24 deletions.
18 changes: 14 additions & 4 deletions adapter/docker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RedisCache } from 'cloudflare-worker-adapter/cache/redis.js';
import toml from 'toml';
import fs from 'fs';
import { default as worker } from 'chatgpt-telegram-workers';
import cron from 'node-cron';


const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379';
Expand All @@ -11,10 +12,19 @@ const cache = new RedisCache(redisUrl);
// 定时任务
const raw = fs.readFileSync('./config/config.toml');
const env = { ...toml.parse(raw).vars, DATABASE: cache };
if (env.SCHEDULE_TIME && env.SCHEDULE_TIME >= 5) {
setInterval(async () => {
await worker.scheduled(null, env, null);
}, env.SCHEDULE_TIME * 60 * 1000);
// if (env.EXPIRED_TIME && env.EXPIRED_TIME >= 5) {
// setInterval(async () => {
// await worker.scheduled(null, env, null);
// }, env.EXPIRED_TIME * 60 * 1000);
// }
if (env.EXPIRED_TIME && env.EXPIRED_TIME > 0 && env.CRON_CHECK_TIME) {
try {
cron.schedule(env.CRON_CHECK_TIME, async () => {
await worker.scheduled(null, env, null);
});
} catch (e) {
console.error('Failed to schedule cron job:', e);
}
}

adapter.startServer(
Expand Down
6 changes: 3 additions & 3 deletions adapter/docker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"license": "MIT",
"dependencies": {
"chatgpt-telegram-workers": "github:adolphnov/ChatGPT-Telegram-Workers#test",
"cloudflare-worker-adapter": "github:adolphnov/cloudflare-worker-adapter"
},
"devDependencies": {}
"cloudflare-worker-adapter": "github:adolphnov/cloudflare-worker-adapter",
"node-cron": "^3.0.3"
}
}
19 changes: 15 additions & 4 deletions adapter/local/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fetch from 'node-fetch';
import { ENV } from '../../src/config/env.js';
import toml from 'toml';
import { default as worker } from '../../main.js';
import cron from 'node-cron';

const config = JSON.parse(fs.readFileSync('./config.json', 'utf-8'));

Expand Down Expand Up @@ -60,10 +61,20 @@ try {
// 定时任务
const raw = fs.readFileSync('../../wrangler.toml');
const env = { ...toml.parse(raw).vars, DATABASE: cache };
if (env.SCHEDULE_TIME && env.SCHEDULE_TIME >= 5) {
setInterval(async () => {
await worker.scheduled(null, env, null);
}, env.SCHEDULE_TIME * 60 * 1000);
// if (env.EXPIRED_TIME && env.EXPIRED_TIME >= 5) {
// setInterval(async () => {
// await worker.scheduled(null, env, null);
// }, env.EXPIRED_TIME * 60 * 1000);
// }

if (env.EXPIRED_TIME && env.EXPIRED_TIME > 0 && env.CRON_CHECK_TIME) {
try {
cron.schedule(env.CRON_CHECK_TIME, async () => {
await worker.scheduled(null, env, null);
});
} catch (e) {
console.error('Failed to schedule cron job:', e);
}
}

} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion adapter/local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"license": "ISC",
"dependencies": {
"cloudflare-worker-adapter": "github:adolphnov/cloudflare-worker-adapter",
"https-proxy-agent": "^7.0.5"
"https-proxy-agent": "^7.0.5",
"node-cron": "^3.0.3"
}
}
2 changes: 1 addition & 1 deletion dist/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sha":"37f994b","timestamp":1723888992}
{"sha":"5ea58d5","timestamp":1723898343}
14 changes: 8 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,9 +1091,9 @@ var Environment = class {
// -- 版本数据 --
//
// 当前版本
BUILD_TIMESTAMP = 1723888992;
BUILD_TIMESTAMP = 1723898343;
// 当前版本 commit id
BUILD_VERSION = "37f994b";
BUILD_VERSION = "5ea58d5";
// -- 基础配置 --
/**
* @type {I18n | null}
Expand Down Expand Up @@ -1211,8 +1211,10 @@ var Environment = class {
// 发文的作者链接; 发文作者目前为机器人ID, 未设置时为anonymous
TELEGRAPH_AUTHOR_URL = "";
DISABLE_WEB_PREVIEW = false;
// 定时任务时间间隔, 单位:分钟, 最小间隔为5
SCHEDULE_TIME = -1;
// 消息过期时间, 单位: 分钟
EXPIRED_TIME = -1;
// 任务扫描周期 使用cron 表达式, 例如 '*/10 0-2,6-23 * * *' 表示每天0-2点,6-23点,每十分钟执行一次定时任务
CRON_CHECK_TIME = "";
// 定时删除群组消息的类型 提示信息:tip 普通对话:chat
SCHEDULE_GROUP_DELETE_TYPE = ["tip"];
// 定时删除私人消息的类型 命令对话:command与普通对话:chat
Expand Down Expand Up @@ -1476,7 +1478,7 @@ var Context = class {
this.SHARE_CONTEXT.chatId = message.chat.id;
this.SHARE_CONTEXT.speakerId = message.from.id || message.chat.id;
this.SHARE_CONTEXT.messageId = message.message_id;
if (ENV.SCHEDULE_TIME >= 5)
if (ENV.EXPIRED_TIME > 0)
this.SHARE_CONTEXT.sentMessageIds = /* @__PURE__ */ new Set();
}
/**
Expand Down Expand Up @@ -4578,7 +4580,7 @@ async function scheduledDeleteMessage(request, context) {
if (!scheduledData[botName][chatId]) {
scheduledData[botName][chatId] = [];
}
const offsetInMillisenconds = ENV.SCHEDULE_TIME * 60 * 1e3;
const offsetInMillisenconds = ENV.EXPIRED_TIME * 60 * 1e3;
scheduledData[botName][chatId].push({
id: [...sentMessageIds],
ttl: Date.now() + offsetInMillisenconds
Expand Down
2 changes: 1 addition & 1 deletion dist/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1723888992
1723898343
2 changes: 1 addition & 1 deletion src/config/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class Context {
this.SHARE_CONTEXT.chatId = message.chat.id;
this.SHARE_CONTEXT.speakerId = message.from.id || message.chat.id;
this.SHARE_CONTEXT.messageId = message.message_id;
if (ENV.SCHEDULE_TIME >= 5) this.SHARE_CONTEXT.sentMessageIds = new Set();
if (ENV.EXPIRED_TIME > 0) this.SHARE_CONTEXT.sentMessageIds = new Set();

}

Expand Down
6 changes: 4 additions & 2 deletions src/config/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,10 @@ class Environment {
// 发文的作者链接; 发文作者目前为机器人ID, 未设置时为anonymous
TELEGRAPH_AUTHOR_URL = '';
DISABLE_WEB_PREVIEW = false;
// 定时任务时间间隔, 单位:分钟, 最小间隔为5
SCHEDULE_TIME = -1;
// 消息过期时间, 单位: 分钟
EXPIRED_TIME = -1;
// 任务扫描周期 使用cron 表达式, 例如 '*/10 0-2,6-23 * * *' 表示每天0-2点,6-23点,每十分钟执行一次定时任务
CRON_CHECK_TIME = '';
// 定时删除群组消息的类型 提示信息:tip 普通对话:chat
SCHEDULE_GROUP_DELETE_TYPE = ['tip'];
// 定时删除私人消息的类型 命令对话:command与普通对话:chat
Expand Down
2 changes: 1 addition & 1 deletion src/telegram/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ async function scheduledDeleteMessage(request, context) {
if (!scheduledData[botName][chatId]) {
scheduledData[botName][chatId] = [];
}
const offsetInMillisenconds = ENV.SCHEDULE_TIME * 60 * 1000;
const offsetInMillisenconds = ENV.EXPIRED_TIME * 60 * 1000;
scheduledData[botName][chatId].push({
id: [...sentMessageIds],
ttl: Date.now() + offsetInMillisenconds,
Expand Down

0 comments on commit 561ba79

Please sign in to comment.