example.js 3.6 KB

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