Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use queue for conversion

  • Loading branch information...
commit ab54f07fe6103ee4e3191d8dd1d43f706c4f4710 1 parent b78bf4f
@kcivey kcivey authored
Showing with 25 additions and 6 deletions.
  1. +2 −1  package.json
  2. +23 −5 server.js
View
3  package.json
@@ -20,6 +20,7 @@
"nconf": "~0.6.7",
"request": "~2.27.0",
"temp": "~0.6.0",
- "underscore": "~1.5.2"
+ "underscore": "~1.5.2",
+ "async": "~0.2.9"
}
}
View
28 server.js
@@ -4,18 +4,23 @@ var fs = require('fs'),
request = require('request'),
_ = require('underscore'),
temp = require('temp'),
+ async = require('async'),
config = require('./config'),
app = express(),
basePath = config.basePath,
convertArguments = config.convertCommand.split(/\s+/),
convertCommand = convertArguments.shift(),
- port = config.port;
+ port = config.port,
+ concurrency = config.concurrency || 1,
+ convertQueue;
if (config.proxy) {
console.log('using proxy', config.proxy);
request = request.defaults({proxy: config.proxy});
}
+convertQueue = async.queue(doConversion, concurrency);
+
app.use(express.logger());
app.get(/^(\/.+)\.([^.\/]+)(\.jpe?g)$/i, function (req, res) {
var convertOptions = getConvertOptions(req.params[1]),
@@ -46,13 +51,15 @@ app.get(/^(\/.+)\.([^.\/]+)(\.jpe?g)$/i, function (req, res) {
cleanup();
})
.on('finish', function () {
- var args = convertArguments.concat(rawFile, convertOptions, convertedFile);
+ var task = {
+ rawFile: rawFile,
+ convertOptions: convertOptions,
+ convertedFile: convertedFile
+ };
times.downloaded = Date.now();
console.log('%s written', rawFile);
- console.log(convertCommand, args.join(' '));
- execFile(convertCommand, args, function (err, stdout, stderr) {
+ convertQueue.push(task, function (err) {
if (err) {
- console.log('convert error', err, stderr);
res.send(500);
cleanup();
return;
@@ -131,6 +138,17 @@ function getTempFilename(options) {
return temp.path(options);
}
+function doConversion(task, callback) {
+ var args = convertArguments.concat(task.rawFile, task.convertOptions, task.convertedFile);
+ console.log(convertCommand, args.join(' '));
+ execFile(convertCommand, args, function (err, stdout, stderr) {
+ if (err) {
+ console.log('convert error', err, stderr);
+ }
+ callback(err);
+ });
+}
+
app.listen(port);
console.log('listening on port', port);
Please sign in to comment.
Something went wrong with that request. Please try again.