Skip to content

Create index.js #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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()