Skip to content

Commit

Permalink
feat: extend runInBackground on application (eggjs#442)
Browse files Browse the repository at this point in the history
use mz-modules/sleep instead of ko-sleep
  • Loading branch information
fengmk2 authored and popomore committed Feb 24, 2017
1 parent fbcc1cc commit 80a06fc
Show file tree
Hide file tree
Showing 19 changed files with 62 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const proto = module.exports = {

/**
* Run generator function in the background
* @param {Generator} scope - generator function, the first args is ctx
* @param {Generator} scope - generator function, the first args is ctx
* ```js
* this.body = 'hi';
*
Expand Down
11 changes: 10 additions & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class Application extends EggApplication {
this[LOCALS_LIST].push(val);
}


/**
* Create egg context
* @method Application#createContext
Expand Down Expand Up @@ -156,6 +155,16 @@ class Application extends EggApplication {
return this.createContext(request, response);
}

/**
* Run generator function in the background
* @see Context#runInBackground
* @param {Generator} scope - generator function, the first args is an anonymous ctx
*/
runInBackground(scope) {
const ctx = this.createAnonymousContext();
ctx.runInBackground(scope);
}

/**
* secret key for Application
* @member {String} Application#keys
Expand Down
1 change: 0 additions & 1 deletion lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ class EggApplication extends EggCore {
return realClient;
};
}

}

module.exports = EggApplication;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"gh-pages": "^0.12.0",
"glob": "^7.1.1",
"jsdoc": "^3.4.3",
"ko-sleep": "^1.0.2",
"merge-descriptors": "^1.0.1",
"moment": "^2.17.1",
"mz": "^2.6.0",
Expand Down
27 changes: 25 additions & 2 deletions test/app/extend/application.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

const request = require('supertest');
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');
const fs = require('fs');
const path = require('path');
const utils = require('../../utils');

describe('test/app/extend/application.test.js', () => {

describe('app.logger', () => {
let app;
before(() => {
Expand Down Expand Up @@ -121,4 +122,26 @@ describe('test/app/extend/application.test.js', () => {
config.foo.should.equal('barrr');
});
});

describe('app.runInBackground(scope)', () => {
let app;
before(() => {
app = utils.app('apps/ctx-background');
return app.ready();
});
after(() => app.close());

it('should run background task success', function* () {
yield request(app.callback())
.get('/app_background')
.expect(200)
.expect('hello app');
yield sleep(5000);
const logdir = app.config.logger.dir;
const log = fs.readFileSync(path.join(logdir, 'ctx-background-web.log'), 'utf8');
log.should.match(/mock background run at app result file size: \d+/);
fs.readFileSync(path.join(logdir, 'egg-web.log'), 'utf8')
.should.match(/\[egg:background] task:saveUserInfo success \(\d+ms\)/);
});
});
});
2 changes: 1 addition & 1 deletion test/app/extend/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs');
const path = require('path');
const mm = require('egg-mock');
const request = require('supertest');
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');
const assert = require('assert');
const utils = require('../../utils');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const EventEmitter = require('events').EventEmitter;
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');

class MockClient extends EventEmitter {
constructor(options) {
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/apps/ctx-background/app/controller/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const fs = require('mz/fs');

module.exports = function* () {
this.body = 'hello app';
this.app.runInBackground(function* saveUserInfo(ctx) {
const buf = yield fs.readFile(__filename);
ctx.logger.warn('mock background run at app result file size: %s', buf.length);
});
};
1 change: 1 addition & 0 deletions test/fixtures/apps/ctx-background/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

module.exports = app => {
app.get('/', app.controller.home);
app.get('/app_background', app.controller.app);
app.get('/error', app.controller.error);
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/ctx-background/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'foo';
2 changes: 1 addition & 1 deletion test/fixtures/apps/dumpconfig/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');

module.exports = app => {
app.config.dynamic = 1;
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/apps/logger-level-debug/app/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const sleep = require('ko-sleep');

const sleep = require('mz-modules/sleep');

module.exports = app => {
app.get('/', function*() {
Expand Down
2 changes: 1 addition & 1 deletion test/lib/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const assert = require('assert');
const mm = require('egg-mock');
const request = require('supertest');
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');
const fs = require('fs');
const path = require('path');
const Application = require('../../lib/application');
Expand Down
2 changes: 1 addition & 1 deletion test/lib/cluster/cluster-client-error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');
const utils = require('../../utils');

describe('test/lib/cluster/cluster-client-error.test.js', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/lib/cluster/master.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const mm = require('egg-mock');
const request = require('supertest');
const coffee = require('coffee');
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');
const utils = require('../../utils');


Expand Down
2 changes: 1 addition & 1 deletion test/lib/core/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fs = require('fs');
const mm = require('egg-mock');
const request = require('supertest');
const Logger = require('egg-logger');
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');

const utils = require('../../utils');

Expand Down
2 changes: 1 addition & 1 deletion test/lib/core/messenger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const assert = require('assert');
const mm = require('egg-mock');
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');
const utils = require('../../utils');
const Messenger = require('../../../lib/core/messenger');

Expand Down
2 changes: 1 addition & 1 deletion test/lib/egg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const assert = require('assert');
const path = require('path');
const fs = require('fs');
const request = require('supertest');
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');
const utils = require('../utils');

describe('test/lib/egg.test.js', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/lib/plugins/watcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require('should');
const mm = require('egg-mock');
const fs = require('fs');
const request = require('supertest');
const sleep = require('ko-sleep');
const sleep = require('mz-modules/sleep');
const utils = require('../../utils');
const file_path1 = utils.getFilepath('apps/watcher-development-app/tmp.txt');
const file_path2 = utils.getFilepath('apps/watcher-development-app/tmp/tmp.txt');
Expand Down

0 comments on commit 80a06fc

Please sign in to comment.