Skip to main content

Python client to interract with Karya; the open sourced distributed job scheduler

Project description

Karya Python Client

This here is the Python client to interract with Karya - the open sourced distributed job scheduler


Getting Started

This section highlights the steps to get started with the Karya Python client.

Installation

    pip install karya-client

The distribution files can also be found here - Github Release.

Useage Examples

A list of samples to configure different plans with various actions and hooks can be found here

Using the Client

Do refer to the Client API Documentation to understand the various methods and classes provided by the client.

  1. Create a config object:

    from karya.clients.config import ClientConfig
    from karya.entities.enums import Protocol
    
    ## point this to where the Karya server is running
    config = ClientConfig(
        protocol=Portocol.HTTP,
        host='localhost',
        port=8080
    )
    
    ## For localsetup, a default config is provided as: ClientConfig.dev()
    
  2. Create a client object:

    from karya.clients import KaryaRestClient
    
    client = KaryaRestClient(config)
    
  3. Creat a user. Only a user configured in the Karya server can be used to create a client object.

    from karya.clients.requests import CreateUserRequest
    
    create_user_request = CreateUserRequest(name="python-client")
    user = await client.create_user(create_user_request)
    
  4. Specify the action that you would want to trigger once the task is scheduled.

    from karya.entities.actions import RestApiRequest
    
    ## For example, we shall be making a POST request to a local server
    action = RestApiRequest(
        protocol=Protocol.HTTPS,  # Use HTTPS for secure communication
        base_url="localhost",  # Base URL for the REST API
        method=Method.POST,  # HTTP method for the request (POST)
        headers={"content-type": "application/json"},  # Set the content type to JSON
        body=RestApiRequest.JsonBody.from_dict(
            {"message": "Hello from python client"}
        ),  # JSON body to send in the request
        timeout=2000,  # Timeout for the request (in milliseconds)
    )
    
  5. Submit the plan to Karya.

    period_time has to be in the ISO 8601 format.

    from karya.clients.requests SubmitPlanRequest
    from karya.entities.plan_types import Recurring
    
    # For example, we shall be submitting a recurring plan
    plan_request = SubmitPlanRequest(
        user_id=user.id,  # Use the created user's ID
        description="Make a recurring API call from python client",  # Description of the plan
        period_time="PT7S",  # Time period between each execution (7 seconds)
        max_failure_retry=3,  # Retry count in case of failure
        plan_type=Recurring(  # Define a recurring plan
            end_at=None,  # No specific end time, so it will continue indefinitely
        ),
        action=rest_action,  # The action to be executed as part of the plan (REST API call)
    )
    
    plan = await client.submit_plan(plan_request)
    
  6. And you're done! The plan will be executed as per the schedule:

    • The action will be triggered every 7 seconds.
    • The action will make a POST request to localhost with the JSON body {"message": "Hello from python client"}
    • The request will have a timeout of 2 seconds.

Plan Type

Karya supports the following plan types:

One Time

This can be used to trigger a delayed action.

from karya.entities.plan_types import OneTime

val plan_type = OneTime()

Recurring

This can be used to trigger an action periodically.

NOTE: If the endAt field is not specified, the plan will run indefinitely.

from karya.entities.plan_types import Recurring

plan_type = Recurring(end_at=1734694042) # define time in epoch-milli second

Actions

Actions define what Karya should do once it has to execute the plan. The client supports the following actions:

REST API Request

Make a REST API request to a specified URL with the given parameters.

    rest_action = RestApiRequest(
        protocol=Protocol.HTTPS,  # Use HTTPS for secure communication
        base_url="localhost",  # Base URL for the REST API
        method=Method.POST,  # HTTP method for the request (POST)
        headers={"content-type": "application/json"},  # Set the content type to JSON
        body=RestApiRequest.JsonBody.from_dict(
            {"message": "Hello from python client"}
        ),  # JSON body to send in the request
        timeout=2000,  # Timeout for the request (in milliseconds)
    )

Push to Kafka

Push a message to a Kafka topic.

    kafka_action = KafkaPush(
        topic="test-topic",  # Kafka topic to push the message to
        message="Hello from python client",  # Message to push to the Kafka topic
    )

Send Email

Send an email to a specified email address.

    email_action = EmailRequest(
        recipient="recipient@gmail.com",  # Email recipient
        subject="Karya notification",  # Email subject
        message="Hello from Karya!",  # Email message body
    )

Send a Slack Message

Send a message to a specified Slack channel.

    slack_action = SlackMessage(
        channel="test-channel",  # Slack channel to send the message to
        message="Hello from python client",  # Message to send to the Slack channel
    )

Chain another job

Chain another job to the current job.

    chained_action = ChainedRequest(
        request=SubmitPlanRequest(
            user_id=user.id,  # Use the created user's ID
            description="Make a recurring API call from python client",  # Plan description
            period_time="PT5S",  # Time period between calls (5 seconds)
            max_failure_retry=3,  # Retry count in case of failure
            plan_type=Recurring(end_at=None),  # Recurring plan with no end time
            action=RestApiRequest(
                base_url="eox7wbcodh9parh.m.pipedream.net"
            ),  # API request action
        )
    )

Hooks

API documentation

    hook = Hook(
        hook_type=HookType  # Hook type
        action=ActionType,  # Can be any of the actions specified above
    )

Hooks are used to trigger actions on certain triggers. The client supports the following hooks:

  • ON_FAILURE: Trigger an action when the plan fails.
  • ON_COMPLETION: Trigger an action when the plan completes successfully.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

karya_client-1.1.3.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

karya_client-1.1.3-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file karya_client-1.1.3.tar.gz.

File metadata

  • Download URL: karya_client-1.1.3.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.11 Linux/6.8.0-1017-azure

File hashes

Hashes for karya_client-1.1.3.tar.gz
Algorithm Hash digest
SHA256 0967717aa3715efd12ee1c74c098e62f35cbb07023db9c18b2a3abe0952eae98
MD5 0078a7136e9be9cda8e118f1072b35a0
BLAKE2b-256 36a8566067d85cf8dc12d6c5c25c7d2a5ed1afc6b93c499c3ab9b903aaabd943

See more details on using hashes here.

File details

Details for the file karya_client-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: karya_client-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.11 Linux/6.8.0-1017-azure

File hashes

Hashes for karya_client-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bbf94da670dfe3ddff4b1ad23508a07b6e74715779a73a2777323c76c13ff00f
MD5 47edc56c6568c53e8498f18c16bfdabd
BLAKE2b-256 d0ebcee1fde0b2f3b7d4c4f4989ba2edcd992a5bddc7d9bad5c39546db694e56

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page