# PM2 deploy migration (one-time VPS cleanup)

After deploying the PM2 reliability fixes, run these steps on the VPS as **root**.

## 1. Inspect current processes

```bash
pm2 list
pm2 jlist | node -e "const d=JSON.parse(require('fs').readFileSync(0)); d.forEach(p=>console.log(p.name, p.pm2_env?.pm_cwd, p.pm2_env?.env?.PORT||p.pm2_env?.PORT))"
```

Note legacy names such as `dev.skcsolution.in`, `skcart.skcsolution.in`, `umi-api`, `testapi`, and duplicates like `skcadmin.skcsolution.in`.

## 2. Deploy owner clients first

From Client Environments, deploy backend owners (these restart the correct API process):

| UI client | Canonical PM2 name (new) | Port |
|-----------|--------------------------|------|
| dev.skcsolution.in | devapi.skcsolution.in | 3001 |
| skcart.skcsolution.in | skcartapi.skcsolution.in | 3006 |
| umiya.skcsolution.in | umiyaapi.skcsolution.in | 3002 |
| tsrcatering.co.in | tsrapi.tsrcatering.co.in | 3007 |

Deploy will restart legacy names if they still exist; no duplicate start when cwd/port matches.

## 3. Do not deploy backend for shared-API UI-only clients

`skcadmin.skcsolution.in` (and similar) will **skip** backend deploy automatically. Only deploy their frontend/SQL, or deploy **skcart** when the shared API needs updating.

## 4. Remove orphan duplicates (after owners are online)

Only after the owner API responds correctly:

```bash
# Example — adjust names to match your pm2 list
pm2 delete skcadmin.skcsolution.in   # if it was a duplicate on port 3006
pm2 delete dev.skcsolution.in        # only after devapi.skcsolution.in is online
pm2 save
```

## 5. Verify

```bash
pm2 list
curl -sI https://devapi.skcsolution.in/api/v1/health  # or a known endpoint
curl -sI https://skcartapi.skcsolution.in/api/v1/health
pm2 save
```

## Expected behavior going forward

- One PM2 process per API backend (named after API host).
- Shared-API clients skip backend deploy; deploy logs show the owner client URL.
- Failed PM2 restart fails the deploy (check deployment logs).
- `pm2 save` runs after every successful backend deploy.
