example.js 3.9 KB

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