From 0fa8df61330611c4bb53a11f97ffdf8f79ab6638 Mon Sep 17 00:00:00 2001 From: HADIXHACKER Date: Fri, 14 Mar 2025 09:01:12 +0500 Subject: [PATCH] Create index.js --- index.js | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 000000000000..466ce32d3999 --- /dev/null +++ b/index.js @@ -0,0 +1,158 @@ +const { spawnSync, spawn } = require('child_process') +const { existsSync, writeFileSync } = require('fs') +const path = require('path') + +const SESSION_ID = 'levanter_14e31ee000d21b4cc69194fcadae950c75' // Edit this line only, don't remove ' <- this symbol + +let nodeRestartCount = 0 +const maxNodeRestarts = 5 +const restartWindow = 30000 // 30 seconds +let lastRestartTime = Date.now() + +function startNode() { + const child = spawn('node', ['index.js'], { cwd: 'levanter', stdio: 'inherit' }) + + child.on('exit', (code) => { + if (code !== 0) { + const currentTime = Date.now() + if (currentTime - lastRestartTime > restartWindow) { + nodeRestartCount = 0 + } + lastRestartTime = currentTime + nodeRestartCount++ + + if (nodeRestartCount > maxNodeRestarts) { + console.error('Node.js process is restarting continuously. Stopping retries...') + return + } + console.log( + `Node.js process exited with code ${code}. Restarting... (Attempt ${nodeRestartCount})` + ) + startNode() + } + }) +} + +function startPm2() { + const pm2 = spawn('yarn', ['pm2', 'start', 'index.js', '--name', 'levanter', '--attach'], { + cwd: 'levanter', + stdio: ['pipe', 'pipe', 'pipe'], + }) + + let restartCount = 0 + const maxRestarts = 5 // Adjust this value as needed + + pm2.on('exit', (code) => { + if (code !== 0) { + // console.log('yarn pm2 failed to start, falling back to node...') + startNode() + } + }) + + pm2.on('error', (error) => { + console.error(`yarn pm2 error: ${error.message}`) + startNode() + }) + + // Check for infinite restarts + if (pm2.stderr) { + pm2.stderr.on('data', (data) => { + const output = data.toString() + if (output.includes('restart')) { + restartCount++ + if (restartCount > maxRestarts) { + // console.log('yarn pm2 is restarting indefinitely, stopping yarn pm2 and starting node...') + spawnSync('yarn', ['pm2', 'delete', 'levanter'], { cwd: 'levanter', stdio: 'inherit' }) + startNode() + } + } + }) + } + + if (pm2.stdout) { + pm2.stdout.on('data', (data) => { + const output = data.toString() + console.log(output) + if (output.includes('Connecting')) { + // console.log('Application is online.') + restartCount = 0 + } + }) + } +} + +function installDependencies() { + // console.log('Installing dependencies...') + const installResult = spawnSync( + 'yarn', + ['install', '--force', '--non-interactive', '--network-concurrency', '3'], + { + cwd: 'levanter', + stdio: 'inherit', + env: { ...process.env, CI: 'true' }, // Ensure non-interactive environment + } + ) + + if (installResult.error || installResult.status !== 0) { + console.error( + `Failed to install dependencies: ${ + installResult.error ? installResult.error.message : 'Unknown error' + }` + ) + process.exit(1) // Exit the process if installation fails + } +} + +function checkDependencies() { + if (!existsSync(path.resolve('levanter/package.json'))) { + console.error('package.json not found!') + process.exit(1) + } + + const result = spawnSync('yarn', ['check', '--verify-tree'], { + cwd: 'levanter', + stdio: 'inherit', + }) + + // Check the exit code to determine if there was an error + if (result.status !== 0) { + console.log('Some dependencies are missing or incorrectly installed.') + installDependencies() + } else { + // console.log('All dependencies are installed properly.') + } +} + +function cloneRepository() { + // console.log('Cloning the repository...') + const cloneResult = spawnSync( + 'git', + ['clone', 'https://github.com/lyfe00011/levanter.git', 'levanter'], + { + stdio: 'inherit', + } + ) + + if (cloneResult.error) { + throw new Error(`Failed to clone the repository: ${cloneResult.error.message}`) + } + + const configPath = 'levanter/config.env' + try { + // console.log('Writing to config.env...') + writeFileSync(configPath, `VPS=true\nSESSION_ID=${SESSION_ID}`) + } catch (err) { + throw new Error(`Failed to write to config.env: ${err.message}`) + } + + installDependencies() +} + +if (!existsSync('levanter')) { + cloneRepository() + checkDependencies() +} else { + checkDependencies() +} + +startPm2()