Skip to content

dblayer-dev/worker.dblayer.dev

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 

Repository files navigation

worker.dblayer.dev

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.

Overview

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.

Features

  • 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

Architecture

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

Prerequisites

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

Installation

1. Clone the Repository

git clone https://github.com/scorcism/worker.dblayer.dev.git
cd worker.dblayer.dev/go_scripts

2. Install Dependencies

go mod download

3. Database Schema

4. Configuration

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/caddy

Note: Update the .env file path in config.go to match your deployment location.

Building

Build All Services

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.go

Automated Setup with Cron

The project includes a setup script that builds binaries and configures cron jobs:

sudo ./cron_setup.sh

This 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

Services

Email Worker

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)

Subdomain Manager

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

Log Cleaner

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

Account Usage Reset

Resets account usage counters on a scheduled basis.

  • Schedule: Daily at 12:30 AM
  • Features:
    • Bulk reset operations
    • Database transaction management

Caddy Backup

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

Usage

Running Individual Services

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_backup

Monitoring

Logs 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

Managing Cron Jobs

# View installed cron jobs
cat /etc/crontab

# Manually trigger a service
sudo /path/to/bin/emailer

# Check cron service status
systemctl status cron

Development

Project Structure

  • 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

Testing

# Run tests
go test ./...

# Test with verbose output
go test -v ./...

# Test a specific package
go test ./cmd/internal/service/emailer/...

Dependencies

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.

Security Considerations

  • Store sensitive credentials in environment variables, never in code
  • Use strong passwords for SMTP authentication
  • Restrict file permissions on .env file: 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

Troubleshooting

Common Issues

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/*.log

Database connection errors

  • Verify DATABASE_URL in .env file
  • 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

Contributing

Contributions are welcome! Please follow these guidelines:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Commit your changes: git commit -am 'Add new feature'
  4. Push to the branch: git push origin feature/your-feature
  5. Submit a pull request

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors