Module: function_task

Expand source code
# Copyright (C) 2023-present The Project Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect
from dataclasses import dataclass
from typing import Callable
from typing import Type
from typing_extensions import Self
from cl.runtime.records.dataclasses_extensions import missing
from cl.runtime.tasks.callable_task import CallableTask
from cl.runtime.tasks.task_key import TaskKey
from cl.runtime.tasks.task_queue_key import TaskQueueKey


@dataclass(slots=True, kw_only=True)
class FunctionTask(CallableTask):
    """Invoke a function defined in the module directly, do not use for class methods."""

    module: str = missing()
    """Module string in dot-delimited format."""

    function_name: str = missing()
    """Function name in snake_case or PascalCase format."""

    def _execute(self) -> None:
        """Invoke the specified function."""
        raise NotImplementedError()

    @classmethod
    def create(
            cls,
            *,
            queue: TaskQueueKey,
            record_type: Type,
            method: Callable,
    ) -> Self:
        """Create from static or class handler method callable."""
        raise NotImplementedError()

Classes

class FunctionTask (*, task_id: str = None, label: str | None = None, queue: TaskQueueKey = None, status: TaskStatusEnum = None, progress_pct: float = None, elapsed_sec: float | None = None, remaining_sec: float | None = None, error_message: str | None = None, module: str = None, function_name: str = None)

Invoke a function defined in the module directly, do not use for class methods.

Expand source code
@dataclass(slots=True, kw_only=True)
class FunctionTask(CallableTask):
    """Invoke a function defined in the module directly, do not use for class methods."""

    module: str = missing()
    """Module string in dot-delimited format."""

    function_name: str = missing()
    """Function name in snake_case or PascalCase format."""

    def _execute(self) -> None:
        """Invoke the specified function."""
        raise NotImplementedError()

    @classmethod
    def create(
            cls,
            *,
            queue: TaskQueueKey,
            record_type: Type,
            method: Callable,
    ) -> Self:
        """Create from static or class handler method callable."""
        raise NotImplementedError()

Ancestors

Static methods

def create(*, queue: TaskQueueKey, record_type: Type, method: Callable) -> Self

Create from static or class handler method callable.

def get_key_type() -> Type

Inherited from: CallableTask.get_key_type

Return key type even when called from a record.

def normalize_method_name(method_name: str) -> str

Inherited from: CallableTask.normalize_method_name

If method name has uppercase letters, assume it is PascalCase and convert to snake_case.

def wait_for_completion(task_key: TaskKey, timeout_sec: int = 10) -> None

Inherited from: CallableTask.wait_for_completion

Wait for completion of the specified task run before exiting from this method (not async/await).

Fields

var elapsed_sec -> float | None

Inherited from: CallableTask.elapsed_sec

Elapsed time in seconds if available.

var error_message -> str | None

Inherited from: CallableTask.error_message

Error message for Failed status if available.

var function_name -> str

Function name in snake_case or PascalCase format.

var label -> str | None

Inherited from: CallableTask.label

Label for information purposes only (should not be used in processing).

var module -> str

Module string in dot-delimited format.

var progress_pct -> float

Inherited from: CallableTask.progress_pct

Task progress in percent from 0 to 100.

var queue -> TaskQueueKey

Inherited from: CallableTask.queue

The queue that will run the task once it is saved.

var remaining_sec -> float | None

Inherited from: CallableTask.remaining_sec

Remaining time in seconds if available.

var status -> TaskStatusEnum

Inherited from: CallableTask.status

Begins from Pending, continues to Running or Paused, and ends with Completed, Failed, or Cancelled.

var task_id -> str

Inherited from: CallableTask.task_id

Unique task identifier.

Methods

def get_key(self) -> TaskKey

Inherited from: CallableTask.get_key

Return a new key object whose fields populated from self, do not return self.

def init_all(self) -> None

Inherited from: CallableTask.init_all

Invoke ‘init’ for each class in the order from base to derived, then validate against schema.

def run_task(self) -> None

Inherited from: CallableTask.run_task

Invoke execute with task status updates and exception handling.