example.js 3.6 KB

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