Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
1. [#733](https://github.com/influxdata/influxdb-client-python/pull/733): Write the last batch when the batching `WriteApi` is closed in an `atexit` handler instead of dropping it with "cannot schedule new futures after interpreter shutdown".
1. [#734](https://github.com/influxdata/influxdb-client-python/pull/734): Match `data_frame_timestamp_column` by exact column name, so a field whose name is part of the timestamp column name (e.g. `Open` with `OpenTime`) is no longer dropped.
1. [#739](https://github.com/influxdata/influxdb-client-python/pull/739): Async CSV query cancellation no longer raises `AttributeError` with pre 3.11 release Python platforms.
1. [#741](https://github.com/influxdata/influxdb-client-python/pull/741): Escape `\`, `"` and `${` in the task name and cron of `TasksApi.create_task_every`/`create_task_cron`, so the generated Flux `option task` stays a valid string literal.

## 1.50.0 [2026-01-23]

Expand Down
10 changes: 8 additions & 2 deletions influxdb_client/client/tasks_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def create_task(self, task: Task = None, task_create_request: TaskCreateRequest

raise ValueError("task or task_create_request must be not None")

@staticmethod
def _escape_flux_string(value: str) -> str:
"""Escape a value to be used inside a Flux string literal."""
return value.replace('\\', '\\\\').replace('"', '\\"').replace('${', '\\${')

@staticmethod
def _create_task(name: str, flux: str, every, cron, org_id: str) -> Task:

Expand All @@ -76,9 +81,10 @@ def _create_task(name: str, flux: str, every, cron, org_id: str) -> Task:

if cron is not None:
repetition += "cron: "
repetition += '"' + cron + '"'
repetition += '"' + TasksApi._escape_flux_string(cron) + '"'

flux_with_options = '{} \n\noption task = {{name: "{}", {}}}'.format(flux, name, repetition)
escaped_name = TasksApi._escape_flux_string(name)
flux_with_options = '{} \n\noption task = {{name: "{}", {}}}'.format(flux, escaped_name, repetition)
task.flux = flux_with_options

return task
Expand Down
18 changes: 17 additions & 1 deletion tests/test_TasksApi.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import datetime
import time
import unittest

import pytest

from influxdb_client import PermissionResource, Permission, Task, InfluxDBClient
from influxdb_client import PermissionResource, Permission, Task, InfluxDBClient, TasksApi
from influxdb_client.rest import ApiException
from tests.base_test import BaseTest

TASK_FLUX = 'from(bucket: "my-bucket") |> range(start: -2m) |> last()'


class TasksApiFluxOptionTest(unittest.TestCase):

def test_create_task_escapes_name_and_cron(self):
task = TasksApi._create_task(name='my "task" \\ ${x}', flux=TASK_FLUX, every=None,
cron='0 2 * * * "', org_id="org")

self.assertEqual(TASK_FLUX + ' \n\noption task = {name: "my \\"task\\" \\\\ \\${x}", '
'cron: "0 2 * * * \\""}', task.flux)

def test_create_task_every_escapes_name(self):
task = TasksApi._create_task(name='a"b', flux=TASK_FLUX, every="1h", cron=None, org_id="org")

self.assertEqual(TASK_FLUX + ' \n\noption task = {name: "a\\"b", every: 1h}', task.flux)


class TasksApiTest(BaseTest):

def setUp(self) -> None:
Expand Down
Loading