Skip to main content

Schedule async tasks and manage them using a REST API or WEB UI

Project description

aiocronjob

Join the chat at https://gitter.im/aiocronjob/community PyPI - Python Version PyPI PyPI - License GitHub last commit PyPI - Status

Schedule and run asyncio coroutines and manage them from a web interface or programmatically using the rest api.

Requires python >= 3.6

How to install

pip3 install aiocronjob

Usage example

# examples/simple_tasks.py

import asyncio

from aiocronjob import manager, Job
from aiocronjob import run_app


async def first_task():
    for i in range(20):
        print("first task log", i)
        await asyncio.sleep(1)


async def second_task():
    for i in range(10):
        await asyncio.sleep(1.5)
        print("second task log", i)
    raise Exception("second task exception")


manager.register(first_task, name="First task", crontab="22 * * * *")

manager.register(second_task, name="Second task", crontab="23 * * * *")


async def on_job_exception(job: Job, exc: BaseException):
    print(f"An exception occurred for job {job.name}: {exc}")


async def on_job_cancelled(job: Job):
    print(f"{job.name} was cancelled...")


async def on_startup():
    print("The app started.")


async def on_shutdown():
    print("The app stopped.")


manager.set_on_job_cancelled_callback(on_job_cancelled)
manager.set_on_job_exception_callback(on_job_exception)
manager.set_on_shutdown_callback(on_shutdown)
manager.set_on_startup_callback(on_startup)

if __name__ == "__main__":
    run_app()

After running the app, the FastAPI server runs at localhost:5000.

Web Interface

Open localhost:5000 in your browser:

screenshot-actionmenu screenshot-all

Rest API

Open localhost:5000/docs for endpoints docs.

EndpointsScreenshot

curl example:

$ curl http://0.0.0.0:5000/api/jobs
[
  {
    "name": "First task",
    "next_run_in": "3481.906931",
    "last_status": "pending",
    "enabled": "True",
    "crontab": "22 * * * *",
    "created_at": "2020-06-06T10:20:25.118630+00:00",
    "started_at": null,
    "stopped_at": null
  },
  {
    "name": "Second task",
    "next_run_in": "3541.904723",
    "last_status": "error",
    "enabled": "True",
    "crontab": "23 * * * *",
    "created_at": "2020-06-06T10:20:25.118661+00:00",
    "started_at": "2020-06-06T10:23:00.000906+00:00",
    "stopped_at": "2020-06-06T10:23:15.004351+00:00"
  }
]

Development

Requirements:

  • Python >= 3.6 and Poetry for backend
  • npm for frontend

The frontend is a separate Single Page Application (SPA), so the backend does not depend on it. It just calls the backend's API endpoints.

Install backend dependencies (Python)

$ git clone https://github.com/devtud/aiocronjob.git

$ cd aiocronjob

$ poetry install

Run backend tests

poetry run pytest --cov -s

Run backend example

poetry run python examples/simple_tasks.py

uvicorn will run the FastAPI app at http://localhost:5000.

Install frontend dependencies (React SPA)

Open another terminal tab in the project root.

$ cd src/webapp

$ npm i

Run frontend tests

npm test

Let frontend know about backend

Create .env file with the content from .env.example file to let the frontend know that the backend is running at http://localhost:5000.

cp .env.example .env

Serve frontend

npm start

A React app starts at http://localhost:3000.

You should now be able to view the example jobs in your browser at http://localhost:3000.

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

aiocronjob-0.2.6.tar.gz (746.4 kB view details)

Uploaded Source

Built Distribution

aiocronjob-0.2.6-py3-none-any.whl (1.5 MB view details)

Uploaded Python 3

File details

Details for the file aiocronjob-0.2.6.tar.gz.

File metadata

  • Download URL: aiocronjob-0.2.6.tar.gz
  • Upload date:
  • Size: 746.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.9.1 Linux/5.4.85-1-lts

File hashes

Hashes for aiocronjob-0.2.6.tar.gz
Algorithm Hash digest
SHA256 6623c786a2274ea82c9b5c5e1fe66ac70e49e306f5adf5165b9afcab5da3b38b
MD5 67068261e81ae2879769fb54b915f355
BLAKE2b-256 1883228fff1a7baa3e5aa60cf9febc988d3da3d5f4e96b4f13e9a67e94556213

See more details on using hashes here.

Provenance

File details

Details for the file aiocronjob-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: aiocronjob-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.9.1 Linux/5.4.85-1-lts

File hashes

Hashes for aiocronjob-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 96b13201363731211fd68dfb997d6ab4eb8c2662f38106cca0607bc5dd32e9da
MD5 ac3ab8230a1c919220f44800f0a5ba43
BLAKE2b-256 956a66c698b6e40cfa44930b469c2c272b022c53574f2df11b8f89d07deefe24

See more details on using hashes here.

Provenance

Supported by

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