feat(cron): add lib cron-hnr, ready to count h&r warning every midnight.

This commit is contained in:
OldHawk
2017-09-07 14:50:55 +08:00
parent dfc984c0f0
commit 61cba33dde
4 changed files with 81 additions and 1 deletions

View File

@@ -258,6 +258,7 @@ module.exports = {
* @canReDownloadWarningSelf: if true, user can redownload the warning torrent and continue seed until the warning disappears
* @scoreToRemoveWarning: if user has any warning, user can remove one warning by score number, if the user has not enough score, user still can remove these
* warning by donate the VIP class.
* @cronTimeZone: count warning crontab timezone, because need to auto count warning at midnight
*/
hitAndRun: {
condition: {
@@ -268,6 +269,7 @@ module.exports = {
downloadHnR: 2,
downloadAll: 5
},
cronTimeZone: 'Asia/Shanghai',
countWarningAtDownloadComplete: true,
countWarningAtMidnightEveryday: true,
canReDownloadWarningSelf: true,

64
config/lib/cron-hnr.js Normal file
View File

@@ -0,0 +1,64 @@
'use strict';
var path = require('path'),
config = require(path.resolve('./config/config')),
mongoose = require('mongoose'),
chalk = require('chalk'),
CronJob = require('cron').CronJob,
moment = require('moment'),
Torrent = mongoose.model('Torrent'),
User = mongoose.model('User');
var hnrConfig = config.meanTorrentConfig.hitAndRun;
/**
* cron params of time
*
* Seconds: 0-59
* Minutes: 0-59
* Hours: 0-23
* Day of Month: 1-31
* Months: 0-11
* Day of Week: 0-6
*
*
* CronJob
*
* constructor(cronTime, onTick, onComplete, start, timezone, context, runOnInit) - Of note, the first parameter here can be a JSON object that has
* the below names and associated types (see examples above).
* cronTime - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS Date object.
* onTick - [REQUIRED] - The function to fire at the specified time.
* onComplete - [OPTIONAL] - A function that will fire when the job is complete, when it is stopped.
* start - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at
* default you will need to call job.start() in order to start the job (assuming job is the variable you set the cronjob to). This does
* not immediately fire your onTick function, it just gives you more control over the behavior of your jobs.
* timeZone - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is
* invalid, an error is thrown.
* context - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call this.stop().
* However, if you change this you'll have access to the functions and values within your context object.
* runOnInit - [OPTIONAL] - This will immediately fire your onTick function as soon as the requisit initialization has happened. This option is
* set to false by default for backwards compatibility.
* start - Runs your job.
* stop - Stops your job.
*
* @param app
*/
module.exports = function (app) {
var cronJobHnR = new CronJob({
//cronTime: '00 00 1 * * *',
//cronTime: '*/5 * * * * *',
cronTime: '00 00 * * * *',
onTick: function () {
console.log(chalk.green('cronJobHnR: process!'));
},
onComplete: function () {
console.log(chalk.green('cronJobHnR: complete!'));
},
start: false,
timeZone: hnrConfig.cronTimeZone
});
cronJobHnR.start();
return cronJobHnR;
};

View File

@@ -222,10 +222,19 @@ module.exports.configureSocketIO = function (app, db) {
* Initialize IRC announce
*/
module.exports.initIRCAnnounce = function (app) {
var client = require('./ircAnnounce.js')(app);
var client = require('./ircAnnounce')(app);
app.set('ircClient', client);
};
/**
* initCronJobHnR
* @param app
*/
module.exports.initCronJobHnR = function (app) {
var cron = require('./cron-hnr')(app);
app.set('cronJobHnR', cron);
};
/**
* Initialize the Express application
*/
@@ -267,6 +276,9 @@ module.exports.init = function (db) {
if (ircConfig.enable)
this.initIRCAnnounce(app);
// Initialize cron job of hit and run
this.initCronJobHnR(app);
// Configure Socket.io
app = this.configureSocketIO(app, db);

View File

@@ -1,3 +1,5 @@
'use strict';
var path = require('path'),
bencoding = require('bencoding'),
common = require(path.resolve('./config/lib/common')),