Skip to content

Commit 9a444d7

Browse files
committed
refactor: remove inline code support, use script files only
Both NATS and database sources now only accept script paths. This simplifies the model and ensures workers are always in the --root directory. - Update NATS TaskMessage to require script field - Simplify handle_nats_message - Update README with testing section and consistent docs
1 parent 008aa07 commit 9a444d7

2 files changed

Lines changed: 25 additions & 27 deletions

File tree

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,7 @@ task-executor listen \
8787
}
8888
```
8989

90-
Or with inline code:
91-
92-
```json
93-
{
94-
"code": "export default { task: () => ({ hello: 'world' }) }",
95-
"payload": {}
96-
}
97-
```
90+
> **Note**: Scripts must exist in the `--root` directory. Nested paths like `"script": "workers/task.js"` are allowed.
9891
9992
### Database listener (`db-listen`)
10093

@@ -219,6 +212,25 @@ task-executor db-listen --database-url $DATABASE_URL
219212
# Both will process tasks without conflicts
220213
```
221214

215+
## Testing
216+
217+
```bash
218+
# Run all tests (requires PostgreSQL for db tests)
219+
cargo test --features v8,database
220+
221+
# Run only database tests
222+
cargo test --features v8,database db_
223+
```
224+
225+
Database tests use `.env.test` for configuration:
226+
227+
```bash
228+
# .env.test
229+
DATABASE_URL=postgres://postgres:postgres@localhost/postgres
230+
```
231+
232+
Tests create isolated tables (`test_tasks_{uuid}`) and clean up automatically.
233+
222234
## License
223235

224236
MIT

src/main.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ async fn listen_nats(
238238
#[derive(serde::Deserialize)]
239239
struct TaskMessage {
240240
/// Path to script file (relative to root)
241-
script: Option<String>,
242-
/// Inline code to execute
243-
code: Option<String>,
241+
script: String,
244242
/// JSON payload for the task
245243
payload: Option<serde_json::Value>,
246244
/// Timeout in milliseconds (optional, uses default if not specified)
@@ -260,22 +258,10 @@ async fn handle_nats_message(
260258
let msg: TaskMessage =
261259
serde_json::from_slice(payload).map_err(|e| format!("Invalid message format: {}", e))?;
262260

263-
// Get script content (either from file or inline)
264-
let script_content = match (&msg.script, &msg.code) {
265-
(Some(script_path), None) => {
266-
// Resolve and validate path
267-
let resolved = resolve_script_path(root, script_path)?;
268-
std::fs::read_to_string(&resolved)
269-
.map_err(|e| format!("Failed to read script '{}': {}", script_path, e))?
270-
}
271-
(None, Some(code)) => code.clone(),
272-
(Some(_), Some(_)) => {
273-
return Err("Cannot specify both 'script' and 'code'".into());
274-
}
275-
(None, None) => {
276-
return Err("Must specify either 'script' or 'code'".into());
277-
}
278-
};
261+
// Get script content from file
262+
let resolved = resolve_script_path(root, &msg.script)?;
263+
let script_content = std::fs::read_to_string(&resolved)
264+
.map_err(|e| format!("Failed to read script '{}': {}", msg.script, e))?;
279265

280266
let timeout = msg.timeout.unwrap_or(default_timeout);
281267

0 commit comments

Comments
 (0)