Exemple #1
0
    return x + 1


# Define a custom exception_handler for a task like so:
def example_exception_handler(exception: Exception, job: Job) -> None:
    print(exception)
    print(job)
    job.set_failure_status(
        f"Failed to run task {job.type}. Reason: {exception}")


@worker.task(task_type="exception_task",
             exception_handler=example_exception_handler)
def exception_task():
    raise Exception("Oh no!")


# We can also add decorators to tasks.
# The order of the decorators will be as follows:
# Worker decorators -> Task decorators -> Task -> Task decorators -> Worker decorators
# Here is how:
@worker.task(task_type="decorator_task",
             before=[example_logging_task_decorator],
             after=[example_logging_task_decorator])
def decorator_task() -> Dict:
    return {"output": "Hello world, test!"}


if __name__ == "__main__":
    worker.work()
from loguru import logger
from pyzeebe import ZeebeWorker

from worker.decorators.log_decorator import log_decorator
from worker.tasks import example_task_router
from worker.utils.config import get_credentials, get_zeebe_config

logger.add(sys.stderr,
           format="{time} {level} {message}",
           filter="root",
           level="INFO")

config = ConfigParser()
config.read(os.getenv("CONFIG_FILE_LOCATION") or "/src/config/worker.ini")

credentials = get_credentials(config)
zeebe_config = get_zeebe_config(config)

zeebe_worker = ZeebeWorker(credentials=credentials, **zeebe_config)

zeebe_worker.before(log_decorator)
zeebe_worker.after(log_decorator)

zeebe_worker.include_router(example_task_router)

if __name__ == '__main__':
    logger.info(
        f"Connecting to gateway at address: {zeebe_worker.zeebe_adapter.connection_uri}"
    )
    zeebe_worker.work()
Exemple #3
0
# Define a custom exception_handler for a task like so:
async def example_exception_handler(exception: Exception, job: Job) -> None:
    print(exception)
    print(job)
    await job.set_failure_status(
        f"Failed to run task {job.type}. Reason: {exception}")


@worker.task(task_type="exception_task",
             exception_handler=example_exception_handler)
async def exception_task():
    raise Exception("Oh no!")


# We can also add decorators to tasks.
# The order of the decorators will be as follows:
# Worker decorators -> Task decorators -> Task -> Task decorators -> Worker decorators
# Here is how:
@worker.task(
    task_type="decorator_task",
    before=[example_logging_task_decorator],
    after=[example_logging_task_decorator],
)
async def decorator_task() -> Dict:
    return {"output": "Hello world, test!"}


if __name__ == "__main__":
    loop = asyncio.get_running_loop()
    loop.run_until_complete(worker.work())
def start_worker(event_loop: asyncio.AbstractEventLoop,
                 zeebe_worker: ZeebeWorker):
    event_loop.create_task(zeebe_worker.work())
    yield
    event_loop.create_task(zeebe_worker.stop())