A
A
Alexander Knyazev2016-03-28 18:56:16
JavaScript
Alexander Knyazev, 2016-03-28 18:56:16

Working with queues with npm - the kue module. Did I understand the meaning correctly?

I am building a web application using Express. Using the phantomjs module, I take screenshots of the site (in the child script 'phantom-script.js'), then from these images, by calling the generate_video function, I create a video.

app.get('/', function (req, res) {
  	var url = req.query.presentation;
  	var reg_url_base = new RegExp("http/");
  	//name of folder where will save screensots of site
  var name_folder = url.replace(reg_url_base, '');
  	var type = req.query.format;
  	var childArgs = [
  		path.join(__dirname, 'phantom-script.js'),
  		duration,
  		url,
  		FPS,
  		name_folder
  ]
  	childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
    generate_video(res,type,name_folder);
  })
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

video generation function
function generate_video(res,type,name_folder){
  ////код создания видео, неважно
  res.json('Файл создавался ' + function_time + 'ms' + ', ' + 'местоположение: video_output/' + name_folder+'.' + type);
}

It is necessary to make sure that the server does not create more at the same time, for example, 5 videos. To do this, I want to use the Kue
module . I understood this: create a job and a task for this job
app.get('/', function (req, res) {
  	/*............*/
  	childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
                var job_video_creationg = queue.create('video_creating', {
        title: 'creating_video',
        }).save( function(err){
      if(err) console.log( 'error in creating of job with id' + job_video_creationg.id );
    });
               queue.process('video_creating', 200, function(job_video_creationg, done){
                      generate_video(res,type,name_folder, done);
                });
  })
});

The done parameter was passed to the generate_video function, we execute it at the end of the function:
function generate_video(res,type,name_folder, done){
  //генерация видео
  res.json('Файл создавался ' + function_time + 'ms' + ', ' + 'местоположение: video_output/' + name_folder+'.' + type);
  done();
}

Did I understand correctly how it works? (apparently not, because after one request and creating a video, the server crashes on the next request)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pomeo, 2016-03-31
@alexandrknyazev13071995

Not right. Why queue.process inside a route? What's the point in that?
If you press F5 200 times while in /, what do you think will happen? Where will all these res.json answer?
There should not be a response inside the job process.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question