index.js 1.87 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
const server = require("./server");
const { app: electronApp } = require('electron');
const Log = require('ee-core/log');
const Conf = require('ee-core/config');
const GetPort = require('ee-core/utils/get-port');

/**
 * java server插件
 * @class
 */
class JavaServerAddon {

  constructor() {
    this.cfg;
    this.javaServer;
  }

  /**
   * 创建java服务
   *
   * @function 
   * @since 1.0.0
   */
  async createServer () {

    this.cfg = Conf.getValue('addons.javaServer');
    await this.createJavaPorts();

    this.javaServer = new server();
    await this.javaServer.create(this.cfg);

    // kill
    electronApp.on("before-quit", async () => {
      Log.info("[addon:javaServer] before-quit: kill-----------");
      await this.javaServer.kill();
    });

    return;
  }

  /**
   * todo 检查服务是否启动
   *
   * @function 
   * @since 1.0.0
   */
  async check () {
wuhao's avatar
wuhao committed
48 49 50 51 52
    Log.info("进入-----检查服务是否启动------"+this.javaServer);
    if(this.javaServer == undefined){
      Log.info("[addon:javaServer:check] status-----------"+false);
      return false;
    }
wuhao's avatar
wuhao committed
53
    
wuhao's avatar
wuhao committed
54 55 56 57
    const flag = await this.javaServer.isRun(Conf.getValue('addons.javaServer'));
    Log.info("[addon:javaServer:check] status-----------"+flag);

    return flag;    
wuhao's avatar
wuhao committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  }

  /**
   * 创建服务端口
   *
   * @function 
   * @since 1.0.0
   */
  async createJavaPorts() {
    if (!this.cfg.enable) {
      return;
    }
    const javaPort = await GetPort({ port: this.cfg.port });
    process.env.EE_JAVA_PORT = javaPort;
    this.cfg.port = javaPort;

    // 更新config配置
    Conf.setValue('addons.javaServer', this.cfg);
  }

  /**
   * 杀掉进程
   *
   * @function 
   * @since 1.0.0
   */
  async kill() {
    if (!this.cfg.enable) {
      return;
    }
    await this.javaServer.kill();
  }
}

JavaServerAddon.toString = () => '[class JavaServerAddon]';
module.exports = JavaServerAddon;