Deployment Configuration
Production deployment patterns using PM2 process manager.
PM2 Configuration
Configuration file: ecosystem.config.js
Purpose:
- Defines application process configuration
- Manages environment variables
- Handles automatic restart on failure
- Supports graceful shutdown
Basic structure:
module.exports = {
apps: [{
name: 'mjpin',
script: './src/index.js',
instances: 1,
exec_mode: 'fork',
watch: false,
autorestart: true,
max_restarts: 10,
env: {
NODE_ENV: 'production',
// Environment variables here
}
}]
};
PM2 Commands
Start application:
pm2 start ecosystem.config.js
View status:
pm2 status
pm2 list
View logs:
pm2 logs mjpin
pm2 logs mjpin --lines 100
Restart application:
pm2 restart mjpin
Stop application:
pm2 stop mjpin
Delete application:
pm2 delete mjpin
Save PM2 configuration:
pm2 save
Setup PM2 startup:
pm2 startup
pm2 save
Restart Behavior
Bot-initiated restart:
Via /restart command:
- Saves restart context to data/restart_info.json
- Sends "Restarting bot…" message
- Exits with code 0 after 500ms
- PM2 detects exit and restarts process
- Bot reconnects to Discord
- Bot updates restart message to "Restart successful."
- Bot deletes restart_info.json
PM2 automatic restart:
On crash or error:
- PM2 restarts immediately on exit code 1
- PM2 respects max_restarts configuration
- No restart message update (no context saved)
Graceful shutdown:
On SIGTERM or SIGINT:
- Bot calls
client.destroy() - Bot exits with code 0
- PM2 does not restart (manual stop/restart)
Process Management
Process isolation:
Single instance per bot (instances: 1).
Execution mode:
Fork mode (not cluster).
Automatic restart:
Enabled for production reliability.
Max restarts:
Configurable limit to prevent restart loops.
Watch mode:
Disabled for production (no automatic restart on file changes).
Logging
Log location:
PM2 manages logs:
- stdout:
~/.pm2/logs/mjpin-out.log - stderr:
~/.pm2/logs/mjpin-error.log
Log rotation:
Configured via PM2:
pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 7
Application logging:
Console.log/error redirected to PM2 logs automatically.
Data Persistence
Critical data directory: /data/
Persistent files:
pinterest_tokens.json– OAuth tokensboards.json– Cached boardspin_counts.json– Rate limit trackingmodel_settings.json– Model configurations*.txt– System prompt files
Deployment strategy:
- Preserve data/ directory during updates
- Never delete data/ directory
- Back up data/ directory before major updates
Data directory location:
Relative to application root. PM2 starts application from correct directory.
Build and Deployment Process
Build process: Use build.sh script to create production package
Deployment preparation:
- Run
./build.shlocally to createmjpin-production.tar.gz - The script automatically excludes development files using
.buildignore - Validates essential files are present in the build
- Creates compressed production archive
Deployment steps:
- Upload
mjpin-production.tar.gzto server - Extract archive:
tar -xzf mjpin-production.tar.gz - Run
npm install --productionin the extracted directory - Configure environment variables
- Start/restart PM2 process:
pm2 restart mjpin(orpm2 start ecosystem.config.js)
Critical:
Do not delete or overwrite data/ directory during deployment. The build script excludes data/ from the production package.
Environment Variables
Production configuration:
Set in ecosystem.config.js env block or system environment.
Required variables:
- MJPIN_DISCORD_TOKEN
- MJPIN_DISCORD_CLIENT_ID
- MJPIN_DISCORD_GUILD_ID
- MJPIN_PINTEREST_CLIENT_ID
- MJPIN_PINTEREST_CLIENT_SECRET
- MJPIN_PINTEREST_REDIRECT_URI
- MJPIN_OPENAI_API_KEY
Security:
- Never commit ecosystem.config.js with secrets
- Use environment variable substitution
- Restrict file permissions
Network Requirements
Inbound:
- HTTP/HTTPS port for Pinterest OAuth callback
- Must be publicly accessible
Outbound:
- Discord Gateway (WSS): wss://gateway.discord.gg
- Discord API: https://discord.com/api/v10
- Pinterest API: https://api.pinterest.com/v5
- OpenAI API: https://api.openai.com/v1
Firewall:
Allow outbound HTTPS (443) and WSS (443).
Monitoring
Process monitoring:
pm2 status
Real-time monitoring:
pm2 monit
Resource usage:
pm2 show mjpin
Uptime tracking:
PM2 tracks uptime, restart count, and memory usage.
Backup Strategy
What to back up:
- data/ directory (all files)
- ecosystem.config.js (configuration)
- .env file (if used)
Backup frequency:
- Before major updates
- Daily automated backup recommended
Restore process:
- Stop bot:
pm2 stop mjpin - Restore data/ directory
- Restore configuration
- Start bot:
pm2 start mjpin
Troubleshooting
Bot not starting:
pm2 logs mjpin --lines 50
Check for environment variable errors or connection issues.
Bot restarting repeatedly:
Check max_restarts count and logs for crash cause.
Commands not working:
Verify GUILD_ID matches target Discord server.
OAuth callback failing:
Verify REDIRECT_URI matches Pinterest app configuration exactly.