# Scheduled tasks (cron)

## Troubleshooting: cron not running

If birthday wishes or event reminders are not being sent:

0. **One-time DB upgrade (delivery logs)** (from project root):
   ```bash
   cd /path/to/nazareth
   php install/upgrade-notification-logs.php
   ```
   This creates the tables used for **Settings → Notifications** delivery history.

1. **Run the environment check** (from project root):
   ```bash
   cd /path/to/nazareth
   php cron/cron-check.php
   ```
   This verifies config, database, settings, and today’s birthday count. Fix any errors it reports.

2. **Run the birthday script manually** to see output or errors:
   ```bash
   cd /path/to/nazareth
   php cron/birthday-wishes.php
   ```
   If you see "command not found: php", use the **full path** to PHP (e.g. `/usr/bin/php` or `/Applications/AMPPS/bin/php`). Find it with: `which php`.

3. **Cron uses a minimal PATH** – the cron daemon often does not have `php` in its PATH. In crontab, use the full path to the PHP binary:
   ```cron
   0 8 * * * cd /Applications/AMPPS/www/nazareth && /Applications/AMPPS/bin/php cron/birthday-wishes.php
   ```
   Replace the path with your actual project path and PHP path (from `which php` when logged in as the same user that runs cron).

4. **Ensure the cron job is installed** – run `crontab -l` and confirm the line is present. If you edit crontab with `crontab -e`, save and exit the editor.

5. **Create a log directory** so you can see output in a file:
   ```bash
   mkdir -p /path/to/nazareth/logs
   chmod 755 /path/to/nazareth/logs
   ```
   After the next run, check `logs/birthday-wishes.log` or `logs/event-reminders.log`.

---

## Birthday wishes

Sends birthday SMS and/or email to all members whose birthday is **today**, using the template and options set under **Settings → Birthday messages**.

### Script

- **Path:** `cron/birthday-wishes.php`
- **Run from project root:**  
  `php cron/birthday-wishes.php`

### Recommended schedule

**Until go-live:** Run at **midnight (12:00 AM)** so a test SMS is sent to the number in `config/app.php` (BIRTHDAY_CRON_TEST_PHONE) to verify the cron runs:

```cron
0 0 * * * cd /path/to/nazareth && /full/path/to/php cron/birthday-wishes.php
```

**After go-live:** Switch to morning (e.g. 8:00 AM) and remove or clear `BIRTHDAY_CRON_TEST_PHONE` in `config/app.php`:

```cron
0 8 * * * cd /path/to/nazareth && /full/path/to/php cron/birthday-wishes.php
```

Replace `/path/to/nazareth` with the full path to your project (e.g. `/Applications/AMPPS/www/nazareth`). Use the full path to `php` (run `which php` to get it).

### How to add the cron job

**Linux / macOS (crontab):**

1. Open crontab:  
   `crontab -e`
2. Add the line above (with the correct path).
3. Save and exit.

**Server panel (cPanel, Plesk, etc.):**

Use the “Cron jobs” or “Scheduled tasks” section and add a new job with:

- **Schedule:** Daily at 8:00 AM (or your preferred time).
- **Command:**  
  `cd /path/to/nazareth && php cron/birthday-wishes.php`  
  (again, use the real path to the project).

### Logging

- Messages are written to **stderr** (so they appear in cron mail if cron is configured to mail output).
- If a `logs/` directory exists and is writable, lines are also appended to **`logs/birthday-wishes.log`**.

Create the log directory if you want file logging:

```bash
mkdir -p logs
chmod 755 logs
```

### Requirements

- PHP CLI with the same extensions as the web app (PDO MySQL, etc.).
- **Settings → Birthday messages** configured (template, and “Send by SMS” and/or “Send by email” as desired).
- **Settings → Email** and/or **Settings → SMS** configured if you enable email/SMS for birthday messages.

---

## Event reminders

Sends SMS and/or email reminders for events that have reminders enabled. Reminders are sent:
- **1 day before** the event (if `reminder_when` = `day_before`)
- **Morning of** the event at 9:00 AM (if `reminder_when` = `morning_of` and cron runs after 7:00 AM)

Recipients can be:
- **All members** (if `reminder_recipient_type` = `all`)
- **Selected groups** (if `reminder_recipient_type` = `groups` and groups are selected)

### Script

- **Path:** `cron/event-reminders.php`
- **Run from project root:**  
  `php cron/event-reminders.php`

### Recommended schedule

Run once per day in the morning (e.g. 7:00 AM):

```cron
0 7 * * * cd /path/to/nazareth && php cron/event-reminders.php
```

Replace `/path/to/nazareth` with the full path to your project.

### How to add the cron job

Same as birthday wishes (see above). Add a new cron entry with the command above.

### Logging

- Messages are written to **stderr**.
- If a `logs/` directory exists and is writable, lines are also appended to **`logs/event-reminders.log`**.

### Requirements

- PHP CLI with the same extensions as the web app (PDO MySQL, etc.).
- **Settings → Email** and/or **Settings → SMS** configured.
- **Settings → Message templates** (optional): Create templates named "Event reminder SMS" and/or "Event reminder Email" with placeholders like `{member_name}`, `{event_title}`, `{event_date}`, `{event_time}`, `{event_location}`. If templates don't exist, default messages are used.
- **Events** with reminders enabled (checkbox "Send reminder to members" checked when creating/editing an event).
