example.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict';
  2. const { Service } = require('ee-core');
  3. const Log = require('ee-core/log');
  4. const { ChildJob, ChildPoolJob } = require('ee-core/jobs');
  5. /**
  6. * 示例服务(service层为单例)
  7. * @class
  8. */
  9. class ExampleService extends Service {
  10. constructor(ctx) {
  11. super(ctx);
  12. // 在构造函数中初始化一些变量
  13. this.myJob = new ChildJob();
  14. this.myJobPool = new ChildPoolJob();
  15. this.taskForJob = {};
  16. this.taskForJobPool = {};
  17. }
  18. /**
  19. * test
  20. */
  21. async test(args) {
  22. let obj = {
  23. status:'ok',
  24. params: args
  25. }
  26. return obj;
  27. }
  28. /**
  29. * 执行任务
  30. */
  31. doJob(jobId, action, event) {
  32. let res = {};
  33. let oneTask;
  34. const channel = 'controller.example.timerJobProgress';
  35. if (action == 'create') {
  36. // 执行任务及监听进度
  37. const timerTask = this.myJob.exec('./jobs/example/timer', {jobId});
  38. timerTask.emitter.on('job-timer-progress', (data) => {
  39. Log.info('[main-process] timerTask, from TimerJob data:', data);
  40. // 发送数据到渲染进程
  41. event.sender.send(`${channel}`, data)
  42. })
  43. // 执行任务及监听进度 异步
  44. // myjob.execPromise('./jobs/example/timer', {jobId}).then(task => {
  45. // task.emitter.on('job-timer-progress', (data) => {
  46. // Log.info('[main-process] timerTask, from TimerJob data:', data);
  47. // // 发送数据到渲染进程
  48. // event.sender.send(`${channel}`, data)
  49. // })
  50. // });
  51. res.pid = timerTask.pid;
  52. this.taskForJob[jobId] = timerTask;
  53. }
  54. if (action == 'close') {
  55. oneTask = this.taskForJob[jobId];
  56. oneTask.kill();
  57. event.reply(`${channel}`, {jobId, number:0, pid:0});
  58. }
  59. return res;
  60. }
  61. /**
  62. * 执行任务
  63. */
  64. doCreatePool(num, event) {
  65. const channel = 'controller.example.createPoolNotice';
  66. // let pids = await myjobPool.create(num);
  67. this.myJobPool.create(num).then(pids => {
  68. event.reply(`${channel}`, pids);
  69. });
  70. }
  71. /**
  72. * 通过进程池执行任务
  73. */
  74. doJobByPool(jobId, type, event) {
  75. let pid = 0;
  76. if (type == 'timer') {
  77. // 执行任务及监听进度
  78. const channel = 'controller.example.timerJobProgress';
  79. const timerTask = this.myJobPool.run('./jobs/example/timer', {jobId});
  80. timerTask.emitter.on('job-timer-progress', (data) => {
  81. Log.info('[main-process] [ChildPoolJob] timerTask, from TimerJob data:', data);
  82. // 发送数据到渲染进程
  83. event.sender.send(`${channel}`, data)
  84. })
  85. pid = timerTask.pid;
  86. }
  87. return pid;
  88. }
  89. /**
  90. * test
  91. */
  92. monitorJob() {
  93. setInterval(() => {
  94. let jobPids = this.myJob.getPids();
  95. let jobPoolPids = this.myJobPool.getPids();
  96. Log.info(`[main-process] [monitorJob] jobPids: ${jobPids}, jobPoolPids: ${jobPoolPids}`);
  97. }, 5000)
  98. }
  99. /**
  100. * 上传到smms
  101. */
  102. async uploadFileToSMMS(tmpFile) {
  103. const res = {
  104. code: 1000,
  105. message: 'unknown error',
  106. };
  107. try {
  108. const headersObj = {
  109. 'Content-Type': 'multipart/form-data',
  110. 'Authorization': 'aaaaaaaaaaaaa' // 请修改这个token,用你自己的账号token
  111. };
  112. const url = 'https://sm.ms/api/v2/upload';
  113. const response = await this.app.curl(url, {
  114. method: 'POST',
  115. headers: headersObj,
  116. files: {
  117. smfile: tmpFile,
  118. },
  119. dataType: 'json',
  120. timeout: 15000,
  121. });
  122. const result = response.data;
  123. if (this.app.config.env === 'local') {
  124. Log.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  125. }
  126. if (result.code !== 'success') {
  127. Log.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  128. }
  129. return result;
  130. } catch (e) {
  131. Log.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  132. }
  133. return res;
  134. }
  135. }
  136. ExampleService.toString = () => '[class ExampleService]';
  137. module.exports = ExampleService;