Index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div id="app-base-jobs">
  3. <div class="one-block-1">
  4. <span>
  5. 1. 任务 / 并发任务
  6. </span>
  7. </div>
  8. <div class="one-block-2">
  9. <a-space>
  10. <a-button @click="runJob(1)">执行任务1</a-button>
  11. 进度:{{ progress1 }} , 进程pid:{{ progress1_pid }}
  12. <a-button @click="closeJob(1)">关闭任务1</a-button>
  13. </a-space>
  14. <p></p>
  15. <a-space>
  16. <a-button @click="runJob(2)">执行任务2</a-button>
  17. 进度:{{ progress2 }} , 进程pid:{{ progress2_pid }}
  18. <a-button @click="closeJob(2)">关闭任务2</a-button>
  19. </a-space>
  20. </div>
  21. <div class="one-block-1">
  22. <span>
  23. 2. 任务池 / 并发任务
  24. </span>
  25. </div>
  26. <div class="one-block-2">
  27. <a-space>
  28. <a-button @click="createPool()">创建进程池</a-button>
  29. 进程pids:{{ processPids }}
  30. </a-space>
  31. <p></p>
  32. <a-space>
  33. <a-button @click="runJobByPool(3)">执行任务3</a-button>
  34. 进度:{{ progress3 }} , 进程pid:{{ progress3_pid }}
  35. <a-button @click="closeJob(3)">关闭任务3</a-button>
  36. </a-space>
  37. <p></p>
  38. <a-space>
  39. <a-button @click="runJobByPool(4)">执行任务4</a-button>
  40. 进度:{{ progress4 }} , 进程pid:{{ progress4_pid }}
  41. <a-button @click="closeJob(4)">关闭任务4</a-button>
  42. </a-space>
  43. </div>
  44. </div>
  45. </template>
  46. <script>
  47. import { ipcApiRoute } from '@/api/main'
  48. export default {
  49. data() {
  50. return {
  51. progress1: 0,
  52. progress1_pid: 0,
  53. progress2: 0,
  54. progress2_pid: 0,
  55. progress3: 0,
  56. progress3_pid: 0,
  57. progress4: 0,
  58. progress4_pid: 0,
  59. processPids: '',
  60. }
  61. },
  62. mounted () {
  63. this.init();
  64. },
  65. methods: {
  66. init () {
  67. // 避免重复监听,或者将 on 功能写到一个统一的地方,只加载一次
  68. this.$ipc.removeAllListeners(ipcApiRoute.timerJobProgress);
  69. this.$ipc.removeAllListeners(ipcApiRoute.createPoolNotice);
  70. // 监听任务进度
  71. this.$ipc.on(ipcApiRoute.timerJobProgress, (event, result) => {
  72. switch (result.jobId) {
  73. case 1:
  74. this.progress1 = result.number;
  75. this.progress1_pid = result.pid == 0 ? result.pid : this.progress1_pid;
  76. break;
  77. case 2:
  78. this.progress2 = result.number;
  79. this.progress2_pid = result.pid == 0 ? result.pid : this.progress2_pid;
  80. break;
  81. case 3:
  82. this.progress3 = result.number;
  83. this.progress3_pid = result.pid == 0 ? result.pid : this.progress3_pid;
  84. break;
  85. case 4:
  86. this.progress4 = result.number;
  87. this.progress4_pid = result.pid == 0 ? result.pid : this.progress4_pid;
  88. break;
  89. }
  90. })
  91. // 监听pool
  92. this.$ipc.on(ipcApiRoute.createPoolNotice, (event, result) => {
  93. let pidsStr = JSON.stringify(result);
  94. this.processPids = pidsStr;
  95. })
  96. },
  97. runJob(jobId) {
  98. let params = {
  99. id: jobId,
  100. type: 'timer'
  101. }
  102. this.$ipc.invoke(ipcApiRoute.someJob, params).then(data => {
  103. switch (data.jobId) {
  104. case 1:
  105. this.progress1_pid = data.pid;
  106. break;
  107. case 2:
  108. this.progress2_pid = data.pid;
  109. break;
  110. }
  111. })
  112. },
  113. closeJob(jobId) {
  114. let params = {
  115. id: jobId,
  116. type: 'timer'
  117. }
  118. this.$ipc.send(ipcApiRoute.closeJob, params);
  119. },
  120. createPool() {
  121. let params = {
  122. number: 3,
  123. }
  124. this.$ipc.send(ipcApiRoute.createPool, params);
  125. },
  126. runJobByPool(jobId) {
  127. let params = {
  128. id: jobId,
  129. type: 'timer'
  130. }
  131. this.$ipc.invoke(ipcApiRoute.someJobByPool, params).then(data => {
  132. switch (data.jobId) {
  133. case 3:
  134. this.progress3_pid = data.pid;
  135. break;
  136. case 4:
  137. this.progress4_pid = data.pid;
  138. break;
  139. }
  140. })
  141. },
  142. }
  143. }
  144. </script>
  145. <style lang="less" scoped>
  146. #app-base-jobs {
  147. padding: 0px 10px;
  148. text-align: left;
  149. width: 100%;
  150. .one-block-1 {
  151. font-size: 16px;
  152. padding-top: 10px;
  153. }
  154. .one-block-2 {
  155. padding-top: 10px;
  156. }
  157. }
  158. </style>