Commit fc6672a4 authored by nanahira's avatar nanahira

add randomInterval

parent 6cc7e356
......@@ -27,7 +27,7 @@ export default class ExtrasInDev {
instances: [
{
message: 'dress',
interval: 5000,
randomInterval: { max: 10000, min: 5000 },
immediate: true,
targets: [
{
......
// import 'source-map-support/register';
import { Context } from 'koishi';
import { Context, Random } from 'koishi';
import { SendTarget } from 'koishi-target-def';
import { SchemaProperty } from 'koishi-thirdeye';
import { RegisterSchema, SchemaProperty } from 'koishi-thirdeye';
import { scheduleJob } from 'node-schedule';
@RegisterSchema()
export class RandomInterval {
@SchemaProperty({ step: 1, default: 0 })
max: number;
@SchemaProperty({ step: 1, default: 0 })
min: number;
getInterval() {
return Random.int(this.min, this.max + 1);
}
isAvailable() {
return !!this.max && !!this.min;
}
processRandom(ctx: Context, callback: () => void) {
ctx.setTimeout(() => {
callback();
this.processRandom(ctx, callback);
}, this.getInterval());
}
}
export class SchedulePluginConfig {
@SchemaProperty({ description: 'cron 时间' })
@SchemaProperty({ description: 'cron 时间' })
cron: string;
@SchemaProperty({ description: '执行的间隔 ms', step: 1 })
@SchemaProperty({ description: '执行的间隔 ms', step: 1 })
interval: number;
@SchemaProperty({ description: '随机间隔。' })
randomInterval: RandomInterval;
@SchemaProperty({
default: false,
description: '插件启动时是否立即运行一遍。',
......@@ -30,11 +57,14 @@ export class SchedulePluginConfig {
if (this.cron) {
const job = scheduleJob(this.cron, callback);
ctx.on('dispose', () => {
job.cancel()
job.cancel();
});
}
if (this.interval) {
ctx.setInterval(callback, this.interval);
}
if (this.randomInterval?.isAvailable()) {
this.randomInterval.processRandom(ctx, callback);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment