Skip to content

Running the example

manikvenkat edited this page Oct 16, 2019 · 11 revisions

Kronos Examples

In this guide will schedule a workflow to runs periodically every 5 mins. The workflow consists of a single task executing shell command printing "Hello World" which will be later modified to show how to configure dependent tasks.

Prerequisite

Note: The ShellCommandHandler is configured as one of the handler as part of the Kronos distribution which executes tasks of type shellCommand. We will be using this handler in the example below. The handler configuration is defined in executor.yaml.

Running the sample workflow

  • Create a namespace for the workflow. We will be scheduling the sample workflow under cognitree namespace. The namespace is more like a container to isolate workflows usually used in a multi-tenant environment.

POST /kronos/namespaces

{
    "name": "cognitree",
    "description": "cognitree namespace"
}
  • Create workflow

POST /kronos/workflows

HEADERS namespace=cognitree

{
    "name": "TestWorkflow",
    "namespace": "cognitree",
    "description": "sample workflow",
    "tasks": [
        {
            "name": "TaskOne",
            "type": "shellCommand",
            "properties": {
                "cmd": "echo",
                "args": "Hello World",
                "workingDir": "/tmp/",
                "logDir": "/tmp/"
            }
        }
    ]
}

We have created a workflow with the name TestWorkflow. The workflow contains only 1 task namely TaskOne which is of type shellCommand. The task of type shellCommand is configured to be handled by ShellCommandHandler in executor.yaml.

  • Attach a workflow trigger to schedule the workflow every 5 min

POST /kronos/workflows/TestWorkflow/triggers

HEADERS namespace=cognitree

{
    "name": "TestWorkflowTrigger",
    "namespace": "cognitree",
    "schedule": {
        "type": "cron",
        "cronExpression": "0 0/5 * 1/1 * ? *"
    }
}

Here, we have attached a trigger with the name TestWorkflowTrigger for workflow TestWorkflow and is scheduled every 5 mins.

  • Wait for the workflow to be triggered. The workflow will be triggered every 5 min. The next trigger time can be viewed by using the cronmaker utility.

  • On trigger Kronos creates a job (instance of the workflow) and schedules it for execution. A unique id is assigned to each job which can be used to query back the status.

GET /kronos/workflows/TestWorkflow/jobs

HEADERS namespace=cognitree

Response:

[
    {
        "id": "12cd27a7-9215-4ed9-ab3c-4dadd0857e75",
        "namespace": "cognitree",
        "workflow": "TestWorkflow",
        "trigger": "TestWorkflowTrigger",
        "status": RUNNING,
        "createdAt": 1534147440011
    }
]

The response is a list of all the jobs scheduled/executed in descending order of createdAt field.

  • Copy the unique id from the above response and execute to get the complete status of the job along with tasks status executed by the job.

GET /kronos/workflows/TestWorkflow/jobs/12cd27a7-9215-4ed9-ab3c-4dadd0857e75

HEADERS namespace=cognitree

Response:

{
    "id": "5b06933d-5e9a-43d6-9ec8-eb5d44817d7f",
    "namespace": "cognitree",
    "workflow": "workflow",
    "trigger": "TestWorkflowTrigger",
    "status": "SUCCESSFUL",
    "createdAt": 1534147440011,
    "completedAt": 1534147440017,
    "tasks": [
        {
            "name": "TaskOne",
            "jobId": "12cd27a7-9215-4ed9-ab3c-4dadd0857e75",
            "namespace": "cognitree",
            "type": "shellCommand",
            "maxExecutionTimeInMs": 86400,
            "dependsOn": [],
            "properties": {
                "args": "Hello World",
                "cmd": "echo",
                "logDir": "/tmp/",
                "workingDir": "/tmp/"
            },
            "status": "SUCCESSFUL",
            "createdAt": 1534147440011,
            "submittedAt": 1534147440014,
            "completedAt": 1534147440016
        }
    ]
}

Dependant Tasks in a Workflow

  • Update existing workflow TestWorkflow to define tasks with dependency.

PUT /kronos/workflows/TestWorkflow

HEADERS namespace=cognitree

{
    "name": "TestWorkflow",
    "namespace": "cognitree",
    "description": "sample workflow",
    "tasks": [
        {
            "name": "TaskOne",
            "type": "shellCommand",
            "properties": {
                "cmd": "echo",
                "args": "Hello World",
                "workingDir": "/tmp/",
                "logDir": "/tmp/"
            }
        },
        {
            "name": "TaskTwo",
            "type": "shellCommand",
            "properties": {
                "cmd": "sleep",
                "args": 120,
                "workingDir": "/tmp/",
                "logDir": "/tmp/"
            }
        },
        {
            "name": "TaskThree",
            "type": "shellCommand",
            "properties": {
                "cmd": "echo",
                "args": "Hello World from TaskThree",
                "workingDir": "/tmp/",
                "logDir": "/tmp/"
            },
            "dependsOn": [
                "TaskTwo"
            ]
        }
    ]
}

Here, we update TestWorkflow with two more tasks namely TaskTwo and TaskThree. Also, there is a dependency added between TaskTwo and TaskThree where TaskThree depends on TaskTwo.

TaskThree will be scheduled for execution only if TaskTwo is scheduled and executed successfully.

Repeat the steps mentioned above to query the status of updated workflow

Kronos exposes a Swagger UI listing all the available API at http://localhost:8080.