Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gyteng committed Jan 18, 2019
1 parent 37c406a commit ec82d37
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 19 deletions.
14 changes: 9 additions & 5 deletions init/cron.js
Expand Up @@ -10,9 +10,10 @@ const minute = function(fn, name, time = 1) {
// const fnWithRedis = async () => {
// const run = await redis.setnx(`Cron:${ name }`, 1);
// if(run) {
// redis.expire(`Cron:${ name }`, time * 60 - 1);
// logger.info(`cron: ${ name }, [${ time }]`);
// redis.expire(`Cron:${ name }`, time * 60);
// logger.info(`[${ cluster.worker.id }] cron: ${ name }, [${ time * 60 }]`);
// await fn();
// await redis.del(`Cron:${ name }`);
// }
// };
const fnWithRedis = () => { if(isMainWorker()) { logger.info(`[${ cluster.worker.id }]cron: ${ name }, [${ time }]`); fn(); }; };
Expand All @@ -24,8 +25,9 @@ const second = function(fn, name, time = 10) {
// const run = await redis.setnx(`Cron:${ name }`, 1);
// if(run) {
// redis.expire(`Cron:${ name }`, time - 1);
// logger.info(`cron: ${ name }, [${ time }]`);
// logger.info(`[${ cluster.worker.id }] cron: ${ name }, [${ time }]`);
// await fn();
// await redis.del(`Cron:${ name }`);
// }
// };
const fnWithRedis = () => { if(isMainWorker()) { logger.info(`[${ cluster.worker.id }]cron: ${ name }, [${ time }]`); fn(); }; };
Expand All @@ -37,8 +39,9 @@ const cron = function(fn, name, cronString, time) {
// const run = await redis.setnx(`Cron:${ name }`, 1);
// if(run) {
// redis.expire(`Cron:${ name }`, time - 1);
// logger.info(`cron: ${ name }, [${ time }]`);
// logger.info(`[${ cluster.worker.id }] cron: ${ name }, [${ time }]`);
// await fn();
// await redis.del(`Cron:${ name }`);
// }
// };
const fnWithRedis = () => { if(isMainWorker()) { logger.info(`[${ cluster.worker.id }]cron: ${ name }, [${ time }]`); fn(); }; };
Expand All @@ -51,7 +54,7 @@ const loop = function(fn, name, time = 300, multiCore = false) {
// if(run) {
// await redis.expire(`Cron:${ name }`, time);
// try {
// logger.info(`cron: ${ name }, [${ time }]`);
// logger.info(`[${ cluster.worker.id }] cron: ${ name }, [${ time }]`);
// await fn();
// await redis.del(`Cron:${ name }`);
// await fnWithRedis();
Expand All @@ -71,6 +74,7 @@ const loop = function(fn, name, time = 300, multiCore = false) {
// }
// };
// fnWithRedis();

(async () => {
if(isMainWorker()) {
while(true) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "shadowsocks-manager",
"version": "0.30.3",
"version": "0.30.4",
"description": "A shadowsocks manager tool for multi user and traffic control.",
"main": "server.js",
"scripts": {
Expand Down
92 changes: 82 additions & 10 deletions plugins/account_checker/index.js
Expand Up @@ -118,17 +118,24 @@ const isBaned = async (server, account) => {

const isOverFlow = async (server, account) => {
let realFlow = 0;
const writeFlow = async (serverId, accountId, flow, time) => {
const writeFlow = async (serverId, accountId, flow) => {
const exists = await knex('account_flow').where({ serverId, accountId }).then(s => s[0]);
if(exists) {
await knex('account_flow').update({
flow,
checkTime: Date.now(),
// nextCheckTime: Date.now() + Math.ceil(time),
checkFlowTime: Date.now(),
}).where({ id: exists.id });
}
};
const writeFlowForOtherServer = async (serverId, accountId, flow) => {
await knex('account_flow').update({
flow,
checkFlowTime: Date.now(),
}).where({
serverId
}).whereNotIn('accountId', [ accountId ]);
};
if(account.type >= 2 && account.type <= 5) {
let timePeriod = 0;
if(account.type === 2) { timePeriod = 7 * 86400 * 1000; }
Expand Down Expand Up @@ -180,15 +187,17 @@ const isOverFlow = async (server, account) => {
return { flow: a.flow + b.flow };
}, { flow: data.flow }).flow;

let nextCheckTime = (flowWithFlowPacks - sumFlow) / 60000000 * 60 * 1000 / server.scale;
if(nextCheckTime >= account.expireTime - Date.now() && account.expireTime - Date.now() > 0) { nextCheckTime = account.expireTime - Date.now(); }
if(nextCheckTime <= 0) { nextCheckTime = 600 * 1000; }
if(nextCheckTime >= 12 * 60 * 60 * 1000) { nextCheckTime = 12 * 60 * 60 * 1000; }
await writeFlow(server.id, account.id, realFlow, nextCheckTime);

// let nextCheckTime = (flowWithFlowPacks - sumFlow) / 60000000 * 60 * 1000 / server.scale;
// if(nextCheckTime >= account.expireTime - Date.now() && account.expireTime - Date.now() > 0) { nextCheckTime = account.expireTime - Date.now(); }
// if(nextCheckTime <= 0) { nextCheckTime = 600 * 1000; }
// if(nextCheckTime >= 12 * 60 * 60 * 1000) { nextCheckTime = 12 * 60 * 60 * 1000; }
await writeFlow(server.id, account.id, realFlow);
if(account.multiServerFlow && sumFlow < flowWithFlowPacks) {
await writeFlowForOtherServer(server.id, account.id, realFlow);
}
return sumFlow >= flowWithFlowPacks;
} else {
await writeFlow(server.id, account.id, 0, 30 * 60 * 1000 + Number(Math.random().toString().substr(2, 7)));
await writeFlow(server.id, account.id, 0);
return false;
}
};
Expand Down Expand Up @@ -379,8 +388,70 @@ cron.loop(
360,
);

cron.minute(async () => {
await knex('account_flow').delete()
.where('nextCheckTime', '<', Date.now() - 3 * 60 * 60 * 1000)
.orderBy('nextCheckTime', 'asc');
}, 'DeleteInvalidAccountFlow', 30);

(async () => {
while(true) {
const serverNumber = await knex('server').select(['id']).then(s => s.length);
const accountNumber = await knex('account_plugin').select(['id']).then(s => s.length);
if(serverNumber * accountNumber > 300) {
while(true) {
const accountLeft = await redis.lpop('CheckAccountQueue');
const accountRight = await redis.rpop('CheckAccountQueue');
const queueLength = await redis.llen('CheckAccountQueue');
if(!accountLeft || queueLength < 10) {
let accounts = [];
try {
const datas = await knex('account_flow').select()
.where('nextCheckTime', '<', Date.now())
.orderBy('nextCheckTime', 'desc')
.limit(50)
.offset(0);
accounts = [...accounts, ...datas];
} catch(err) {
logger.error(err);
}
try {
const datas = await knex('account_flow').select()
.where('updateTime', '>', Date.now() - 8 * 60 * 1000)
.where('checkFlowTime', '<', Date.now() - 10 * 60 * 1000)
.whereNotIn('id', accounts.map(account => account.id))
.orderBy('updateTime', 'desc')
.limit(50)
.offset(0);
accounts = [...accounts, ...datas];
} catch(err) { logger.error(err); }
try {
datas = await knex('account_flow').select()
.whereNotIn('id', accounts.map(account => account.id))
.orderByRaw('rand()').limit(accounts.length < 30 ? 35 - accounts.length : 5);
accounts = [...accounts, ...datas];
} catch(err) { }
try {
datas = await knex('account_flow').select()
.whereNotIn('id', accounts.map(account => account.id))
.orderByRaw('random()').limit(accounts.length < 30 ? 35 - accounts.length : 5);
accounts = [...accounts, ...datas];
} catch(err) { }
logger.info(`Add [${ accounts.length }] elements to queue`);
await redis.lpush('CheckAccountQueue', accounts.map(m => `${ m.serverId }:${ m.accountId }`));
await sleep(5000);
}
if(accountLeft) {
const serverId = +accountLeft.split(':')[0];
const accountId = +accountLeft.split(':')[1];
await checkAccount(serverId, accountId).catch();
}
if(accountRight) {
const serverId = +accountRight.split(':')[0];
const accountId = +accountRight.split(':')[1];
await checkAccount(serverId, accountId).catch();
}
}
} else {
await sleep(randomInt(2000));
const start = Date.now();
let accounts = [];
Expand Down Expand Up @@ -457,3 +528,4 @@ cron.loop(
}
}
})();

3 changes: 1 addition & 2 deletions plugins/webgui/public/controllers/adminAccount.js
Expand Up @@ -147,8 +147,7 @@ app.controller('AdminAccountController', ['$scope', '$state', '$mdMedia', '$http
}
$scope.getServerPortData($scope.servers[0], $scope.accountId);
$scope.isMultiServerFlow = !!$scope.account.multiServerFlow;
}).catch(err => {
console.log(err);
}).catch(() => {
$state.go('admin.account');
});
let currentServerId;
Expand Down

0 comments on commit ec82d37

Please sign in to comment.