# Cronjob Handling System - Architecture Overview

## Purpose

This system provides a robust middleware layer between OS-level cronjobs and the Django application. It ensures:

- **Execution tracking**: Records when cronjobs run and their completion status
- **Duplicate prevention**: Prevents the same cronjob from running multiple times for the same date
- **Error handling**: Comprehensive error logging and tracking
- **Audit trail**: Complete history of cronjob executions with success/failure status

## System Architecture

```
OS Cronjob (crontab)
    ↓
Django Cronjob Handler (this system)
    ↓
Business Logic (Django models/views)
    ↓
Database (cron execution records)
```

## Core Components

### 1. CronBase (Base Class)

The foundation class that provides common functionality for all cronjob handlers.

**Key Responsibilities:**
- Database connection management
- Cron execution record management
- Error handling and logging
- Duplicate execution detection

**Database Tables:**
- `cron`: Standard cronjob execution records
- `cron_rapid`: Rapid/frequent cronjob execution records (for jobs that run multiple times per day)

### 2. Cron (Implementation Class)

Extends CronBase and implements specific cronjob tasks.

**Key Features:**
- Multiple cronjob methods, each handling a specific business task
- Consistent execution pattern across all methods
- Error code mapping system
- Transaction code generation for logging

## Execution Flow

### Standard Cronjob Execution Pattern

```
1. Initialize system
   ↓
2. Validate input (typically date)
   ↓
3. Check if cron already executed for this date
   ↓
4. Record cron start in database
   ↓
5. Execute business logic
   ↓
6. Handle errors (if any)
   ↓
7. Update cron completion status
   ↓
8. Log results
```

### Error Handling Flow

```
Business Logic Exception
   ↓
Catch exception
   ↓
Set error code
   ↓
Log error details
   ↓
Mark cron as failed
   ↓
Record error message
```

## Key Design Patterns

### 1. Singleton Pattern
- Database connection is initialized once and reused
- Cron instance is created once per execution

### 2. Template Method Pattern
- All cronjob methods follow the same structure
- Base class provides common functionality
- Derived class implements specific business logic

### 3. Error Code System
- Hierarchical error codes (class code + method code + error code)
- Example: `0801A111001` = Class 0801, Method A111, Error 001

### 4. Transaction Code System
- Unique transaction codes for logging
- Format: `cron-{cron-type-name}`
- Example: `cron-send-brith-noti`

## Database Schema

### cron table
- `id`: Primary key
- `code`: Cron job code (e.g., 'A111', 'A002')
- `started`: Timestamp when cron started
- `ended`: Timestamp when cron completed
- `completed`: 'y' or 'n'
- `success`: 'y' or 'n'
- `message`: Error/success message
- `err_code`: Error code if failed
- `year`: Year of execution
- `month`: Month of execution
- `day`: Day of execution
- `odate`: Date in YYYY-MM-DD format

### cron_rapid table
- Same structure as `cron` table
- Used for frequent cronjobs that may run multiple times per day
