Skip to content

Commit

Permalink
feat(video): commands vs script
Browse files Browse the repository at this point in the history
  • Loading branch information
wrussell1999 committed Jan 9, 2025
1 parent 962770f commit 7dbb798
Showing 1 changed file with 124 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ icon: /docs/icons/dev.svg

Types of tasks for executing programming languages.

<div>
<iframe src="https://www.youtube.com/embed/l-1MDlhV2oM?si=A--h-VEvmVNWW1TW" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div>

---

For each of the [supported languages](./00.languages.md) (e.g. Python, R, Node.js, Shell), Kestra provides two types of tasks: Script and Commands.

1. The `Script` tasks — typically written **inline** in the YAML flow configuration. They are best suited for short scripts and they make it easy to pass data from flow inputs and other tasks to your custom scripts.
2. The `Commands` tasks — best suited for more complex and longer scripts, typically integrated into kestra as [namespace files](../../../05.concepts/02.namespace-files.md).
2. The `Commands` tasks — best suited for more complex and longer scripts, typically integrated into Kestra as [namespace files](../../../05.concepts/02.namespace-files.md).


The following table gives an overview of all script tasks and their configuration.
Expand Down Expand Up @@ -56,3 +62,120 @@ However, the `Script` tasks also have some **disadvantages** as compared to the
- **Complex use cases**: if your business logic comprises multiple files importing classes and functions from other modules, you will need to use the `Commands` tasks instead.

Overall, we recommend using `Commands` tasks for advanced production workloads. However, the `Script` tasks offer a great alternative for simple use cases.

## Examples

Here's an example comparing the Python `Script` and `Commands` task using the same Python Code:

Script Task:
```yaml
id: python_script
namespace: company.team

tasks:
- id: python
type: io.kestra.plugin.scripts.python.Script
beforeCommands:
- pip install requests kestra
script: |
from kestra import Kestra
import requests
response = requests.get('https://kestra.io')
print(response.status_code)
Kestra.outputs({'status': response.status_code, 'text': response.text})
```
Commands Task:
```yaml
id: python_commands
namespace: company.team

tasks:
- id: python
type: io.kestra.plugin.scripts.python.Commands
namespaceFiles:
enabled: true
include:
- main.py
beforeCommands:
- pip install requests kestra
commands:
- python main.py
```
`main.py` file:
```python
from kestra import Kestra
import requests
response = requests.get('https://kestra.io')
print(response.status_code)
Kestra.outputs({'status': response.status_code, 'text': response.text})
```

You can pass values into your code too using expressions. We can put the expression directly inside of the `Script` task:

```yaml
id: python_script_dynamic
namespace: company.team
inputs:
- id: uri
type: STRING
defaults: https://kestra.io
tasks:
- id: python
type: io.kestra.plugin.scripts.python.Script
beforeCommands:
- pip install requests kestra
script: |
from kestra import Kestra
import requests
response = requests.get('{{ inputs.uri }}')
print(response.status_code)
Kestra.outputs({'status': response.status_code, 'text': response.text})
```

We can also pass values into `Commands` tasks by using an environment variable:

```yaml
id: python_commands_dynamic
namespace: company.team
inputs:
- id: uri
type: STRING
defaults: https://kestra.io
tasks:
- id: python
type: io.kestra.plugin.scripts.python.Commands
namespaceFiles:
enabled: true
include:
- main.py
beforeCommands:
- pip install requests kestra
commands:
- python main.py
env:
URI: "{{ inputs.uri }}"
```

`main.py` file:
```python
from kestra import Kestra
import requests
import os
response = requests.get(os.environ['URI'])
print(response.status_code)
Kestra.outputs({'status': response.status_code, 'text': response.text})
```

0 comments on commit 7dbb798

Please sign in to comment.