缘起

STF 之环境搭建可能的几个坑里提到过

初始化设备时,提示类似 Setup had an error Error: Service had an error: "Error: Not found; no service started."

官方认为有可能是杀毒软件的问题;实测需要手动先启动一个 UI 页面。现在都是手动到后台进行操作的,希望可以把这个添加到业务逻辑里进行处理。

STF 启动时,device 模块启动是由lib/units/device/index.js处理。这里到依赖就包括里 STFService 的处理.dependency(require('./plugins/service')

Agent 和 Service 都来自 STFService.apk,源码可参考这里

目前采用的方式是:在抛出服务异常之前,主动启动异常 IdentityActivity。这样下次执行设备初始化时,Service 就可以正常启动了。 ——不破坏目前的业务逻辑。观察一下效果再说

//before throw error, try to start ui activity first. next time, it will launch normally.
function startIdentityActivity(tag) {
  log.info("try to start Identivity first ", tag)
  return adb.shell(options.serial, util.format('am start -n %s/%s','jp.co.cyberagent.stf', '.IdentityActivity'))
            .timeout(15000)
            .then(function (out){
              log.info("start UI identityActivity for ",options.serial)
            })
}

这个方法这 callService 方法里抛出异常之前进行调用。 STF 中的 adb 命令都是异步操作,所以方法需要返回,调用的时候需要等待返回后再进行抛出 Error。

类似——

startIdentityActivity("user").then(function() {
    throw new Error(util.format(
    'Service had an error: "%s"'
    , line
    ))
}).catch(streamutil.NoSuchLineError, function(){
    return true 
})

就是从接手 STF 才开始学 Nodejs 的,刚刚两个月。上面的调用会抛出异常——

Unhandled rejection Error: Service had an error: "Error: Not found; no service started."
    at /Users/gebitang/projects/project-remote/RemoteDevice/lib/units/device/plugins/service.js:117:25
    at tryCatcher (/Users/gebitang/projects/project-remote/RemoteDevice/node_modules/adbkit/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (/Users/gebitang/projects/project-remote/RemoteDevice/node_modules/adbkit/node_modules/bluebird/js/main/promise.js:503:31)
    at Promise._settlePromiseAt (/Users/gebitang/projects/project-remote/RemoteDevice/node_modules/adbkit/node_modules/bluebird/js/main/promise.js:577:18)
    at Promise._settlePromises (/Users/gebitang/projects/project-remote/RemoteDevice/node_modules/adbkit/node_modules/bluebird/js/main/promise.js:693:14)
    at Async._drainQueue (/Users/gebitang/projects/project-remote/RemoteDevice/node_modules/adbkit/node_modules/bluebird/js/main/async.js:123:16)
    at Async._drainQueues (/Users/gebitang/projects/project-remote/RemoteDevice/node_modules/adbkit/node_modules/bluebird/js/main/async.js:133:10)
    at Immediate.Async.drainQueues (/Users/gebitang/projects/project-remote/RemoteDevice/node_modules/adbkit/node_modules/bluebird/js/main/async.js:15:14)
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
    at processImmediate [as _immediateCallback] (timers.js:745:5

目前还不知道如何处理:(
欢迎留言指教:)


↙↙↙阅读原文可查看相关链接,并与作者交流