molly-file
Version:
molly-file is a free and opensource
64 lines (49 loc) • 2.83 kB
JavaScript
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
const api = require('./api_handler');
const cluster = require('cluster');
const path = require('path');
const net = require('net');
const os = require('os');
const fs = require('fs');
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
function getTime(time){
const today = new Date();
const tomrw = new Date();
tomrw.setHours(0); tomrw.setSeconds(0);
tomrw.setDate( tomrw.getDate() + time );
tomrw.setMinutes(0); tomrw.setMilliseconds(0);
return parseInt(tomrw.getTime()-today.getTime());
}
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
function cacheInterval(arg){
function callback(){
fs.readdirSync( os.tmpdir() ).map(x=>{
if( x.length == 64 ) fs.unlinkSync(
path.join( os.tmpdir(), x )
);
});
}
setInterval( callback, getTime(arg.cache) );
callback();
}
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/
module.exports = (arg)=>{
if( cluster.isPrimary ){
for( const i in os.cpus() ){ cluster.fork()
cluster.on('exit',(worker,code,signal)=>{ cluster.fork();
console.log(`worker ${worker.process.pid} died by: ${code}`);
});
} cacheInterval(arg);
} else {
const server = net.createServer({
allowHalfOpen: true
},api);
server.listen( arg.port, arg.host,()=>{
console.log(JSON.stringify({
name: 'molly-file', protocol: 'TCP',
port: arg.port, host: arg.host,
}));
})
}
}
/*--────────────────────────────────────────────────────────────────────────────────────────────--*/