-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathstart-with-redis-hf.cjs
More file actions
161 lines (136 loc) · 4.2 KB
/
start-with-redis-hf.cjs
File metadata and controls
161 lines (136 loc) · 4.2 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
159
160
161
/**
* HuggingFace Spaces Redis startup script.
* Runs Redis and the Node.js app in the same container.
*/
const { spawn, execSync } = require('child_process')
const fs = require('fs')
console.log('ManimCat HuggingFace Spaces startup script')
console.log('============================================')
const REDIS_PORT = process.env.REDIS_PORT || 6379
const REDIS_DIR = '/data/redis'
const REDIS_CONFIG = {
port: REDIS_PORT,
dir: REDIS_DIR,
maxmemory: process.env.REDIS_MAXMEMORY || '256mb',
maxmemoryPolicy: 'allkeys-lru',
appendonly: 'yes',
appendfsync: 'everysec',
daemonize: 'yes'
}
function ensureRedisDir() {
try {
if (!fs.existsSync(REDIS_DIR)) {
fs.mkdirSync(REDIS_DIR, { recursive: true, mode: 0o755 })
console.log(`Created Redis data directory: ${REDIS_DIR}`)
}
} catch (error) {
console.error(`Failed to create Redis directory: ${error.message}`)
process.exit(1)
}
}
function startRedis() {
return new Promise((resolve, reject) => {
console.log(`Starting Redis server on port ${REDIS_PORT}...`)
const redisArgs = [
'--port', REDIS_PORT.toString(),
'--dir', REDIS_DIR,
'--maxmemory', REDIS_CONFIG.maxmemory,
'--maxmemory-policy', REDIS_CONFIG.maxmemoryPolicy,
'--appendonly', REDIS_CONFIG.appendonly,
'--appendfsync', REDIS_CONFIG.appendfsync,
'--daemonize', REDIS_CONFIG.daemonize
]
try {
execSync(`redis-server ${redisArgs.join(' ')}`, { stdio: 'pipe' })
console.log('Redis server started successfully')
setTimeout(() => {
try {
execSync('redis-cli ping', { stdio: 'pipe' })
console.log('Redis is ready and responding to PING')
resolve()
} catch (error) {
reject(new Error('Redis started but not responding to PING'))
}
}, 2000)
} catch (error) {
reject(new Error(`Failed to start Redis: ${error.message}`))
}
})
}
function startNodeApp() {
return new Promise((resolve, reject) => {
console.log('Starting Node.js application...')
const nodeApp = spawn('npm', ['run', 'start'], {
stdio: 'inherit',
env: {
...process.env,
REDIS_HOST: 'localhost',
REDIS_PORT: REDIS_PORT.toString()
}
})
nodeApp.on('error', (error) => {
console.error('Failed to start Node.js application:', error)
reject(error)
})
nodeApp.on('exit', (code, signal) => {
if (signal) {
console.log(`Node.js application stopped by signal ${signal}`)
} else {
console.log(`Node.js application exited with code ${code}`)
}
cleanup()
process.exit(code || 0)
})
resolve(nodeApp)
})
}
function cleanup() {
console.log('Cleaning up resources...')
try {
execSync('redis-cli shutdown', { stdio: 'pipe' })
console.log('Redis server stopped')
} catch (error) {
console.warn('Redis may have already stopped')
}
}
function setupSignalHandlers(nodeApp) {
const signals = ['SIGINT', 'SIGTERM', 'SIGQUIT']
signals.forEach((signal) => {
process.on(signal, () => {
console.log(`Received ${signal}, shutting down gracefully...`)
if (nodeApp) {
nodeApp.kill(signal)
} else {
cleanup()
process.exit(0)
}
})
})
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error)
cleanup()
process.exit(1)
})
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason)
cleanup()
process.exit(1)
})
}
async function main() {
try {
ensureRedisDir()
await startRedis()
const nodeApp = await startNodeApp()
setupSignalHandlers(nodeApp)
console.log('All services started successfully')
console.log('Application is running on port', process.env.PORT || 7860)
console.log('Health check:', `http://localhost:${process.env.PORT || 7860}/health`)
console.log('Press Ctrl+C to stop')
} catch (error) {
console.error('Startup failed:', error.message)
cleanup()
process.exit(1)
}
}
main()