Skip to content

Commit

Permalink
feat(schedule): optimize logger
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 5, 2020
1 parent b67a705 commit 1f0394d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 0 additions & 2 deletions packages/plugin-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export interface Config extends HandlerOptions, RepeaterOptions, SenderConfig {
contextify?: false
echo?: false
info?: false
usage?: false
debug?: DebugOptions
}

Expand All @@ -29,5 +28,4 @@ export function apply(ctx: Context, config: Config = {}) {
if (config.contextify !== false) ctx.plugin(require('./contextify'))
if (config.debug) ctx.plugin(require('./debug'), config.debug)
if (config.info !== false) ctx.plugin(require('./info'))
if (config.usage !== false) ctx.plugin(require('./usage'))
}
21 changes: 13 additions & 8 deletions packages/plugin-schedule/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ export * from './database'

const logger = new Logger('schedule')

function inspectSchedule({ id, session, interval, command, time }: Schedule) {
function prepareSchedule({ id, session, interval, command, time }: Schedule) {
const now = Date.now()
const date = time.valueOf()
const { database } = session.$app
logger.debug('inspect', command)
logger.debug('prepare %d: %c at %s', id, command, time)

function executeSchedule() {
logger.debug('execute %d: %c', id, command)
return session.$execute(command)
}

if (!interval) {
if (date < now) return database.removeSchedule(id)
return setTimeout(async () => {
if (!await database.getSchedule(id)) return
session.$execute(command)
database.removeSchedule(id)
await database.removeSchedule(id)
await executeSchedule()
}, date - now)
}

Expand All @@ -26,9 +31,9 @@ function inspectSchedule({ id, session, interval, command, time }: Schedule) {
if (!await database.getSchedule(id)) return
const timer = setInterval(async () => {
if (!await database.getSchedule(id)) return clearInterval(timer)
session.$execute(command)
await executeSchedule()
}, interval)
session.$execute(command)
await executeSchedule()
}, timeout)
}

Expand All @@ -46,7 +51,7 @@ export function apply(ctx: Context) {
schedules.forEach((schedule) => {
if (!ctx.bots[schedule.assignee]) return
schedule.session = new Session(ctx.app, schedule.session)
inspectSchedule(schedule)
prepareSchedule(schedule)
})
})

Expand Down Expand Up @@ -96,7 +101,7 @@ export function apply(ctx: Context) {
}

const schedule = await database.createSchedule(time, interval, options.rest, session)
inspectSchedule(schedule)
prepareSchedule(schedule)
return `日程已创建,编号为 ${schedule.id}。`
})
}

0 comments on commit 1f0394d

Please sign in to comment.