-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: impl Application and Agent extend with Class #65
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
import { Agent as EggAgent } from 'egg'; | ||
import { BaseStrategy } from '../../lib/strategy/base.js'; | ||
import { TimerStrategy } from '../../lib/strategy/timer.js'; | ||
import { Schedule } from '../../lib/schedule.js'; | ||
|
||
const SCHEDULE = Symbol('agent#schedule'); | ||
const SCHEDULE = Symbol('agent schedule'); | ||
|
||
export default { | ||
export default class Agent extends EggAgent { | ||
/** | ||
* @member agent#ScheduleStrategy | ||
*/ | ||
ScheduleStrategy: BaseStrategy, | ||
get ScheduleStrategy() { | ||
return BaseStrategy; | ||
} | ||
|
||
/** | ||
* @member agent#TimerScheduleStrategy | ||
*/ | ||
TimerScheduleStrategy: TimerStrategy, | ||
get TimerScheduleStrategy() { | ||
return TimerStrategy; | ||
} | ||
|
||
/** | ||
* @member agent#schedule | ||
*/ | ||
get schedule() { | ||
if (!this[SCHEDULE]) { | ||
this[SCHEDULE] = new Schedule(this); | ||
this.lifecycle.registerBeforeClose(() => { | ||
return this[SCHEDULE].close(); | ||
}); | ||
let schedule = this[SCHEDULE] as Schedule; | ||
if (!schedule) { | ||
this[SCHEDULE] = schedule = new Schedule(this); | ||
} | ||
return this[SCHEDULE]; | ||
}, | ||
} as any; | ||
return schedule; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import { Application as EggApplication } from 'egg'; | ||
import { ScheduleWorker } from '../../lib/schedule_worker.js'; | ||
|
||
const SCHEDULE_WORKER = Symbol('application#scheduleWorker'); | ||
const SCHEDULE_WORKER = Symbol('application scheduleWorker'); | ||
|
||
export default { | ||
export default class Application extends EggApplication { | ||
/** | ||
* @member app#schedule | ||
*/ | ||
get scheduleWorker() { | ||
if (!this[SCHEDULE_WORKER]) { | ||
this[SCHEDULE_WORKER] = new ScheduleWorker(this); | ||
let scheduleWorker = this[SCHEDULE_WORKER] as ScheduleWorker; | ||
if (!scheduleWorker) { | ||
this[SCHEDULE_WORKER] = scheduleWorker = new ScheduleWorker(this); | ||
} | ||
return this[SCHEDULE_WORKER]; | ||
}, | ||
} as any; | ||
return scheduleWorker; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
import Agent from './app/extend/agent.js'; | ||
import Application from './app/extend/application.js'; | ||
import ApplicationUnittest from './app/extend/application.unittest.js'; | ||
|
||
export { Agent, Application, ApplicationUnittest }; | ||
export { ScheduleWorker } from './lib/schedule_worker.js'; | ||
export { Schedule } from './lib/schedule.js'; | ||
|
||
export * from './lib/types.js'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { debuglog } from 'node:util'; | ||
import type { Agent, EggLogger } from 'egg'; | ||
import type { EggLogger } from 'egg'; | ||
import { loadSchedule } from './load_schedule.js'; | ||
import type { EggScheduleItem, EggScheduleJobInfo } from './types.js'; | ||
import type { BaseStrategy } from './strategy/base.js'; | ||
import type Agent from '../app/extend/agent.js'; | ||
|
||
const debug = debuglog('@eggjs/schedule/lib/schedule'); | ||
|
||
|
@@ -30,7 +31,7 @@ export class Schedule { | |
} | ||
|
||
/** | ||
* load all schedule jobs, then initialize and register speical strategy | ||
* load all schedule jobs, then initialize and register special strategy | ||
*/ | ||
async init() { | ||
const scheduleItems = await loadSchedule(this.#agent); | ||
|
@@ -82,18 +83,18 @@ export class Schedule { | |
/** | ||
* start schedule | ||
*/ | ||
start() { | ||
async start() { | ||
debug('start'); | ||
this.closed = false; | ||
for (const instance of this.#strategyInstanceMap.values()) { | ||
instance.start(); | ||
} | ||
} | ||
|
||
close() { | ||
async close() { | ||
this.closed = true; | ||
for (const instance of this.#strategyInstanceMap.values()) { | ||
instance.close(); | ||
await instance.close(); | ||
} | ||
debug('close'); | ||
Comment on lines
+86
to
99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Missing await for async start() call in Schedule class The
🔗 Analysis chainVerify consistent async/await usage across the codebase Let's ensure all async method calls are properly awaited throughout the codebase. Based on the results, I can see that 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Find potential missing await keywords for start/close method calls
rg -A 2 "\\.(start|close)\\(\\)" --type ts
# Find all async method declarations to cross-reference
ast-grep --pattern 'async $_($$) {
$$$
}'
Length of output: 704 Script: #!/bin/bash
# Check if strategy instances have async start methods
ast-grep --pattern 'class $_ {
$$$
async start() {
$$$
}
$$$
}'
# Also check for non-async start methods to be thorough
ast-grep --pattern 'class $_ {
$$$
start() {
$$$
}
$$$
}'
Length of output: 18415 |
||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: start() method should await instance.start()
The start() method is marked async but doesn't await the instance.start() calls. This could lead to race conditions if strategy implementations rely on async initialization.
📝 Committable suggestion