example.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 == 'pause') {
  55. oneTask = this.taskForJob[jobId];
  56. // 不支持window平台
  57. oneTask.sleep();
  58. }
  59. if (action == 'continue') {
  60. oneTask = this.taskForJob[jobId];
  61. // 不支持window平台
  62. oneTask.wakeup();
  63. }
  64. if (action == 'close') {
  65. oneTask = this.taskForJob[jobId];
  66. oneTask.kill();
  67. const channel = 'controller.example.timerJobProgress';
  68. event.reply(`${channel}`, {jobId, number:0, pid:0});
  69. }
  70. return res;
  71. }
  72. /**
  73. * 执行任务
  74. */
  75. doCreatePool(num, event) {
  76. const channel = 'controller.example.createPoolNotice';
  77. // let pids = await myjobPool.create(num);
  78. this.myJobPool.create(num).then(pids => {
  79. event.reply(`${channel}`, pids);
  80. });
  81. }
  82. /**
  83. * 通过进程池执行任务
  84. */
  85. doJobByPool(jobId, type, event) {
  86. let pid = 0;
  87. if (type == 'timer') {
  88. // 执行任务及监听进度
  89. const channel = 'controller.example.timerJobProgress';
  90. const timerTask = this.myJobPool.run('./jobs/example/timer', {jobId});
  91. timerTask.emitter.on('job-timer-progress', (data) => {
  92. Log.info('[main-process] [ChildPoolJob] timerTask, from TimerJob data:', data);
  93. // 发送数据到渲染进程
  94. event.sender.send(`${channel}`, data)
  95. })
  96. pid = timerTask.pid;
  97. }
  98. return pid;
  99. }
  100. /**
  101. * test
  102. */
  103. monitorJob() {
  104. setInterval(() => {
  105. let jobPids = this.myJob.getPids();
  106. let jobPoolPids = this.myJobPool.getPids();
  107. Log.info(`[main-process] [monitorJob] jobPids: ${jobPids}, jobPoolPids: ${jobPoolPids}`);
  108. }, 5000)
  109. }
  110. /**
  111. * 上传到smms
  112. */
  113. async uploadFileToSMMS(tmpFile) {
  114. const res = {
  115. code: 1000,
  116. message: 'unknown error',
  117. };
  118. try {
  119. const headersObj = {
  120. 'Content-Type': 'multipart/form-data',
  121. 'Authorization': 'aaaaaaaaaaaaa' // 请修改这个token,用你自己的账号token
  122. };
  123. const url = 'https://sm.ms/api/v2/upload';
  124. const response = await this.app.curl(url, {
  125. method: 'POST',
  126. headers: headersObj,
  127. files: {
  128. smfile: tmpFile,
  129. },
  130. dataType: 'json',
  131. timeout: 15000,
  132. });
  133. const result = response.data;
  134. if (this.app.config.env === 'local') {
  135. Log.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  136. }
  137. if (result.code !== 'success') {
  138. Log.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  139. }
  140. return result;
  141. } catch (e) {
  142. Log.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  143. }
  144. return res;
  145. }
  146. }
  147. ExampleService.toString = () => '[class ExampleService]';
  148. module.exports = ExampleService;