runabol / tork

A distributed workflow engine
https://tork.run
MIT License
603 stars 40 forks source link

Feature: task ports and proxy #432

Closed runabol closed 4 months ago

runabol commented 4 months ago

This PR provides the ability to proxy requests to running tasks.

Potential use-cases:

  1. Long-running tasks that retain their state over time. This could be particularly useful when dealing with tasks that take a long time to boot up.
  2. Running temporary APIs.

Example:

  1. Create a job containing a task with a ports block.
# examples/service.yaml
name: my service job
tasks:
  - name: my service task
    image: node:14
    run: |
      node server.js
    ports: 
      - port: 8080
    files:
      server.js: |
        const http = require('http');
        const hostname = '0.0.0.0';
        const port = 8080;
        const server = http.createServer((req, res) => {
          res.statusCode = 200;
          res.setHeader('Content-Type', 'text/plain');
          res.end('Hello World\n');
        });
        server.listen(port, hostname, () => {
          console.log(`Server running at http://${hostname}:${port}/`);
        });
JOB_ID=$(curl -s -X POST \
  --data-binary @examples/service.yaml \
  -H "Content-type: text/yaml" http://localhost:8000/jobs | jq -r .id)
  1. Get the task id
    TASK_ID=$(curl -s http://localhost:8000/jobs/$JOB_ID | jq -r '.execution[0].id')
  2. Once the task is RUNNING, Route requests to the task container:
    curl -s http://localhost:8000/tasks/$TASK_ID/proxy/8080/some/path
    Hello World