Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

TaskOrchestrator

CI

A Phoenix JSON API that takes tasks with dependencies, puts them in an order where every task runs after the tasks it requires, and turns them into a bash script.

Requirements

  • Elixir 1.15+ (CI runs Elixir 1.20 / OTP 29)

Setup and running

mix deps.get
mix phx.server

The API runs at http://localhost:4000.

API

POST /api/process

Send a JSON body with a tasks list. Each task has:

Field Required Description
name yes Unique name, a single line
command yes Shell command to run
requires no Names of tasks that must run first

Request

{
  "tasks": [
    {"name": "task-1", "command": "touch /tmp/file1"},
    {"name": "task-3", "command": "echo \"Hello World!\" > /tmp/file1", "requires": ["task-1"]},
    {"name": "task-2", "command": "cat /tmp/file1", "requires": ["task-3"]},
    {"name": "task-4", "command": "rm /tmp/file1", "requires": ["task-2", "task-3"]}
  ]
}

Response (200)

The sorted tasks (only name and command) and the generated script:

{
  "tasks": [
    {"name": "task-1", "command": "touch /tmp/file1"},
    {"name": "task-3", "command": "echo \"Hello World!\" > /tmp/file1"},
    {"name": "task-2", "command": "cat /tmp/file1"},
    {"name": "task-4", "command": "rm /tmp/file1"}
  ],
  "script": "#!/usr/bin/env bash\nset -e\n# task-1\ntouch /tmp/file1\n# task-3\necho \"Hello World!\" > /tmp/file1\n# task-2\ncat /tmp/file1\n# task-4\nrm /tmp/file1\n"
}

The script stops at the first failing command (set -e), and each command has a # name comment above it.

Plain bash output

Add ?format=bash (or send Accept: text/plain) to get the script as text/plain, so you can run it straight away:

curl -s -X POST 'http://localhost:4000/api/process?format=bash' \
  -H 'Content-Type: application/json' \
  -d @tasks.json | bash
#!/usr/bin/env bash
set -e
# task-1
touch /tmp/file1
# task-3
echo "Hello World!" > /tmp/file1
# task-2
cat /tmp/file1
# task-4
rm /tmp/file1

Errors are always returned as JSON, in the same {"error": "..."} shape.

Errors

Status When Example error
400 The body has no tasks key Missing required "tasks" key in request body
400 The body isn't valid JSON Request body is not valid JSON
413 The body is larger than 8 MB Request body is too large
415 Content-Type isn't application/json Content-Type must be application/json
422 Tasks depend on each other in a loop Circular dependency detected: a -> b -> a
422 A required task doesn't exist Task "a" requires "missing", which is not defined
422 Two tasks share a name Duplicate task names: a
422 A field is missing, empty or the wrong type Task "a" is missing required field "command"
{"error": "Circular dependency detected: a -> b -> a"}

How it works

  • TaskOrchestrator.TaskManager validates the tasks (shape and types, unique names, every requires entry exists), sorts them and builds the script. Task names can't contain line breaks, so a name can never turn into a command in the script.
  • TaskOrchestrator.TopologicalSort is a depth-first topological sort. It visits tasks in the order they were sent, so tasks that don't depend on each other keep that order and the output is always the same for the same input. If it finds a loop, it returns the exact path, e.g. a -> b -> a.
  • TaskOrchestratorWeb.TaskController turns the result into JSON or plain text, and TaskOrchestratorWeb.ErrorJSON gives request errors (bad JSON, wrong content type, unknown route) the same JSON shape.

Validation and sorting are linear in the number of tasks and dependencies: 100,000 tasks are processed in about 0.3 s.

Tests

mix test

CI also checks formatting, compiler warnings, Credo (mix credo --strict), and unused or retired dependencies.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages