A robust background worker service built in Go for managing scheduled tasks including email processing, subdomain provisioning, log cleanup, account usage resets, and automated backups.
This project provides a collection of independent worker services that handle background tasks for a web application. Each worker is designed to run as a scheduled job via cron, processing tasks from a PostgreSQL database and performing automated maintenance operations.
- Email Queue Processing: Batch processing of queued emails with support for multiple SMTP configurations
- Subdomain Management: Automated provisioning and management of subdomains via Caddy server
- Log Cleanup: Automated cleanup of expired logs based on account retention policies
- Account Usage Reset: Scheduled reset of account usage counters
- Caddy Backup: Automated backup of Caddy configuration to AWS S3
The project follows a clean architecture pattern with the following structure:
go_scripts/
├── cmd/
│ ├── internal/
│ │ ├── config/ # Configuration management
│ │ ├── database/ # Database connection and models
│ │ ├── repository/ # Data access layer
│ │ ├── service/ # Business logic for each worker
│ │ └── utils/ # Shared utilities (logging, S3, zip)
│ └── schedules/ # Entry points for scheduled jobs
├── cron_setup.sh # Automated cron job installation script
└── go.mod # Go module dependencies
Before setting up this project, ensure you have the following installed:
- Go: Version 1.22.2 or higher
- PostgreSQL: Database for storing queue items and application data
- Caddy Server: For subdomain management features (optional)
- AWS Account: For S3 backup functionality (optional)
- Linux System: Required for cron job scheduling
git clone https://github.com/scorcism/worker.dblayer.dev.git
cd worker.dblayer.dev/go_scriptsgo mod download- All the schema are generate with drizzle orm, from dashboard.backend.dblayer.dev, Vist me for exploring all the tables.
Create a .env file in the project root:
# Database Configuration
DATABASE_URL=postgres://user:password@localhost:5432/database?sslmode=disable
# Email Configuration - NoReply SMTP
SMTP_HOST_NO_REPLY=smtp.example.com
SMTP_PORT_NO_REPLY=465
SMTP_USER_NO_REPLY=noreply@yourdomain.com
SMTP_PASSWORD_NO_REPLY=your_password
# Email Configuration - Personal SMTP
SMTP_HOST_PERSONAL=smtp.example.com
SMTP_PORT_PERSONAL=465
SMTP_USER_PERSONAL=contact@yourdomain.com
SMTP_PASSWORD_PERSONAL=your_password
SMTP_REPLY_TO=contact@yourdomain.com
# Worker Configuration
WORKER_BATCH_SIZE=10
# Logging
LOG_PATH=/path/to/logs
# AWS Configuration (for Caddy backup)
AWS_REGION=us-east-1
AWS_BUCKET=your-bucket-name
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
CADDY_SOURCE_DIR=/etc/caddy
S3_PREFIX=backups/caddyNote: Update the .env file path in config.go to match your deployment location.
cd go_scripts
mkdir -p bin
# Build each service
go build -o bin/emailer cmd/schedules/emailer/main.go
go build -o bin/logs_cleaner cmd/schedules/logs_cleaner/main.go
go build -o bin/subdomain cmd/schedules/subdomain/main.go
go build -o bin/account_usage_reset cmd/schedules/account_usage_reset/main.go
go build -o bin/caddy_backup cmd/schedules/caddy_backup/main.goThe project includes a setup script that builds binaries and configures cron jobs:
sudo ./cron_setup.shThis script will:
- Build all service binaries
- Create necessary directories
- Install cron jobs with the following schedule:
- Email processor: Every 2 minutes
- Log cleaner: Every 30 minutes
- Subdomain manager: Every 15 minutes
- Usage reset: Daily at 12:30 AM
- Caddy backup: Every 4 hours
Processes queued emails from the database in batches.
- Schedule: Every 2 minutes
- Batch Size: Configured via
WORKER_BATCH_SIZE - Features:
- Supports multiple SMTP configurations
- Priority-based processing
- Automatic retry on failure
- Status tracking (Pending, Sending, Sent, Failed)
Manages subdomain provisioning by updating Caddy configuration.
- Schedule: Every 15 minutes
- Features:
- Automated subdomain provisioning
- Caddy configuration updates
- Status tracking for provisioning process
- Automatic Caddy reload
Note: Requires root access to modify Caddy configuration at /etc/caddy/Caddyfile
Removes expired logs based on account retention policies.
- Schedule: Every 30 minutes
- Features:
- Respects account-specific retention periods
- Batch processing of cleanup tasks
- Cleanup date tracking
Resets account usage counters on a scheduled basis.
- Schedule: Daily at 12:30 AM
- Features:
- Bulk reset operations
- Database transaction management
Creates compressed backups of Caddy configuration and uploads to AWS S3.
- Schedule: Every 4 hours
- Features:
- Automated directory compression
- Timestamped backups
- S3 upload with configurable prefix
- Retention management via S3 lifecycle policies
Each service can be run independently for testing:
# Email worker
./bin/emailer
# Log cleaner
./bin/logs_cleaner
# Subdomain manager (requires root)
sudo ./bin/subdomain
# Usage reset
./bin/account_usage_reset
# Caddy backup
./bin/caddy_backupLogs are written to the configured LOG_PATH directory:
# View email worker logs
tail -f /path/to/logs/emailer.log
# View all logs
tail -f /path/to/logs/*.log# View installed cron jobs
cat /etc/crontab
# Manually trigger a service
sudo /path/to/bin/emailer
# Check cron service status
systemctl status cron- config: Environment configuration and settings management
- database: Database connection pooling and model definitions
- repository: Data access layer with SQL queries
- service: Business logic for each worker type
- utils: Shared utilities including:
- Structured logging with zerolog
- S3 operations
- File compression
- Helper functions
# Run tests
go test ./...
# Test with verbose output
go test -v ./...
# Test a specific package
go test ./cmd/internal/service/emailer/...Key dependencies include:
- github.com/lib/pq: PostgreSQL driver
- github.com/joho/godotenv: Environment variable management
- github.com/rs/zerolog: Structured logging
- github.com/wneessen/go-mail: Email sending
- github.com/aws/aws-sdk-go-v2: AWS S3 integration
- github.com/google/uuid: UUID generation
Full dependency list available in go.mod.
- Store sensitive credentials in environment variables, never in code
- Use strong passwords for SMTP authentication
- Restrict file permissions on
.envfile:chmod 600 .env - Use IAM roles instead of access keys when running on AWS EC2
- Regularly rotate AWS credentials and SMTP passwords
- Limit database user permissions to only required operations
Services not running
# Check cron service
systemctl status cron
# Verify cron jobs are installed
grep worker.dblayer.dev /etc/crontab
# Check log files for errors
tail -100 /path/to/logs/*.logDatabase connection errors
- Verify
DATABASE_URLin.envfile - Ensure PostgreSQL is running:
systemctl status postgresql - Check network connectivity and firewall rules
Email sending failures
- Verify SMTP credentials and server addresses
- Check SMTP server logs
- Ensure port 465 or 587 is not blocked
Subdomain provisioning fails
- Verify script runs with root permissions
- Check Caddy configuration syntax
- Review Caddy logs:
journalctl -u caddy
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature - Commit your changes:
git commit -am 'Add new feature' - Push to the branch:
git push origin feature/your-feature - Submit a pull request