This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
2,283 additions
and
726 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict'; | ||
|
||
const async = require('async'); | ||
const program = require('commander'); | ||
const Config = require('../lib/config'); | ||
const StorageEventsCron = require('../lib/cron/storage-events'); | ||
|
||
program.version(require('../package').version); | ||
program.option('-c, --config <path_to_config_file>', 'path to the config file'); | ||
program.option('-d, --datadir <path_to_datadir>', 'path to the data directory'); | ||
program.parse(process.argv); | ||
|
||
var config = new Config(process.env.NODE_ENV || 'develop', program.config, program.datadir); | ||
|
||
var jobs = [ | ||
new StorageEventsCron(config) | ||
]; | ||
|
||
async.eachSeries(jobs, function(job, next) { | ||
job.start(next); | ||
}, function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const CronJob = require('cron').CronJob; | ||
const Config = require('../config'); | ||
const Storage = require('storj-service-storage-models'); | ||
const log = require('../logger'); | ||
|
||
function StorageEventsCron(config) { | ||
if (!(this instanceof StorageEventsCron)) { | ||
return new StorageEventsCron(config); | ||
} | ||
|
||
assert(config instanceof Config, 'Invalid config supplied'); | ||
|
||
this._config = config; | ||
} | ||
|
||
StorageEventsCron.CRON_TIME = '* */10 * * * *'; // every ten minutes | ||
StorageEventsCron.MAX_RUN_TIME = 600000; // 10 minutes | ||
StorageEventsCron.FINALITY_TIME = 10800000; // 3 hours | ||
|
||
StorageEventsCron.prototype.start = function(callback) { | ||
log.info('starting the storage events cron'); | ||
|
||
this.storage = new Storage( | ||
this._config.storage.mongoUrl, | ||
this._config.storage.mongoOpts, | ||
{ logger: log } | ||
); | ||
|
||
this.job = new CronJob({ | ||
cronTime: StorageEventsCron.CRON_TIME, | ||
onTick: this.run.bind(this), | ||
start: false, | ||
timeZone: 'UTC' | ||
}); | ||
|
||
this.job.start(); | ||
|
||
setImmediate(callback); | ||
}; | ||
|
||
StorageEventsCron.prototype._resolveCodes = function(event, user) { | ||
const threshold = this._config.application.unknownReportThreshold; | ||
|
||
let success = event.success; | ||
let successModified = false; | ||
let unknown = success ? false : true; | ||
if (unknown) { | ||
resolveCodes(); | ||
} | ||
|
||
function resolveCodes() { | ||
const failureCode = 1100; | ||
|
||
const clientCode = event.clientReport ? | ||
event.clientReport.exchangeResultCode : undefined; | ||
const farmerCode = event.farmerReport ? | ||
event.farmerReport.exchangeResultCode : undefined; | ||
|
||
/* jshint eqeqeq: false */ | ||
if (farmerCode == failureCode && !clientCode) { | ||
success = false; | ||
unknown = false; | ||
return; | ||
} | ||
|
||
if (farmerCode == failureCode && clientCode == failureCode) { | ||
success = false; | ||
unknown = false; | ||
return; | ||
} | ||
|
||
if (!farmerCode && clientCode == failureCode) { | ||
success = false; | ||
unknown = false; | ||
return; | ||
} | ||
|
||
if (user.exceedsUnknownReportsThreshold(threshold)) { | ||
successModified = true; | ||
success = true; | ||
unknown = true; | ||
return; | ||
} | ||
} | ||
|
||
return { | ||
success: success, | ||
successModified: successModified, | ||
unknown: unknown | ||
}; | ||
|
||
}; | ||
|
||
StorageEventsCron.prototype._resolveEvent = function(event, callback) { | ||
|
||
this.storage.models.User.findOne({_id: event.user}, (err, user) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
const {success, successModified, unknown} = this._resolveCodes(event); | ||
|
||
if (successModified) { | ||
// TODO also give reputation points to farmer for successful | ||
// transfer for the storage event | ||
event.success = success; | ||
event.save((err) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
finalize(); | ||
}); | ||
} else { | ||
finalize(); | ||
} | ||
|
||
function finalize() { | ||
user.updateUnknownReports(unknown, event.timestamp, (err) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
callback(null, event.timestamp); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
StorageEventsCron.prototype._run = function(lastTimestamp, callback) { | ||
|
||
const StorageEvent = this.storage.models.StorageEvent; | ||
const finalityTime = Date.now() - StorageEventsCron.FINALITY_TIME; | ||
|
||
const cursor = StorageEvent.find({ | ||
timestamp: { | ||
$lt: finalityTime, | ||
$gt: lastTimestamp | ||
}, | ||
user: { | ||
$exists: true | ||
} | ||
}).sort({timestamp: 1}).cursor(); | ||
|
||
const timeout = setTimeout(() => { | ||
finish(new Error('Job exceeded max duration')); | ||
}, StorageEventsCron.MAX_RUN_TIME); | ||
|
||
let callbackCalled = false; | ||
|
||
function finish(err) { | ||
clearTimeout(timeout); | ||
cursor.close(); | ||
if (!callbackCalled) { | ||
callbackCalled = true; | ||
callback(err, lastTimestamp); | ||
} | ||
} | ||
|
||
cursor.on('error', finish); | ||
|
||
cursor.on('data', (event) => { | ||
cursor.pause(); | ||
this._resolveEvent(event, (err, _lastTimestamp) => { | ||
if (err) { | ||
return finish(err); | ||
} | ||
lastTimestamp = _lastTimestamp; | ||
cursor.resume(); | ||
}); | ||
}); | ||
|
||
cursor.on('end', finish); | ||
}; | ||
|
||
StorageEventsCron.prototype.run = function() { | ||
const name = 'StorageEventsFinalityCron'; | ||
const Cron = this.storage.models.CronJob; | ||
Cron.lock(name, StorageEventsCron.MAX_RUN_TIME, (err, locked, res) => { | ||
if (err) { | ||
return log.error('%s lock failed, reason: %s', name, err.message); | ||
} | ||
if (!locked) { | ||
return log.warn('%s already running', name); | ||
} | ||
|
||
log.info('Starting %s cron job', name); | ||
|
||
let lastTimestamp = new Date(0); | ||
if (res && | ||
res.value && | ||
res.value.rawData && | ||
res.value.rawData.lastTimestamp) { | ||
lastTimestamp = new Date(res.value.rawData.lastTimestamp); | ||
} else { | ||
log.warn('%s cron has unknown lastTimestamp', name); | ||
} | ||
|
||
this._run(lastTimestamp, (err, _lastTimestamp) => { | ||
if (err) { | ||
let message = err.message ? err.message : 'unknown'; | ||
log.error('Error running %s, reason: %s', name, message); | ||
} | ||
|
||
log.info('Stopping %s cron', name); | ||
const rawData = {}; | ||
if (_lastTimestamp) { | ||
rawData.lastTimestamp = _lastTimestamp.getTime(); | ||
} | ||
|
||
Cron.unlock(name, rawData, (err) => { | ||
if (err) { | ||
return log.error('%s unlock failed, reason: %s', name, err.message); | ||
} | ||
}); | ||
|
||
}); | ||
}); | ||
}; | ||
|
||
module.exports = StorageEventsCron; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.