example.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * 上传到smms
  77. */
  78. async uploadFileToSMMS(tmpFile) {
  79. const res = {
  80. code: 1000,
  81. message: 'unknown error',
  82. };
  83. try {
  84. const headersObj = {
  85. 'Content-Type': 'multipart/form-data',
  86. 'Authorization': 'aaaaaaaaaaaaa' // 请修改这个token,用你自己的账号token
  87. };
  88. const url = 'https://sm.ms/api/v2/upload';
  89. const response = await this.app.curl(url, {
  90. method: 'POST',
  91. headers: headersObj,
  92. files: {
  93. smfile: tmpFile,
  94. },
  95. dataType: 'json',
  96. timeout: 15000,
  97. });
  98. const result = response.data;
  99. if (this.app.config.env === 'local') {
  100. Log.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  101. }
  102. if (result.code !== 'success') {
  103. Log.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  104. }
  105. return result;
  106. } catch (e) {
  107. Log.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  108. }
  109. return res;
  110. }
  111. }
  112. ExampleService.toString = () => '[class ExampleService]';
  113. module.exports = ExampleService;