Remote Production Server Management
This guide covers convenient local commands for managing the remote production server, similar to Capistrano-style deployment tools.
Quick Start
Two interfaces available:
- Rake tasks (Rails-native):
rake remote:console - Bash script (lightweight):
./scripts/remote.sh console
Both provide the same functionality - choose based on preference.
Available Commands
Rails Console
Open a remote Rails console:
# Using rake
rake remote:console
# Using script
./scripts/remote.sh console
This opens an interactive Rails console in the production Docker container.
Log Management
Tail logs in real-time:
# Rails logs
rake remote:logs
./scripts/remote.sh logs
# Sidekiq logs
rake remote:logs_sidekiq
./scripts/remote.sh logs sidekiq
# All services
rake remote:logs_all
./scripts/remote.sh logs
Download logs locally:
# Downloads last 1000 lines from each service to logs/remote/
rake remote:logs_download
./scripts/remote.sh logs-download
Logs are saved with timestamps:
logs/remote/rails_20251024_143022.loglogs/remote/sidekiq_20251024_143022.log- etc.
Service Management
Check service status:
rake remote:status
./scripts/remote.sh status
Restart services:
# Restart Rails
rake remote:restart_rails
./scripts/remote.sh restart rails
# Restart Sidekiq
rake remote:restart_sidekiq
./scripts/remote.sh restart sidekiq
# Restart all
rake remote:restart_all
./scripts/remote.sh restart all
Database Operations
Run migrations:
rake remote:migrate
Health Checks
Check application health:
rake remote:health
./scripts/remote.sh health
Returns HTTP status and response from /up endpoint.
Resource Monitoring
View CPU and memory usage:
rake remote:stats
./scripts/remote.sh stats
Shows real-time resource usage for all containers.
SSH Access
Direct SSH connection:
rake remote:ssh
./scripts/remote.sh ssh
Run Remote Rake Tasks
Execute any rake task on the remote server:
# Without arguments
bundle exec rake remote:run[cache:refresh_all]
./scripts/remote.sh run cache:refresh_all
# With arguments
bundle exec rake remote:run[parser_jobs:process,10]
./scripts/remote.sh run parser_jobs:process 10
bundle exec rake remote:run[parser_jobs:retry,5]
./scripts/remote.sh run parser_jobs:retry 5
# Multiple arguments
bundle exec rake remote:run[cache:set_all]
List available remote rake tasks:
# List all tasks
bundle exec rake remote:tasks
./scripts/remote.sh tasks
# Filter tasks by pattern
bundle exec rake remote:tasks_filter[cache]
./scripts/remote.sh tasks cache
bundle exec rake remote:tasks_filter[parser_jobs]
./scripts/remote.sh tasks parser_jobs
Custom Commands
Execute arbitrary commands on remote server:
rake remote:exec['docker compose -f docker-compose.production.yml ps']
Configuration
Default Configuration
REMOTE_HOST = "deploy@91.98.204.128"
REMOTE_PATH = "/opt/odapi"
COMPOSE_FILE = "docker-compose.production.yml"
Override with Environment Variables
# Single command
REMOTE_HOST=deploy@other-server.com rake remote:console
# Or export for session
export REMOTE_HOST=deploy@other-server.com
export REMOTE_PATH=/opt/myapp
rake remote:console
Permanent Configuration
Create .env.remote (gitignored):
cp .env.remote.example .env.remote
# Edit with your values
Listing Available Tasks
bundle exec rake -T remote
Output:
rake remote:console # Open a Rails console on the remote production server
rake remote:exec[command] # Execute a custom command on remote server
rake remote:health # Check remote application health endpoint
rake remote:logs # Tail Rails logs from remote server
rake remote:logs_all # Tail all service logs from remote server
rake remote:logs_download # Download recent logs from remote server
rake remote:logs_sidekiq # Tail Sidekiq logs from remote server
rake remote:migrate # Run database migrations on remote server
rake remote:restart_all # Restart all services on remote server
rake remote:restart_rails # Restart Rails service on remote server
rake remote:restart_sidekiq # Restart Sidekiq service on remote server
rake remote:run[task,args] # Run a rake task on the remote server
rake remote:ssh # Open an SSH session to the remote server
rake remote:stats # View Docker stats for remote services
rake remote:status # Check status of all remote services
rake remote:tasks # List all available rake tasks on the remote server
rake remote:tasks_filter # List rake tasks matching a pattern
Common Workflows
Debugging Production Issues
# 1. Check service status
bundle exec rake remote:status
# 2. View recent logs
bundle exec rake remote:logs
# 3. Open console to investigate
bundle exec rake remote:console
# 4. Download logs for analysis
bundle exec rake remote:logs_download
Running Background Job Tasks
# Process queued parser jobs
bundle exec rake remote:run[parser_jobs:process,10]
# Retry failed jobs
bundle exec rake remote:run[parser_jobs:retry,5]
# Check job statistics
bundle exec rake remote:run[parser_jobs:stats]
# Reset stuck processing jobs
bundle exec rake remote:run[parser_jobs:reset_processing]
Cache Management
# Refresh all caches
bundle exec rake remote:run[cache:refresh_all]
# Set cache for specific model
bundle exec rake remote:run[cache:set_all]
# Clear all caches
bundle exec rake remote:run[cache:clear_all]
# Check cache status
bundle exec rake remote:run[cache:status]
Finding Available Tasks
# List all cache-related tasks
bundle exec rake remote:tasks_filter[cache]
# List all parser job tasks
bundle exec rake remote:tasks_filter[parser_jobs]
# List ALL available tasks
bundle exec rake remote:tasks
Deploying Code Changes
# 1. SSH to server
rake remote:ssh
# 2. Pull code and rebuild (on server)
cd /opt/odapi
git pull
docker build -f Dockerfile.production -t odapi:latest .
docker compose -f docker-compose.production.yml up -d
# 3. Check status (from local)
rake remote:status
rake remote:health
Performance Investigation
# Check resource usage
rake remote:stats
# Monitor logs for slow queries
rake remote:logs
SSH Key Setup
These commands use SSH key authentication. Ensure your key is configured:
# Test SSH connection
ssh deploy@91.98.204.128 echo "Connection successful"
# If issues, check SSH key
ls -la ~/.ssh/id_ed25519*
Security Notes
- Commands use SSH key authentication (no passwords stored)
.env.remoteis gitignored (never commit credentials)- Console access requires deploy user permissions
- All commands execute over encrypted SSH connection
Troubleshooting
"Connection refused" errors
Check SSH key:
ssh -v deploy@91.98.204.128
"No such file or directory"
Verify remote path:
rake remote:exec['ls -la']
Logs not downloading
Check local directory permissions:
mkdir -p logs/remote
ls -la logs/
Comparison to Capistrano
| Feature | Capistrano | This Setup |
|---|---|---|
| Remote console | cap production rails:console | rake remote:console |
| View logs | cap production logs:tail | rake remote:logs |
| Restart services | cap production deploy:restart | rake remote:restart_rails |
| Run migrations | cap production deploy:migrate | rake remote:migrate |
| SSH access | cap production ssh | rake remote:ssh |
Additional Resources
- Production deployment:
docs/SERVER_SETUP_GUIDE.md - Docker configuration:
docker-compose.production.yml - Deployment scripts:
scripts/deploy-production.sh