example.js 4.1 KB

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