forked from lyfe00011/levanter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoded
More file actions
158 lines (136 loc) · 4.12 KB
/
Copy pathCoded
File metadata and controls
158 lines (136 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const { spawnSync, spawn } = require('child_process')
const { existsSync, writeFileSync } = require('fs')
const path = require('path')
const SESSION_ID = 'updateThis' // 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()