# tbi-acc-cron-service

PHP 8.0 + Lumen cron service for TBI Access platform.

## Overview

This service runs scheduled tasks using Laravel Lumen's scheduler. It's deployed as a Kubernetes CronJob that executes on a defined schedule.

## Technology Stack

- **Framework**: Laravel Lumen 8.x
- **PHP Version**: 8.0
- **Dependencies**: AWS SDK, Elasticsearch, Redis, OpenSearch
- **Deployment**: Kubernetes CronJob
- **CI/CD**: CircleCI

## Environments

- **dev**: Development environment (tbi-dev-eks)
- **uat**: UAT environment (tbi-uat-eks)

## Repository Structure

```
tbi-acc-cron-service/
├── .circleci/
│   └── config.yml           # CircleCI pipeline configuration
├── .github/
│   └── workflows/
│       └── manage-secrets.yml  # Secrets management workflow
├── charts/
│   └── tbi-acc-cron-service/   # Helm chart
│       ├── Chart.yaml
│       ├── values.yaml
│       └── templates/
├── docker/
│   └── Dockerfile           # Multi-stage Docker build
├── scripts/
│   ├── deploy-services.sh   # Deployment script
│   └── setup-kubectl.sh     # kubectl setup script
└── README.md
```

## Local Development

### Prerequisites

- PHP 8.0+
- Composer
- Docker (optional)

### Setup

```bash
# Install dependencies
composer install

# Copy environment file
cp .env.example .env

# Run locally
php artisan schedule:run
```

### Running with Docker

```bash
# Build image
docker build -t tbi-acc-cron-service:local -f docker/Dockerfile .

# Run container
docker run --rm tbi-acc-cron-service:local
```

## Deployment

### Automated Deployment (CircleCI)

Deployments are triggered automatically on branch push:

- **dev branch** → Deploys to dev environment
- **uat branch** → Deploys to uat environment

### Manual Deployment

```bash
# Setup kubectl
./scripts/setup-kubectl.sh tbi-dev-eks

# Deploy to dev
./scripts/deploy-services.sh dev tbi-acc-cron-service access 1.0.0
```

## Configuration

### Environment Variables

Set in `tbi-environments/{env}/values/tbi-acc-cron-service.yaml`:

```yaml
env:
  - name: APP_ENV
    value: "production"
  - name: APP_DEBUG
    value: "false"
  - name: LOG_CHANNEL
    value: "stderr"
```

### Secrets

Managed in `tbi-environments/{env}/secrets/secrets.tbi-acc-cron-service.yaml`:

```yaml
secrets:
  DB_PASSWORD: "encrypted-value"
  AWS_ACCESS_KEY_ID: "encrypted-value"
  AWS_SECRET_ACCESS_KEY: "encrypted-value"
  REDIS_PASSWORD: "encrypted-value"
```

Encrypt secrets using SOPS:
```bash
sops -e -i secrets.tbi-acc-cron-service.yaml
```

### CronJob Schedule

Configure in values.yaml:

```yaml
cronjob:
  enabled: true
  schedule: "*/5 * * * *"  # Every 5 minutes
  concurrencyPolicy: Forbid
  restartPolicy: OnFailure
```

## Monitoring

### Check CronJob Status

```bash
# List CronJobs
kubectl get cronjobs -n access

# List Jobs
kubectl get jobs -n access -l app=tbi-acc-cron-service

# Check logs
kubectl logs -n access -l app=tbi-acc-cron-service --tail=100
```

### View Recent Executions

```bash
# Get recent jobs
kubectl get jobs -n access -l app=tbi-acc-cron-service --sort-by=.metadata.creationTimestamp

# Check specific job logs
kubectl logs -n access job/tbi-acc-cron-service-<timestamp>
```

## Troubleshooting

### CronJob Not Running

```bash
# Check CronJob configuration
kubectl describe cronjob tbi-acc-cron-service -n access

# Check for suspended CronJobs
kubectl get cronjob tbi-acc-cron-service -n access -o yaml | grep suspend
```

### Job Failures

```bash
# List failed jobs
kubectl get jobs -n access -l app=tbi-acc-cron-service --field-selector status.successful=0

# Check job logs
kubectl logs -n access job/<job-name>

# Describe job for events
kubectl describe job <job-name> -n access
```

### Image Pull Errors

```bash
# Verify image exists in ECR
aws ecr describe-images --repository-name tbi-acc-cron-service --region ca-central-1

# Check image pull secrets
kubectl get secrets -n access
```

## CI/CD Pipeline

### Pipeline Stages

1. **Lint**: PHP syntax validation
2. **Test**: Run PHPUnit tests
3. **Build**: Build and push Docker image to ECR
4. **Deploy**: Deploy to Kubernetes using Helm

### Pipeline Variables

Set in CircleCI project settings:

- `AWS_ACCESS_KEY_ID`
- `AWS_SECRET_ACCESS_KEY`
- `AWS_DEFAULT_REGION`

## Support

- **DevOps Team**: devops@travelbrands.com
- **Documentation**: [TBI DevOps Wiki](https://wiki.travelbrands.com)
- **Issues**: Create ticket in Jira

## Related Services

- tbi-acc-hotel-service
- tbi-acc-package-service
- tbi-acc-booking-service

## License

Proprietary - TravelBrands International
