# tbi-acc-package-cron

PHP 7.3 + Apache + Oracle cron service for TBI Access platform.

## Overview

This service runs scheduled tasks that sync data between Oracle and MySQL databases. It's deployed as a Kubernetes CronJob that executes on a defined schedule.

## Technology Stack

- **PHP Version**: 7.3
- **Web Server**: Apache
- **Database**: Oracle Instant Client 21.1 + MySQL
- **Deployment**: Kubernetes CronJob
- **CI/CD**: CircleCI

## Environments

- **dev**: Development environment (tbi-dev-eks)
- **prod**: Production environment (tbi-prod-eks)

## Repository Structure

```
tbi-acc-package-cron/
├── .circleci/
│   └── config.yml           # CircleCI pipeline configuration
├── .github/
│   └── workflows/
│       └── manage-secrets.yml  # Secrets management workflow
├── charts/
│   └── tbi-acc-package-cron/   # Helm chart
│       ├── Chart.yaml
│       ├── values.yaml
│       └── templates/
├── docker/
│   └── Dockerfile           # Multi-stage Docker build with Oracle
├── scripts/
│   ├── deploy-services.sh   # Deployment script
│   └── setup-kubectl.sh     # kubectl setup script
├── index.php                # Main cron script
├── citymapping.php          # City mapping script
├── insertLocationCityMapping.php
├── oracleScript.php         # Oracle operations
└── README.md
```

## Local Development

### Prerequisites

- PHP 7.3+
- Oracle Instant Client
- MySQL client
- Docker (recommended)

### Running with Docker

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

# Run container
docker run --rm \
  -e ORACLE_HOST=your-oracle-host \
  -e MYSQL_HOST=your-mysql-host \
  tbi-acc-package-cron:local
```

## Deployment

### Automated Deployment (CircleCI)

Deployments are triggered automatically on branch push:

- **dev branch** → Deploys to dev environment
- **prod/main branch** → Deploys to prod environment

### Manual Deployment

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

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

## Configuration

### Environment Variables

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

```yaml
env:
  - name: TZ
    value: "America/Toronto"
  - name: PHP_MEMORY_LIMIT
    value: "256M"
  - name: PHP_MAX_EXECUTION_TIME
    value: "300"
```

### Secrets

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

```yaml
secrets:
  ORACLE_HOST: "encrypted-value"
  ORACLE_USER: "encrypted-value"
  ORACLE_PASSWORD: "encrypted-value"
  MYSQL_HOST: "encrypted-value"
  MYSQL_USER: "encrypted-value"
  MYSQL_PASSWORD: "encrypted-value"
```

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

### CronJob Schedule

Configure in values.yaml:

```yaml
cronjob:
  enabled: true
  schedule: "0 2 * * *"  # Daily at 2 AM
  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-package-cron

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

### View Recent Executions

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

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

## Troubleshooting

### Oracle Connection Issues

```bash
# Check Oracle connectivity from pod
kubectl exec -it -n access <pod-name> -- bash
php -r "echo oci_connect('user', 'pass', 'tns') ? 'Connected' : 'Failed';"
```

### MySQL Connection Issues

```bash
# Check MySQL connectivity
kubectl exec -it -n access <pod-name> -- bash
php -r "echo mysqli_connect('host', 'user', 'pass') ? 'Connected' : 'Failed';"
```

### CronJob Not Running

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

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

### Job Failures

```bash
# List failed jobs
kubectl get jobs -n access -l app=tbi-acc-package-cron --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
```

## CI/CD Pipeline

### Pipeline Stages

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

### Pipeline Variables

Set in CircleCI project settings:

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

## Oracle Instant Client

This service uses Oracle Instant Client 21.1 for database connectivity. The client is installed during the Docker build process.

### TNS Configuration

Oracle connection strings are configured in the application code. Update the TNS descriptor in the PHP scripts as needed.

## Support

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

## Related Services

- tbi-acc-package-service
- tbi-acc-hotel-service
- tbi-acc-cron-service

## License

Proprietary - TravelBrands International
