'use strict'
// 子进程模块
const worker = require('ddv-worker')
// 工具类
const util = require('ddv-worker/util')
// 启动时间戳
worker.gwcidTimeStamp = parseInt(worker.startTimeStamp)
// 可以通过重写getProcessInfo方法来改变返回数据
worker.getProcessInfo = function (callback) {
  return util.isFunction(callback) && callback({
    pid: process.pid,
    status: worker.status || 'unknown',
    debug: (worker.DEBUG ? 'Enabled' : 'Disabled'),
    lastUptime: (worker.starttime * 1000),
    memoryUsage: process.memoryUsage(),
    socket: worker.socketSum,
    ws: worker.webSocketSum,
    http: worker.httpSum
  })
}
// 更新服务配置参数
worker.updateServerConf = function (serverConf, callback) {
  // 调试模式更新
  worker.DEBUG = util.type(serverConf.debug, 'boolean') || serverConf.debug !== void 0 ? Boolean(serverConf.debug) : worker.DEBUG
  if (worker.isHasMaster) {
  // 发送修改到 master
    worker.callMaster('updateServerConf', serverConf, (state, message) => {
      if (state) {
        if (util.isFunction(callback)) {
          callback(null)
        } else {
          console.log('service config update successful')
        }
      } else {
        if (util.isFunction(callback)) {
          callback(new Error(message))
        } else {
          console.log('service config update fail')
        }
      }
      state = message = void 0
    })
  } else {
    callback(new Error('不支持更新服务器配置'))
  }
}

// 守护进程对主进程的调用事件监听
worker.on('master::call::kill', function getSiteLists (data, handle, callback) {
  callback(true, { pid: process.pid })
  setTimeout(function exitRun () {
    process.exit(0)
  }, 200)
})
/** 管理进程的事件结束 **/
/**
 * [processInfoCb 管理进程查询子进程状态]
 * @author: 桦 <yuchonghua@163.com>
 * @DateTime 2016-11-16T15:02:10+0800
 * @param    {[type]}                 data             [description]
 * @param    {[type]}                 handle           [description]
 * @param    {[type]}                 callback){  data [description]
 * @return   {[type]}                                  [description]
 */
worker.on('master::call::processInfo', function processInfoCb (data, handle, callback) {
  data = handle = void 0
  worker.getProcessInfo((data) => {
    callback && callback(true, data)
  })
})

@@ -6,16 +6,72 @@
 strict'%0A
+const debug = require('debug')('ddv-worker:worker:api')%0A
 // %E5%AD%90%E8%BF%9B%E7%A8%8B%E6%A8%A1%E5%9D%97
@@ -154,16 +154,623 @@
 r/util')
+%0A%0A/**%0A * %5BmasterError %E4%B8%BB%E7%BA%BF%E7%A8%8B%E9%94%99%E8%AF%AF%5D%0A * @author: %E6%A1%A6 %3Cyuchonghua@163.com%3E%0A * @DateTime 2016-11-14T14:39:39+0800%0A * @param    %7Bstring%7D                 message %5Bdescription%5D%0A * @param    %7Bstring%7D                 stack   %5Bdescription%5D%0A * @return   %7Berror%7D                          %5Bdescription%5D%0A*/%0Aworker.WorkerError = class WorkerError extends Error %7B%0A    // %E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%0A  constructor (message, stack) %7B%0A    debug('constructor')%0A      // %E8%B0%83%E7%94%A8%E7%88%B6%E7%B1%BB%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%0A    super(message)%0A    this.name = this.name %7C%7C 'Error'%0A    this.type = this.type %7C%7C 'WorkerError'%0A    this.stack += stack ? ('%5Cn' + stack) : ''%0A    message = stack = void 0%0A  %7D%0A%7D
 %0A// %E5%90%AF%E5%8A%A8%E6%97%B6%E9%97%B4

add worker.WorkerError