0

使用 koa.js,我想创建一个 API,它运行一个在后台运行很长时间的生成器函数,但立即将令牌发送回用户。

然后,用户可以使用该令牌稍后检索其作业的状态。

'use strict';
var generateToken = function(){
    //...
};

var processData = function *(data, token) {
    //...
    var a = yield analysis(data);
    console.log(a) // a is undefined
};

app.post('/process_data', validate, function *(next) {
    var token = generateToken();
    var that = this;
    setTimeout(function() {
        for (var i of processData(that.request.body, token)){
            continue;
        }
    });
    this.body = "this should return immediately " + token;
    return next;
});

在 setTimeout 内运行它,不会保存变量“a”。我如何构造它以使 processData 像正常产量一样运行?

4

2 回答 2

3

您可能希望由诸如Kue之类的作业队列处理长时间运行的进程

您将使用 http 帖子将作业排队

然后使用 http get 检查作业

这是我认为您想要做的事情的粗略概述:

var kue       = require('kue'),
    koa       = require('koa'),
    route     = require('koa-router'),
    thunkify  = require('thunkify'),
    parse     = require('co-body'),
    co        = require('co'),
    app       = koa(),
    jobs      = kue.createQueue();

app.use(route(app));

// turn callbacks into thunks for generators
var createJob = thunkify(jobs.create);
var findJob   = thunkify(kue.Job.get);

// Process the job here
jobs.process('longProcess', function(job, done){
    // do work in here
    // call done(err) when completed
    // EDIT: if you want to handle job using generators/yield
    // you could use a library like co
    co(function *(){
        var qs = yield doWork(job.data); 
        done();
      }).error(done);
});

// Queue/Start the Job here
app.post('/jobs', function *(){
  var body = yield parse(this);
  var job = yield createJob('longProcess', body);
  this.body = job.id;
});

// Check Status of job here
app.get('/jobs/:token', function *(){
  var job = yield findJob(this.params.token);
  this.body = job;
  // job.status === 'complete' || ...
});

app.listen(3000); 
于 2015-01-13T22:06:05.610 回答
0

感谢 Bergi 的解决方案。

app.post('/process_data', validate, function *(next) {
    var token = generateToken();

    co(processData(this.request.body, token));

    this.body = "this should return immediately " + token;
    return next;
});
于 2015-01-13T21:07:34.150 回答