在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ egg 如何后臺執(zhí)行某個任務?

egg 如何后臺執(zhí)行某個任務?

小程序屏蔽了阿里云的OSS

所以程序的上傳先傳到應用服務器,返回成功代碼和臨時地址給前臺,然后應用服務器在后臺上傳到阿里云oss,再寫數(shù)據(jù)庫,返回前臺。

try {
          yield awaitWriteStream(stream.pipe(writeStream));
          yield this.ctx.service.updatefile.updateToss(target, upFileName, uid);
          return { code: 0, msg: '', data: tmpPath };
        } catch (err) {
          yield sendToWormhole(stream);
          throw err;
        }

這里是上傳到應用服務器的

我的問題是,

yield this.ctx.service.updatefile.updateToss(target, upFileName, uid);

在updateToss這個function里,有一個update數(shù)據(jù)庫動作,一個上傳文件動作,都是yield。

這任務實際上是希望在后臺執(zhí)行的,所以那個return的問題應該如何處理,其實我是想把這句放到后臺執(zhí)行,不影響前臺的。

嘗試了on監(jiān)視,但是不支持yield

try {
          yield awaitWriteStream(stream.pipe(writeStream));
          console.log(writeStream);
          writeStream.once('closed', () => {
            if (writeStream.finished === true) {
              yield this.ctx.app.service.updatefile.updateToss(target, upFileName, uid);
            }
          });
          writeStream.emit('closed');
          if (writeStream.closed) {
            return { code: 0, msg: '', data: tmpPath, oss: { target, upFileName, uid } };
          }
        } catch (err) {
          yield sendToWormhole(stream);
          throw err;
        }
回答
編輯回答
孤酒

https://github.com/eggjs/egg/...
框架提供的runInBackground做這個是不是很合適?

2017年10月13日 21:05
編輯回答
伐木累

關于上傳可以使用child_process開個子進程去做。
然后還可以監(jiān)聽子進程執(zhí)行的狀態(tài)來決定是否更改數(shù)據(jù)庫的操作。

2017年10月15日 22:47