example.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.sender.send(`${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. this.myJobPool.create(num).then(pids => {
  67. event.reply(`${channel}`, pids);
  68. });
  69. }
  70. /**
  71. * 通过进程池执行任务
  72. */
  73. doJobByPool(jobId, action, event) {
  74. let res = {};
  75. let timerTask;
  76. const channel = 'controller.example.timerJobProgress';
  77. if (action == 'create') {
  78. // 执行任务及监听进度
  79. 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. res.pid = timerTask.pid;
  86. // ???
  87. //this.taskForJobPool[jobId] = timerTask;
  88. }
  89. return res;
  90. }
  91. /**
  92. * test
  93. */
  94. monitorJob() {
  95. setInterval(() => {
  96. let jobPids = this.myJob.getPids();
  97. let jobPoolPids = this.myJobPool.getPids();
  98. Log.info(`[main-process] [monitorJob] jobPids: ${jobPids}, jobPoolPids: ${jobPoolPids}`);
  99. }, 5000)
  100. }
  101. /**
  102. * 上传到smms
  103. */
  104. async uploadFileToSMMS(tmpFile) {
  105. const res = {
  106. code: 1000,
  107. message: 'unknown error',
  108. };
  109. try {
  110. const headersObj = {
  111. 'Content-Type': 'multipart/form-data',
  112. 'Authorization': 'aaaaaaaaaaaaa' // 请修改这个token,用你自己的账号token
  113. };
  114. const url = 'https://sm.ms/api/v2/upload';
  115. const response = await this.app.curl(url, {
  116. method: 'POST',
  117. headers: headersObj,
  118. files: {
  119. smfile: tmpFile,
  120. },
  121. dataType: 'json',
  122. timeout: 15000,
  123. });
  124. const result = response.data;
  125. if (this.app.config.env === 'local') {
  126. Log.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  127. }
  128. if (result.code !== 'success') {
  129. Log.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  130. }
  131. return result;
  132. } catch (e) {
  133. Log.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  134. }
  135. return res;
  136. }
  137. }
  138. ExampleService.toString = () => '[class ExampleService]';
  139. module.exports = ExampleService;