Skip to content

Instantly share code, notes, and snippets.

@Linch1
Created June 24, 2022 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Linch1/74ecce7711d7afe8031c01b0b3c40865 to your computer and use it in GitHub Desktop.
Save Linch1/74ecce7711d7afe8031c01b0b3c40865 to your computer and use it in GitHub Desktop.
Restart Web3TokenCharting on crash
require('dotenv').config();
// initialize mongodb
const Transactions = require('../server/models/history_transactions');
var configDB = require('../server/config/database');
const mongoose = require('mongoose');
console.log( configDB.url )
mongoose.connect(configDB.url, {
autoIndex: false,
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => { console.log('14MongoDB is connected') })
.catch(err => {
console.log('MongoDB connection unsuccessful');
console.log(err)
});
const { exec } = require('child_process');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
(async () => {
let transactionsCount = await Transactions.countDocuments();
while( true ){
let newTransactionsCount = await Transactions.countDocuments();
console.log(`[tx count] new: ${newTransactionsCount} | old: ${transactionsCount}` );
if( transactionsCount >= newTransactionsCount ) {
exec('pm2 restart 17', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.log('Could not execute command', err)
return;
}
console.log('Restarted process')
});
}
transactionsCount = newTransactionsCount;
await sleep( 60 * 1000 );
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment