worker-service 0.2.0

Worker Service - A worker administration microservice that interoperates with the worker-matcher crate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# Worker Service - Deployment Guide

This guide covers deploying the Worker Service (MPI) system using Docker and Docker Compose.

## Table of Contents

- [Prerequisites]#prerequisites
- [Quick Start (Development)]#quick-start-development
- [Production Deployment]#production-deployment
- [Testing Deployment]#testing-deployment
- [Configuration]#configuration
- [Database Migrations]#database-migrations
- [Monitoring]#monitoring
- [Troubleshooting]#troubleshooting

## Prerequisites

### Required Software

- **Docker**: Version 20.10 or later
- **Docker Compose**: Version 2.0 or later

### Verify Installation

```bash
docker --version
docker-compose --version
```

## Quick Start (Development)

### 1. Clone Repository

```bash
git clone https://github.com/your-org/worker-service-rust-crate.git
cd worker-service-rust-crate
```

### 2. Configure Environment

```bash
# Copy example environment file
cp .env.example .env

# Edit configuration as needed
nano .env
```

### 3. Build and Start Services

```bash
# Build the MPI server image
docker-compose build

# Start all services (PostgreSQL + MPI Server)
docker-compose up -d

# View logs
docker-compose logs -f mpi-server
```

### 4. Run Database Migrations

```bash
# Access the MPI server container
docker-compose exec mpi-server bash

# Inside the container, run migrations
sea-orm-cli migrate up --database-url=$DATABASE_URL

# Exit the container
exit
```

### 5. Verify Deployment

```bash
# Check service health
curl http://localhost:8080/api/health

# Expected response:
# {
#   "status": "healthy",
#   "service": "worker-service",
#   "version": "0.1.0"
# }
```

### 6. Access Services

- **MPI API**: http://localhost:8080/api
- **Swagger UI**: http://localhost:8080/swagger-ui
- **pgAdmin** (optional): http://localhost:5050

To enable pgAdmin:

```bash
docker-compose --profile tools up -d
```

## Production Deployment

### 1. Prepare Production Environment

```bash
# Copy production environment template
cp .env.production.example .env.production

# Edit with production values
nano .env.production
```

**IMPORTANT**: Update the following in `.env.production`:

- `DATABASE_URL` - Use strong password and SSL connection
- `POSTGRES_PASSWORD` - Use cryptographically strong password
- `RUST_LOG` - Set to `info` for production

### 2. Build Production Image

```bash
# Build with production optimizations
docker build -t mpi-server:latest .

# Tag for registry
docker tag mpi-server:latest your-registry.com/worker_service-server:v1.0.0
```

### 3. Push to Container Registry

```bash
# Login to your container registry
docker login your-registry.com

# Push image
docker push your-registry.com/worker_service-server:v1.0.0
```

### 4. Deploy to Production Server

```bash
# SSH to production server
ssh production-server

# Pull latest image
docker pull your-registry.com/worker_service-server:v1.0.0

# Start with production compose file
docker-compose -f docker-compose.production.yml up -d
```

### 5. Production Checklist

- [ ] Use SSL/TLS for database connections (`sslmode=require`)
- [ ] Use strong, unique passwords for all services
- [ ] Configure firewall rules (only expose necessary ports)
- [ ] Set up database backups
- [ ] Configure log aggregation
- [ ] Set up monitoring and alerting
- [ ] Use volume mounts for persistent data
- [ ] Enable container restart policies
- [ ] Configure resource limits (CPU, memory)
- [ ] Set up health checks

## Testing Deployment

Run the full test suite using Docker Compose:

```bash
# Build test image and run tests
docker-compose -f docker-compose.test.yml up --build

# View test results
docker-compose -f docker-compose.test.yml logs test-runner

# Clean up test containers
docker-compose -f docker-compose.test.yml down -v
```

### Expected Test Output

```
Running unit tests...
test result: ok. 24 passed; 0 failed; 0 ignored; 0 measured

Running integration tests...
test result: ok. 8 passed; 0 failed; 0 ignored; 0 measured
```

## Configuration

### Environment Variables

All configuration is done via environment variables. See `.env.example` for complete list.

#### Database

```bash
DATABASE_URL=postgresql://user:password@host:5432/database
DATABASE_MAX_CONNECTIONS=10
DATABASE_MIN_CONNECTIONS=2
```

#### Server

```bash
SERVER_HOST=0.0.0.0
SERVER_PORT=8080
```

#### Search Engine

```bash
SEARCH_INDEX_PATH=/app/data/search_index
```

#### Matching Algorithm

```bash
MATCHING_THRESHOLD=0.7
MATCHING_NAME_WEIGHT=0.4
MATCHING_DOB_WEIGHT=0.3
MATCHING_GENDER_WEIGHT=0.1
MATCHING_ADDRESS_WEIGHT=0.2
```

#### Logging

```bash
RUST_LOG=info
RUST_BACKTRACE=0
```

### Docker Compose Profiles

#### Default Profile

Starts only essential services (PostgreSQL + MPI Server):

```bash
docker-compose up -d
```

#### Tools Profile

Includes pgAdmin for database management:

```bash
docker-compose --profile tools up -d
```

## Database Migrations

### Running Migrations

#### Method 1: Inside Container

```bash
docker-compose exec mpi-server bash
sea-orm-cli migrate up
exit
```

#### Method 2: Init Container (Recommended for Production)

Add to `docker-compose.yml`:

```yaml
mpi-migrations:
  image: mpi-server:latest
  depends_on:
    postgres:
      condition: service_healthy
  environment:
    DATABASE_URL: ${DATABASE_URL}
  command: sea-orm-cli migrate up
  networks:
    - mpi-network
```

Then:

```bash
docker-compose up mpi-migrations
```

### Creating New Migrations

```bash
# Inside development environment
sea-orm-cli migrate generate add_new_feature

# Edit up.sql and down.sql
# Test migration
sea-orm-cli migrate up
sea-orm-cli migrate refresh
```

## Monitoring

### Health Checks

The MPI server includes a health check endpoint:

```bash
curl http://localhost:8080/api/health
```

### Docker Health Checks

Health checks are configured in `docker-compose.yml`:

```bash
# View container health status
docker-compose ps

# View health check logs
docker inspect mpi-server --format='{{json .State.Health}}'
```

### Logs

```bash
# View all logs
docker-compose logs

# Follow logs
docker-compose logs -f

# View specific service logs
docker-compose logs mpi-server
docker-compose logs postgres

# View last 100 lines
docker-compose logs --tail=100 mpi-server
```

### Metrics

TODO: Implement Prometheus metrics endpoint

### Resource Usage

```bash
# View resource usage
docker stats

# View resource usage for specific container
docker stats mpi-server
```

## Troubleshooting

### Container Won't Start

**Check logs**:

```bash
docker-compose logs mpi-server
```

**Common issues**:

- Database not ready: Wait for PostgreSQL health check
- Missing environment variables: Check `.env` file
- Port already in use: Change `MPI_PORT` in `.env`

### Database Connection Issues

**Test database connectivity**:

```bash
docker-compose exec postgres psql -U mpi_user -d mpi -c "SELECT 1;"
```

**Common issues**:

- Wrong credentials: Check `DATABASE_URL` matches PostgreSQL settings
- Network issues: Ensure containers are on same network
- PostgreSQL not ready: Check PostgreSQL health status

### Migration Failures

**Reset database** (CAUTION: Destroys all data):

```bash
docker-compose down -v
docker-compose up -d postgres
# Wait for PostgreSQL to be ready
docker-compose exec postgres psql -U mpi_user -d mpi
# Inside psql:
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
\q
# Run migrations
docker-compose exec mpi-server sea-orm-cli migrate up
```

### Search Index Issues

**Reset search index**:

```bash
docker-compose exec mpi-server rm -rf /app/data/search_index/*
docker-compose restart mpi-server
```

### High Memory Usage

**Adjust connection pool sizes**:

```bash
# In .env
DATABASE_MAX_CONNECTIONS=5
DATABASE_MIN_CONNECTIONS=1
```

**Set Docker memory limits**:

```yaml
# In docker-compose.yml
services:
  mpi-server:
    deploy:
      resources:
        limits:
          memory: 512M
```

### Port Conflicts

**Change exposed ports**:

```bash
# In .env
MPI_PORT=8081
POSTGRES_PORT=5433
PGADMIN_PORT=5051
```

## Backup and Recovery

### Database Backup

```bash
# Create backup
docker-compose exec postgres pg_dump -U mpi_user mpi > backup-$(date +%Y%m%d).sql

# Restore from backup
docker-compose exec -T postgres psql -U mpi_user mpi < backup-20231228.sql
```

### Search Index Backup

```bash
# Backup search index
docker cp mpi-server:/app/data/search_index ./search_index_backup

# Restore search index
docker cp ./search_index_backup mpi-server:/app/data/search_index
docker-compose restart mpi-server
```

## Security Best Practices

1. **Use Strong Passwords**: Generate cryptographically strong passwords
2. **Enable SSL**: Use SSL for database connections in production
3. **Limit Network Exposure**: Only expose necessary ports
4. **Regular Updates**: Keep Docker images and dependencies updated
5. **Secrets Management**: Use Docker secrets or environment variable injection
6. **Run as Non-Root**: Container runs as `mpi` user (UID 1000)
7. **Resource Limits**: Set memory and CPU limits in production
8. **Log Management**: Rotate logs and avoid logging sensitive data

## Performance Tuning

### Database Connection Pool

```bash
# Adjust based on workload
DATABASE_MAX_CONNECTIONS=20
DATABASE_MIN_CONNECTIONS=5
```

### Search Index

```bash
# Increase cache for better search performance
SEARCH_CACHE_SIZE_MB=2048
```

### Container Resources

```yaml
services:
  mpi-server:
    deploy:
      resources:
        limits:
          cpus: "2"
          memory: 1G
        reservations:
          cpus: "1"
          memory: 512M
```

## Scaling

### Horizontal Scaling

For high-availability deployments:

1. **Load Balancer**: Use nginx or HAProxy in front of multiple MPI instances
2. **Shared Database**: All instances connect to same PostgreSQL
3. **Shared Search Index**: Use network-mounted search index or separate search service
4. **Stateless Design**: MPI server is stateless, scales horizontally

Example:

```bash
docker-compose up -d --scale mpi-server=3
```

### Vertical Scaling

Increase resources for single instance:

```yaml
services:
  mpi-server:
    deploy:
      resources:
        limits:
          cpus: "4"
          memory: 4G
```

## Next Steps

- Set up CI/CD pipeline for automated deployments
- Configure monitoring with Prometheus and Grafana
- Implement authentication and authorization
- Set up log aggregation (ELK stack or similar)
- Configure automated backups
- Implement disaster recovery procedures