-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement checker loader, load `app/checker`. Checkers will be called after all readyCallbacks called, then emit `app.ready(true)`. Refs: eggjs/egg#2520
- Loading branch information
零弌
committed
May 16, 2018
1 parent
a43dcb9
commit 4de9ecb
Showing
19 changed files
with
276 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const is = require('is-type-of'); | ||
const assert = require('assert'); | ||
|
||
module.exports = { | ||
loadChecker(opt) { | ||
opt = Object.assign({ | ||
caseStyle: 'lower', | ||
directory: path.join(this.options.baseDir, 'app/checker'), | ||
initializer: (obj, opt) => { | ||
const { pathName, path } = opt; | ||
assert(is.function(obj) || is.class(obj), `${path} checker should be function or class`); | ||
if (is.function(obj) && !is.generatorFunction(obj) && !is.class(obj) && !is.asyncFunction(obj)) { | ||
obj = obj(this.app); | ||
} | ||
if (is.class(obj)) { | ||
obj.prototype.pathName = pathName; | ||
obj.prototype.fullPath = path; | ||
assert(is.function(obj.prototype.check), `${path} checker should have check method`); | ||
return new obj(this.app); | ||
} | ||
if (is.function(obj)) { | ||
return () => obj; | ||
} | ||
assert.fail(`${path} checker is invalidate`); | ||
}, | ||
}, opt); | ||
const checkerBase = opt.directory; | ||
|
||
this.loadToApp(checkerBase, 'checker', opt); | ||
this.options.logger.info('[egg:loader] Checker loaded: %s', checkerBase); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
class BaseCheckerClass { | ||
/** | ||
* @constructor | ||
* @param {Application} app - egg application | ||
* @since 4.8.0 | ||
*/ | ||
constructor(app) { | ||
/** | ||
* @member {Application} BaseContextClass#app | ||
* @since 4.8.0 | ||
*/ | ||
this.app = app; | ||
} | ||
|
||
/** | ||
* check app status before app ready callbacks called | ||
* @since 4.8.0 | ||
*/ | ||
check() { | ||
assert.fail('should implement in subclass'); | ||
} | ||
} | ||
|
||
module.exports = BaseCheckerClass; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
const sleep = require('mz-modules/sleep'); | ||
|
||
module.exports = function(app) { | ||
app.readyQueue = []; | ||
app.beforeStart(async () => { | ||
await sleep(20); | ||
app.readyQueue.push('beforeStart'); | ||
}); | ||
app.ready(() => { | ||
app.readyQueue.push('ready'); | ||
}); | ||
}; |
10 changes: 10 additions & 0 deletions
10
test/fixtures/checker-app/app/checker/async_function_checker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = function(app) { | ||
return async function() { | ||
assert.strictEqual(app.name, 'checker-app'); | ||
app.readyQueue.push('async_function_checker'); | ||
}; | ||
}; |
12 changes: 12 additions & 0 deletions
12
test/fixtures/checker-app/app/checker/class_async_function_checker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = app => { | ||
return class AsyncChecker extends app.Checker { | ||
async check() { | ||
assert.strictEqual(this.app.name, 'checker-app'); | ||
this.app.readyQueue.push('class_async_function_checker'); | ||
} | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
test/fixtures/checker-app/app/checker/class_function_checker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = app => { | ||
return class FuncChecker extends app.Checker { | ||
check() { | ||
assert.strictEqual(this.app.name, 'checker-app'); | ||
this.app.readyQueue.push('class_function_checker'); | ||
} | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
test/fixtures/checker-app/app/checker/class_generator_function_checker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = app => { | ||
return class GeneratorChecker extends app.Checker { | ||
* check() { | ||
assert.strictEqual(this.app.name, 'checker-app'); | ||
this.app.readyQueue.push('class_generator_function_checker'); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = function(app) { | ||
return function() { | ||
assert.strictEqual(app.name, 'checker-app'); | ||
app.readyQueue.push('function_checker'); | ||
}; | ||
}; |
10 changes: 10 additions & 0 deletions
10
test/fixtures/checker-app/app/checker/generator_function_checker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = function(app) { | ||
return function* () { | ||
assert.strictEqual(app.name, 'checker-app'); | ||
app.readyQueue.push('generator_function_checker'); | ||
}; | ||
}; |
14 changes: 14 additions & 0 deletions
14
test/fixtures/checker-app/app/checker/module_class_function_checker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = class AsyncChecker { | ||
constructor(app) { | ||
this.app = app; | ||
} | ||
|
||
async check() { | ||
assert.strictEqual(this.app.name, 'checker-app'); | ||
this.app.readyQueue.push('module_class_function_checker'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "checker-app" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.