哆啦好梦 2 years ago
parent
commit
5516155940

+ 15 - 0
electron/controller/example.js

@@ -678,6 +678,21 @@ class ExampleController extends Controller {
   }
 
   /**
+   * 任务
+   */ 
+  someJob (args) {
+    let jobId = args.id;
+    if (args.type == 'timer') {
+      let myjob = new ChildJob();
+      myjob.exec('./jobs/example/timer', {jobId});
+  
+      myjob.on('job-timer', (data) => {
+        Log.info('from TimerJob data:', data);
+      })
+    }
+  }
+
+  /**
    * 测试接口
    */ 
   hello (args) {

+ 5 - 0
electron/jobs/example/hello.js

@@ -0,0 +1,5 @@
+const Log = require('ee-core/module/log');
+
+exports.welcome = function () {
+  Log.info('[child-process] [jobs/example/hello] welcome ! ');
+}

+ 0 - 41
electron/jobs/example/index.js

@@ -1,41 +0,0 @@
-const Job = require('ee-core/module/jobs/baseJobClass');
-const Loader = require('ee-core/module/loader');
-const Log = require('ee-core/module/log');
-const Ps = require('ee-core/module/ps');
-const test = Loader.requireJobsModule('./example/test');
-
-/**
- * 示例服务
- * @class
- */
-class ExampleJob extends Job {
-
-  constructor(params) {
-    super();
-    this.params = params;
-  }
-
-  /**
-   * Execute the job
-   */
-  async handle () {
-    Log.info("[child-process] job params: ", this.params);
-    // setInterval(function() {
-    //   console.log('ddddd')
-    // }, 1000);
-
-    //test.hello();
-    //test.utilsMod();
-
-    setTimeout(function(){
-      test.hello();
-    }, 3000)
-
-    setTimeout(function(){
-      Ps.exit(1);
-    }, 10000)
-  }   
-}
-
-ExampleJob.toString = () => '[class ExampleJob]';
-module.exports = ExampleJob;

+ 0 - 40
electron/jobs/example/test.js

@@ -1,40 +0,0 @@
-const Log = require('ee-core/module/log');
-const Utils = require('ee-core/module/utils');
-const Ps = require('ee-core/module/ps');
-
-exports.hello = function () {
-  Log.info('[child-process] [jobs/test] hello -------- ');
-}
-
-exports.utilsMod = function () {
-  let utilsApis = {
-    getBaseDir: Ps.getBaseDir(),
-    getEnv: Ps.getEnv(),
-    isDev: Ps.isDev(),
-    isRenderer: Ps.isRenderer(),
-    isMain: Ps.isMain(),
-    isForkedChild: Ps.isForkedChild(),
-    getHomeDir: Ps.getHomeDir(),
-    getStorageDir: Ps.getStorageDir(),
-    getLogDir: Ps.getLogDir(),
-    getRootDir: Ps.getRootDir(),
-    getBaseDir: Ps.getBaseDir(),
-    getAppUserDataDir: Ps.getAppUserDataDir(),
-    getHomeDir: Ps.getHomeDir(),
-    getUserHomeDir: Ps.getUserHomeDir(),
-    getMainPort: Ps.getMainPort(),
-    getSocketPort: Ps.getSocketPort(),
-    getHttpPort: Ps.getHttpPort(),
-    getExecDir: Ps.getExecDir(),
-    getPackage: Utils.getPackage(),
-    getEeConfig: Utils.getEeConfig(),
-    getAppVersion: Utils.getAppVersion(),
-    getAddonConfig: Utils.getAddonConfig(),
-    getMainServerConfig: Utils.getMainServerConfig(),
-    getHttpServerConfig: Utils.getHttpServerConfig(),
-    getSocketServerConfig: Utils.getSocketServerConfig(),
-    getSocketChannel: Utils.getSocketChannel(),
-    getExtraResourcesDir: Ps.getExtraResourcesDir(),
-  }
-  Log.info('[child-process] [jobs/test] Utils -------- ', utilsApis);
-}

+ 46 - 0
electron/jobs/example/timer.js

@@ -0,0 +1,46 @@
+const Job = require('ee-core/module/jobs/baseJobClass');
+const Loader = require('ee-core/module/loader');
+const Log = require('ee-core/module/log');
+const Ps = require('ee-core/module/ps');
+const Message = require('ee-core/module/message');
+const Hello = Loader.requireJobsModule('./example/hello');
+
+/**
+ * example - TimerJob
+ * @class
+ */
+class TimerJob extends Job {
+
+  constructor(params) {
+    super();
+    this.params = params;
+  }
+
+  /**
+   * handle()方法是必要的,且会被自动调用
+   */
+  async handle () {
+    Log.info("[child-process] TimerJob params: ", this.params);
+
+    // 模拟计时器任务,执行10秒
+    let childMessage = Message.childMessage;
+    let eventName = 'job-timer';
+    let number = 0;
+    let jobId = this.params.jobId;
+    setInterval(function() {
+      Log.info("[child-process] TimerJob number: ", number);
+      Hello.welcome();
+
+      childMessage.sendToMain(eventName, {number, jobId});
+      number++;
+    }, 1000);
+
+    // 任务完成后,必须调用 Ps.exit() 方法,让进程退出,否则会常驻内存
+    setTimeout(function(){
+      Ps.exit(1);
+    }, 15 * 1000)
+  }   
+}
+
+TimerJob.toString = () => '[class TimerJob]';
+module.exports = TimerJob;

+ 2 - 2
electron/preload/index.js

@@ -21,6 +21,6 @@ module.exports = async (app) => {
   awakenAddon.create();
   autoUpdaterAddon.create();
 
-  let myjob = new ChildJob();
-  myjob.exec('./jobs/example/index');
+  // let myjob = new ChildJob();
+  // myjob.exec('./jobs/example/index');
 }

+ 1 - 0
frontend/src/api/main.js

@@ -34,6 +34,7 @@ const ipcApiRoute = {
   getWCid: 'controller.example.getWCid',
   startJavaServer: 'controller.example.startJavaServer',
   closeJavaServer: 'controller.example.closeJavaServer',
+  someJob: 'controller.example.someJob',
   hello: 'controller.example.hello',
 }
 

+ 3 - 0
frontend/src/utils/message.js

@@ -0,0 +1,3 @@
+const { ipcRenderer: ipc } = (window.require && window.require('electron')) || window.electron || {}
+
+export default ipc

+ 70 - 0
frontend/src/views/base/jobs/Index.vue

@@ -0,0 +1,70 @@
+<template>
+  <div id="app-base-jobs">
+    <div class="one-block-1">
+      <span>
+        1. 任务
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-space>
+        <a-button @click="runJob(1)">执行任务1</a-button>
+        进度:{{ message1 }}
+      </a-space>
+      <p></p>
+      <a-space>
+        <a-button @click="runJob(2)">执行任务2</a-button>
+        进度:{{ message2 }}
+      </a-space>            
+    </div>        
+  </div>
+</template>
+<script>
+import { ipcApiRoute } from '@/api/main'
+export default {
+  data() {
+    return {
+      progress1: 0,
+      progress2: 0,
+    }
+  },
+  mounted () {
+    this.init();
+  },
+  methods: {
+    init () {
+      // 避免重复监听,或者将 on 功能写到一个统一的地方,只加载一次
+      this.$ipc.removeAllListeners(ipcApiRoute.someJob);
+      this.$ipc.on(ipcApiRoute.someJob, (event, result) => {
+        console.log('[ipcRenderer] [someJob] result:', result);
+        switch (result.jId) {
+          case 1:
+            this.progress1 = result.progress1;
+          case 2:
+            this.progress2 = result.progress2;            
+        }
+      })
+    },
+    runJob(jobId) {
+      let params = {
+        id: jobId,
+        type: 'timer'
+      }
+      this.$ipc.send(ipcApiRoute.someJob, params)
+    },
+  }
+}
+</script>
+<style lang="less" scoped>
+#app-base-jobs {
+  padding: 0px 10px;
+  text-align: left;
+  width: 100%;
+  .one-block-1 {
+    font-size: 16px;
+    padding-top: 10px;
+  }
+  .one-block-2 {
+    padding-top: 10px;
+  }
+}
+</style>

+ 1 - 1
package.json

@@ -111,7 +111,7 @@
   "dependencies": {
     "better-sqlite3": "^7.6.0",
     "dayjs": "^1.10.7",
-    "ee-core": "^1.5.0",
+    "ee-core": "^2.0.0",
     "electron-is": "^3.0.0",
     "electron-updater": "^5.3.0",
     "lodash": "^4.17.21"