# NGBVD Concurrency / Performance Patch

This patch implements the earlier recommendations to reduce same-user browser tab blocking and stop slow external integrations from holding page requests.

## Implemented

1. Added `App\Http\Middleware\CloseSessionEarly`.
   - Runs after Laravel starts the session.
   - For `GET` and `HEAD` requests, it saves/releases the session before controller processing.
   - This helps two tabs from the same logged-in user load independently, especially for `/dashboard`, `/dataentry/report`, `/dataentry/update`, `/location/*`, and report/list pages.

2. Registered the middleware in `app/Http/Kernel.php`.
   - It is included in the `web` middleware group after `StartSession`.
   - It is also available as route middleware alias `close.session.early`.

3. Removed production Debugbar registration from `config/app.php`.
   - `Barryvdh\Debugbar\ServiceProvider` and the `Debugbar` alias were removed so production does not try to load a development-only package.

4. Added queued NSR integration job.
   - New job: `App\Jobs\PushNsrToRegistry`.
   - Data-entry saves no longer call NSR directly in the user request.
   - If `QUEUE_CONNECTION=sync`, the system logs a warning and skips NSR push rather than blocking the user screen.
   - Configure a real queue to enable asynchronous NSR push.

5. Added SQL support file:
   - `database/sql/2026_06_09_performance_concurrency_support.sql`
   - Creates `sessions`, `jobs`, and `failed_jobs` tables if missing.

## Production `.env` recommendation

```env
APP_ENV=production
APP_DEBUG=false
DEBUGBAR_ENABLED=false
SESSION_DRIVER=database
QUEUE_CONNECTION=database
```

## Run on production

```bash
mysql -u <user> -p <database> < database/sql/2026_06_09_performance_concurrency_support.sql
php artisan optimize:clear
php artisan config:clear
php artisan view:clear
php artisan config:cache
```

Start a queue worker:

```bash
php artisan queue:work database --queue=integrations,default --tries=3 --timeout=120
```

For production, run the queue worker through Supervisor/systemd so it stays alive.

## Testing

Open two tabs from the same browser/login:

- Tab 1: `/dataentry/report?id=<uuid>`
- Tab 2: `/location/districts`

The second tab should no longer wait for the first page to finish, unless the database/server itself is saturated or a write transaction is locking the same rows.
