/*
** Module dependencies
*/
var es = require('event-stream');
var mongoose = require('mongoose');

var jobs = require('../kue').jobs;

var Service = mongoose.model('Service');
var CswRecord = mongoose.model('CswRecord');
var Record = mongoose.model('Record');


/*
** Actions
*/
exports.list = function (req, res, next) {
    Service
        .find({ protocol: 'csw' })
        .populate('lastSync', 'status started')
        .populate('lastSuccessfulSync', 'started finished itemsFound')
        .exec(function(err, services) {
            if (err) return next(err);
            res.json(services);
        });
};

exports.showRecordHistory = function (req, res, next) {
    var syncSelect = '-__v -service -jobId -log';
    CswRecord
        .find({ identifier: req.params.identifier, parentCatalog: req.service._id })
        .sort('-timestamp')
        .populate('synchronizations', syncSelect)
        .select('-xml')
        .lean()
        .exec(function (err, records) {
            if (err) return next(err);
            res.json(records);
        });
};

exports.downloadRecordSnapshot = function (req, res, next) {
    CswRecord
        .findById(req.params.snapshotId)
        .select('xml')
        .lean()
        .exec(function (err, record) {
            if (err) return next(err);
            res.type('application/xml');
            res.send(record.xml);
        });
};

exports.forceReprocessAll = function (req, res) {
    Record
        .find({ parentCatalog: req.service._id })
        .select({ identifier: 1 })
        .lean()
        .stream()
        .pipe(es.map(function (record, cb) {
            jobs
                .create('process-record', {
                    recordId: record.identifier,
                    catalogId: req.service._id
                })
                .removeOnComplete(true)
                .attempts(5)
                .save(cb);
        }))
        .on('error', function (err) {
            console.trace(err);
        })
        .on('end', function () {
            res.status(200).end();
        });
};

@@ -1428,24 +1428,43 @@
 req, res) %7B%0A
+    var count = 0;%0A
     Record%0A 
@@ -1893,18 +1893,150 @@
   .save(
-cb
+function (err) %7B%0A                    if (err) return cb(err);%0A                    count++;%0A                    cb();%0A                %7D
 );%0A     
@@ -2177,23 +2177,28 @@
 es.s
-tatus(200).end(
+end(%7B count: count %7D
 );%0A 

Add counter to reprocess task