1
0

app.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Application
  3. */
  4. import { Application, IBoot, Context } from 'egg';
  5. import * as moment from 'moment';
  6. // import { worker } from 'cluster';
  7. export default class AdminBoot implements IBoot {
  8. // private readonly app: Application;
  9. private readonly ctx: Context;
  10. constructor(app: Application) {
  11. // this.app = app;
  12. this.ctx = app.createAnonymousContext();
  13. // agent send
  14. app.messenger.on('init-task', () => {
  15. // 启动任务
  16. this.ctx.service.admin.sys.task.initTask();
  17. });
  18. // tgboot
  19. app.messenger.on('init-tgboot-api', () => {
  20. // const tg_wid = worker.id;
  21. this.ctx.service.admin.tg.tgbotApi.initTgbot();
  22. app.messenger.on('req-tgboot-api', async ({ reqId, cmd, data }) => {
  23. const result = await this.ctx.service.admin.tg.tgbotApi.handleReq(cmd, data);
  24. app.messenger.sendToApp(`resp-tgboot-api-${reqId}`, result);
  25. });
  26. });
  27. // Date time
  28. // eslint-disable-next-line no-extend-native
  29. Date.prototype.toJSON = function() {
  30. return moment(this).format('YYYY-MM-DD HH:mm:ss');
  31. };
  32. }
  33. configWillLoad() {
  34. // Ready to call configDidLoad,
  35. // Config, plugin files are referred,
  36. // this is the last chance to modify the config.
  37. }
  38. configDidLoad() {
  39. // Config, plugin files have loaded.
  40. }
  41. async didLoad() {
  42. // All files have loaded, start plugin here.
  43. }
  44. async willReady() {
  45. // All plugins have started, can do some thing before app ready.
  46. }
  47. async didReady() {
  48. // Worker is ready, can do some things
  49. // don't need to block the app boot.
  50. }
  51. async serverDidReady() {
  52. // Server is listening.
  53. }
  54. async beforeClose() {
  55. // Do some thing before app close.
  56. }
  57. }