Module libnova.common.api.Job

Expand source code
#!/usr/bin/env python
# coding: utf-8

import datetime
from enum import Enum

from libnova.common     import api
from libnova.common.api import File, Function

# Define Enum structs
from libnova.common.api.Serializable import Serializable


class JobStatus(Enum):
    """Job Status
    """
    RUNNING   = 1
    COMPLETED = 2
    WAITING   = 3
    FAILED    = 4


class JobResult(Enum):
    """Job Result
    """
    OK              = 1
    OK_WITH_WARNING = 2
    ERROR           = 3


# Define main object structure
class Job(Serializable):
    """Job

    The main methods allows the interaction with the main structures related to jobs hosted in the platform
    """

    id:              int       = 0
    user_id:         int
    function_id:     int
    container_id:    int
    file_id:         int
    status:          JobStatus = 3
    result:          JobResult = 1
    last_heart_beat: datetime.datetime
    start_date:      datetime.datetime
    end_date:        datetime.datetime
    function_input:  str

#Define main functions as "static"


def create(function_id, container_id = None) -> Job:
    """Create a new job

    Returns:
        Job: A new `Job`
    """

    api_driver = api.Driver.get_instance()

    data = {}
    if container_id is not None:
        data["container_id"] = container_id

    return api_driver.serialize(
        api_driver.post(
            url_segment='job/' + str(function_id),
            data=data
        ),
        Job
    )


def get(id) -> Job:
    """Get an existing job

    Args:
        id: The id of the `Job` to retrieve

    Returns:
        Job: An existing `Job` if exists, None otherwise
    """

    api_driver = api.Driver.get_instance()

    return api_driver.serialize(
        api_driver.get(
            url_segment='job/' + str(id)
        ),
        Job
    )


def add_asset(job_id, file: File):
    """Relate a `File` with a `Job`

    Args:
        job_id (int): The `Job` id
        file (File): The `File` to relate to the `Job`

    Returns:
        object: The api result
    """

    api_driver = api.Driver.get_instance()
    return api_driver.post(
        url_segment='job/' + str(job_id) + '/asset/' + str(file.id)
    )


def set_status(job_id, status: JobStatus):
    """Update a job with a given `status`

    Args:
        job_id (int): The `Job` id
        status (str): The new `Job Status`

    Returns:
        Job: The updated `Job`
    """

    query = {
        "status": status.name
    }

    api_driver = api.Driver.get_instance()
    return api_driver.put_json(
        url_segment='job/' + job_id,
        data=query
    )


if __name__ == "__main__":
    print('This file cannot be executed directly!')

Functions

def add_asset(job_id, file: libnova.common.api.File)

Relate a File with a Job

Args

job_id : int
The Job id
file : File
The File to relate to the Job

Returns

object
The api result
Expand source code
def add_asset(job_id, file: File):
    """Relate a `File` with a `Job`

    Args:
        job_id (int): The `Job` id
        file (File): The `File` to relate to the `Job`

    Returns:
        object: The api result
    """

    api_driver = api.Driver.get_instance()
    return api_driver.post(
        url_segment='job/' + str(job_id) + '/asset/' + str(file.id)
    )
def create(function_id, container_id=None) ‑> Job

Create a new job

Returns

Job
A new Job
Expand source code
def create(function_id, container_id = None) -> Job:
    """Create a new job

    Returns:
        Job: A new `Job`
    """

    api_driver = api.Driver.get_instance()

    data = {}
    if container_id is not None:
        data["container_id"] = container_id

    return api_driver.serialize(
        api_driver.post(
            url_segment='job/' + str(function_id),
            data=data
        ),
        Job
    )
def get(id) ‑> Job

Get an existing job

Args

id
The id of the Job to retrieve

Returns

Job
An existing Job if exists, None otherwise
Expand source code
def get(id) -> Job:
    """Get an existing job

    Args:
        id: The id of the `Job` to retrieve

    Returns:
        Job: An existing `Job` if exists, None otherwise
    """

    api_driver = api.Driver.get_instance()

    return api_driver.serialize(
        api_driver.get(
            url_segment='job/' + str(id)
        ),
        Job
    )
def set_status(job_id, status: JobStatus)

Update a job with a given status

Args

job_id : int
The Job id
status : str
The new Job Status

Returns

Job
The updated Job
Expand source code
def set_status(job_id, status: JobStatus):
    """Update a job with a given `status`

    Args:
        job_id (int): The `Job` id
        status (str): The new `Job Status`

    Returns:
        Job: The updated `Job`
    """

    query = {
        "status": status.name
    }

    api_driver = api.Driver.get_instance()
    return api_driver.put_json(
        url_segment='job/' + job_id,
        data=query
    )

Classes

class Job (**entries: dict)

Job

The main methods allows the interaction with the main structures related to jobs hosted in the platform

Expand source code
class Job(Serializable):
    """Job

    The main methods allows the interaction with the main structures related to jobs hosted in the platform
    """

    id:              int       = 0
    user_id:         int
    function_id:     int
    container_id:    int
    file_id:         int
    status:          JobStatus = 3
    result:          JobResult = 1
    last_heart_beat: datetime.datetime
    start_date:      datetime.datetime
    end_date:        datetime.datetime
    function_input:  str

Ancestors

Class variables

var container_id : int
var end_date : datetime.datetime
var file_id : int
var function_id : int
var function_input : str
var id : int
var last_heart_beat : datetime.datetime
var resultJobResult
var start_date : datetime.datetime
var statusJobStatus
var user_id : int
class JobResult (value, names=None, *, module=None, qualname=None, type=None, start=1)

Job Result

Expand source code
class JobResult(Enum):
    """Job Result
    """
    OK              = 1
    OK_WITH_WARNING = 2
    ERROR           = 3

Ancestors

  • enum.Enum

Class variables

var ERROR
var OK
var OK_WITH_WARNING
class JobStatus (value, names=None, *, module=None, qualname=None, type=None, start=1)

Job Status

Expand source code
class JobStatus(Enum):
    """Job Status
    """
    RUNNING   = 1
    COMPLETED = 2
    WAITING   = 3
    FAILED    = 4

Ancestors

  • enum.Enum

Class variables

var COMPLETED
var FAILED
var RUNNING
var WAITING