diff --git a/app/schedule/addUserJob.ts b/app/schedule/addUserJob.ts index 6919c9c..676d618 100644 --- a/app/schedule/addUserJob.ts +++ b/app/schedule/addUserJob.ts @@ -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) } - -} \ No newline at end of file +} diff --git a/app/service/user.ts b/app/service/user.ts index 99d3c93..cf96430 100644 --- a/app/service/user.ts +++ b/app/service/user.ts @@ -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 + } + /** * 测试用户的实例方法 */ diff --git a/test/app/service/Test.test.ts b/test/app/service/Test.test.ts index 14d7e78..96a6218 100644 --- a/test/app/service/Test.test.ts +++ b/test/app/service/Test.test.ts @@ -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();