Skip to content

Commit

Permalink
Merge branch 'egg-ts-mongoose-template' of https://github.com/liangwe…
Browse files Browse the repository at this point in the history
…i0101/egg-demo into egg-ts-mongoose-template

# Conflicts:
#	app/schedule/addUserJob.ts
  • Loading branch information
liangwei0101 committed Nov 19, 2019
2 parents e483553 + ea95245 commit 795d499
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
27 changes: 10 additions & 17 deletions app/schedule/addUserJob.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@

import { Subscription } from 'egg'
import { UserModel } from '../model/User'

/**
* 间隔时间段,定时任务测试
*/
export default class AddUserJob extends Subscription {

static get schedule() {
return {
interval: '10m', // 60s 分钟间隔
interval: '60s', // 60s 间隔
type: 'all', // 指定所有的 worker 都需要执行
};
}

// subscribe 是真正定时任务执行时被运行的函数
async subscribe() {
console.log('我是60s执行一次的定时任务!!' + new Date())
const ctx = this.ctx;

const user = new UserModel();
user.userName = 'add user';
user.userNo = Math.floor(Math.random()*10);
console.log('====================')
console.log(user)
console.log('====================')
const aa = await user.save();
console.log('====================1')
console.log(aa)
console.log('====================1')
console.log('每60s执行一次增加User的定时任务!!' + new Date())

const test = await ctx.service.user.addUserByScheduleTest();

console.log(test)
}

}
}
11 changes: 11 additions & 0 deletions app/service/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ export default class UserService extends Service {
return `hi, ${name}`;
}

public async addUserByScheduleTest() {

const user = new UserModel();
user.userName = 'add user';
user.userNo = 99;

const res = await UserModel.create(user);

return res
}

/**
* 测试用户的实例方法
*/
Expand Down
4 changes: 4 additions & 0 deletions test/app/service/Test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ describe('test/app/service/Test.test.js', () => {
assert(result === 'hi, egg');
});

it('addUserByScheduleTest', async () => {
const result = await ctx.service.user.addUserByScheduleTest();
assert(result.userNo === 99);
});

it('testUserInstanceServiceMethods', async () => {
const user = await ctx.service.user.testUserInstanceServiceMethods();
Expand Down

0 comments on commit 795d499

Please sign in to comment.