torc 0.20.7

Workflow management system
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
# Server Deployment

This guide covers deploying and operating the Torc server in production environments, including
logging configuration, daemonization, and service management.

## Server Subcommands

The `torc-server` binary has two main subcommands:

### `torc-server run`

Use `torc-server run` for:

- **HPC login nodes** - Run the server in a tmux session while your jobs are running.
- **Development and testing** - Run the server interactively in a terminal
- **Manual startup** - When you want to control when the server starts and stops
- **Custom deployment** - Integration with external process managers (e.g., supervisord, custom
  scripts)
- **Debugging** - Running with verbose logging to troubleshoot issues

```bash
# Basic usage
torc-server run

# With options
torc-server run --port 8080 --database ./torc.db --log-level debug
torc-server run --completion-check-interval-secs 5
```

### `torc-server service`

Use `torc-server service` for:

- **Production deployment** - Install as a system service that starts on boot
- **Reliability** - Automatic restart on failure
- **Managed lifecycle** - Standard start/stop/status commands
- **Platform integration** - Uses systemd (Linux), launchd (macOS), or Windows Services

```bash
# Install and start as a user service
torc-server service install --user
torc-server service start --user

# Or as a system service (requires root)
sudo torc-server service install
sudo torc-server service start
```

**Which to choose?**

- For **HPC login nodes/development/testing**: Use `torc-server run`
- For **production servers/standalone computers**: Use `torc-server service install`

## Quick Start

### User Service (Development)

For development, install as a user service (no root required):

```bash
# Install with automatic defaults (logs to ~/.torc/logs, db at ~/.torc/torc.db)
torc-server service install --user

# Start the service
torc-server service start --user
```

### System Service (Production)

For production deployment, install as a system service:

```bash
# Install with automatic defaults (logs to /var/log/torc, db at /var/lib/torc/torc.db)
sudo torc-server service install --user

# Start the service
sudo torc-server service start --user
```

The service will automatically start on boot and restart on failure. Logs are automatically
configured to rotate when they reach 10 MiB (keeping 5 files max). See the
[Service Management](#service-management-recommended-for-production) section for customization
options.

## Logging System

Torc-server uses the `tracing` ecosystem for structured, high-performance logging with automatic
size-based file rotation.

### Console Logging (Default)

By default, logs are written to stdout/stderr only:

```bash
torc-server run --log-level info
```

### File Logging with Size-Based Rotation

Enable file logging by specifying a log directory:

```bash
torc-server run --log-dir /var/log/torc
```

This will:

- Write logs to both console and file
- Automatically rotate when log file reaches 10 MiB
- Keep up to 5 rotated log files (torc-server.log, torc-server.log.1, ..., torc-server.log.5)
- Oldest files are automatically deleted when limit is exceeded

### JSON Format Logs

For structured log aggregation (e.g., ELK stack, Splunk):

```bash
torc-server run --log-dir /var/log/torc --json-logs
```

This writes JSON-formatted logs to the file while keeping human-readable logs on console.

### Log Levels

Control verbosity with the `--log-level` flag or `RUST_LOG` environment variable:

```bash
# Available levels: error, warn, info, debug, trace
torc-server run --log-level debug --log-dir /var/log/torc

# Or using environment variable
RUST_LOG=debug torc-server run --log-dir /var/log/torc
```

### Environment Variables

- `TORC_LOG_DIR`: Default log directory
- `RUST_LOG`: Default log level

Example:

```bash
export TORC_LOG_DIR=/var/log/torc
export RUST_LOG=info
torc-server run
```

## Daemonization (Unix/Linux Only)

Run torc-server as a background daemon:

```bash
torc-server run --daemon --log-dir /var/log/torc
```

**Important:**

- Daemonization is only available on Unix/Linux systems
- When running as daemon, **you must use `--log-dir`** since console output is lost
- The daemon creates a PID file (default: `/var/run/torc-server.pid`)

### Custom PID File Location

```bash
torc-server run --daemon --pid-file /var/run/torc/server.pid --log-dir /var/log/torc
```

### Stopping a Daemon

```bash
# Find the PID
cat /var/run/torc-server.pid

# Kill the process
kill $(cat /var/run/torc-server.pid)

# Or forcefully
kill -9 $(cat /var/run/torc-server.pid)
```

## Complete Example: Production Deployment

```bash
#!/bin/bash
# Production deployment script

# Create required directories
sudo mkdir -p /var/log/torc
sudo mkdir -p /var/run/torc
sudo mkdir -p /var/lib/torc

# Set permissions (adjust as needed)
sudo chown -R torc:torc /var/log/torc
sudo chown -R torc:torc /var/run/torc
sudo chown -R torc:torc /var/lib/torc

# Start server as daemon
torc-server run \
    --daemon \
    --log-dir /var/log/torc \
    --log-level info \
    --json-logs \
    --pid-file /var/run/torc/server.pid \
    --database /var/lib/torc/torc.db \
    --host 0.0.0.0 \
    --port 8080 \
    --threads 8 \
    --auth-file /etc/torc/htpasswd \
    --require-auth
```

## Service Management (Recommended for Production)

### Automatic Installation

The easiest way to install torc-server as a service is using the built-in service management
commands.

#### User Service (No Root Required)

Install as a user service that runs under your user account (recommended for development):

```bash
# Install with defaults (logs to ~/.torc/logs, database at ~/.torc/torc.db)
torc-server service install --user

# Or customize the configuration
torc-server service install --user \
    --log-dir ~/custom/logs \
    --database ~/custom/torc.db \
    --host 0.0.0.0 \
    --port 8080 \
    --threads 4

# Start the user service
torc-server service start --user

# Check status
torc-server service status --user

# Stop the service
torc-server service stop --user

# Uninstall the service
torc-server service uninstall --user
```

**User Service Defaults:**

- Log directory: `~/.torc/logs`
- Database: `~/.torc/torc.db`
- Listen address: `0.0.0.0:8080`
- Worker threads: 4

#### System Service (Requires Root)

Install as a system-wide service (recommended for production):

```bash
# Install with defaults
sudo torc-server service install

# Or customize the configuration
sudo torc-server service install \
    --log-dir /var/log/torc \
    --database /var/lib/torc/torc.db \
    --host 0.0.0.0 \
    --port 8080 \
    --threads 8 \
    --auth-file /etc/torc/htpasswd \
    --require-auth \
    --json-logs

# Start the system service
sudo torc-server service start

# Check status
torc-server service status

# Stop the service
sudo torc-server service stop

# Uninstall the service
sudo torc-server service uninstall
```

**System Service Defaults:**

- Log directory: `/var/log/torc`
- Database: `/var/lib/torc/torc.db`
- Listen address: `0.0.0.0:8080`
- Worker threads: 4

This automatically creates the appropriate service configuration for your platform:

- **Linux**: systemd service (user: `~/.config/systemd/user/`, system: `/etc/systemd/system/`)
- **macOS**: launchd service (user: `~/Library/LaunchAgents/`, system: `/Library/LaunchDaemons/`)
- **Windows**: Windows Service

### Manual Systemd Service (Linux)

Alternatively, you can manually create a systemd service:

```ini
# /etc/systemd/system/torc-server.service
[Unit]
Description=Torc Workflow Orchestration Server
After=network.target

[Service]
Type=simple
User=torc
Group=torc
WorkingDirectory=/var/lib/torc
Environment="RUST_LOG=info"
Environment="TORC_LOG_DIR=/var/log/torc"
ExecStart=/usr/local/bin/torc-server run \
    --log-dir /var/log/torc \
    --json-logs \
    --database /var/lib/torc/torc.db \
    --host 0.0.0.0 \
    --port 8080 \
    --threads 8 \
    --auth-file /etc/torc/htpasswd \
    --require-auth
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target
```

Then:

```bash
sudo systemctl daemon-reload
sudo systemctl enable torc-server
sudo systemctl start torc-server
sudo systemctl status torc-server

# View logs
journalctl -u torc-server -f
```

## Managing Users Without Downtime

User credentials can be added, removed, or updated without restarting the server. After modifying
the htpasswd file, reload the credentials:

```bash
# Add or remove users
torc-htpasswd add --file /etc/torc/htpasswd new_user
torc-htpasswd remove --file /etc/torc/htpasswd old_user

# Reload on the running server (admin credentials required)
torc admin reload-auth
```

For Docker/Kubernetes deployments, call `torc admin reload-auth` after updating the htpasswd file
instead of restarting the container. See
[Hot-Reloading Credentials](./authentication.md#hot-reloading-credentials) for details.

## Log Rotation Strategy

The server uses automatic size-based rotation with the following defaults:

- **Max file size**: 10 MiB per file
- **Max files**: 5 rotated files (plus the current log file)
- **Total disk usage**: Maximum of ~50 MiB for all log files

When the current log file reaches 10 MiB, it is automatically rotated:

1. `torc-server.log``torc-server.log.1`
2. `torc-server.log.1``torc-server.log.2`
3. And so on...
4. Oldest file (`torc-server.log.5`) is deleted

This ensures predictable disk usage without external tools like `logrotate`.

## Timing Instrumentation

For advanced performance monitoring, enable timing instrumentation:

```bash
TORC_TIMING_ENABLED=true torc-server run --log-dir /var/log/torc
```

This adds detailed timing information for all instrumented functions. Note that timing
instrumentation works with both console and file logging.

## Troubleshooting

### Daemon won't start

1. Check permissions on log directory:
   ```bash
   ls -la /var/log/torc
   ```

2. Check if PID file directory exists:
   ```bash
   ls -la /var/run/
   ```

3. Try running in foreground first:
   ```bash
   torc-server run --log-dir /var/log/torc
   ```

### No log files created

1. Verify `--log-dir` is specified
2. Check directory permissions
3. Check disk space: `df -h`

### Logs not rotating

Log rotation happens automatically when a log file reaches 10 MiB. If you need to verify rotation is
working:

1. Check the log directory for numbered files (e.g., `torc-server.log.1`)
2. Monitor disk usage - it should never exceed ~50 MiB for all log files
3. For testing, you can generate large amounts of logs with `--log-level trace`