Skip to content

Commit

Permalink
A bit of a tidy up of the code and args.
Browse files Browse the repository at this point in the history
  • Loading branch information
mberacochea committed Jun 25, 2023
1 parent 4ba1ec3 commit 0b6b526
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 50 deletions.
62 changes: 21 additions & 41 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,30 @@
name: Upload Python Package
name: Build toad Pyinstaller

on:
push:
# Sequence of patterns matched against refs/tags
tags:
- '*' # Push events to matching v*, i.e. v1.0, v20.15.10

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
branches: [main]

jobs:
release:
name: Create Release
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
with:
# by default, it uses a depth of 1
# this fetches all history so that we can read each commit
fetch-depth: 0
- name: Generate Changelog
run: .github/release_message.sh > release_message.md
- name: Release
uses: softprops/action-gh-release@v1
with:
body_path: release_message.md
build:
runs-on: ubuntu-20.04
container: python:3.9-buster

deploy:
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
- name: Check out repository code
uses: actions/checkout@v3

- name: Setup python
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
python3 -m venv venv
. venv/bin/activate
- name: Create bundle
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
pip install -r requirements-dev.txt
pyinstaller mjobs/main.py --onefile --clean --name toad
- name: Publish artifact
uses: actions/upload-artifact@v3
with:
name: toad
path: dist/toad
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.mypy_cache/
__pycache__/
*.pyc
*.pyc

build/
dist/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ A very simple task booking system. It's just sql, jinja2 and python.

You can download a binary, generated with [pyinstaller](https://pyinstaller.org/) from the releases tab.

## Toad cli commands

![The tool provides several sub-commands](docs/img/toad_cli.png)

## Usage

The first step is to create a database and populate it with the `entries`.
Expand Down
Binary file added docs/img/toad_cli.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion toad/VERSION

This file was deleted.

49 changes: 42 additions & 7 deletions toad/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from enum import Enum
from pathlib import Path
from typing import Callable
from typing import Callable, Optional

import typer
from rich.console import Console
Expand All @@ -17,6 +17,13 @@
app = typer.Typer()


__version__ = "0.1.0"

def version_callback(value: bool):
if value:
print(f"Toad Version: {__version__}")
raise typer.Exit()

def create_database(db: str) -> Engine:
"""
Create the database and tables.
Expand All @@ -34,6 +41,7 @@ def create_database(db: str) -> Engine:

@app.command()
def update_template(database: str, template: str):
"""Update any given template, the template file name is going to be used to identify it"""
engine = create_database(database)
with Session(engine) as session:
with open(template, "r") as tfile:
Expand All @@ -51,6 +59,7 @@ def update_template(database: str, template: str):

@app.command()
def run(database: str, batch_size: int = 10):
"""Run a batch of tasks"""
engine = create_database(database)
with Session(engine) as session:
tasks: ScalarResult[Task] = session.exec(
Expand All @@ -69,6 +78,7 @@ def check(
str, typer.Option(help="Python script to check if tasks were completed.")
],
):
"""Run the check script on the running tasks."""
engine = create_database(database)
check_method: Callable = import_script(check_script, method_name="check")

Expand Down Expand Up @@ -102,7 +112,12 @@ class SummaryType(str, Enum):


@app.command(help="Get a summary of the number of tasks per status")
def summary(database: str, format: SummaryType = SummaryType.table):
def summary(
database: str,
format: Annotated[
SummaryType, typer.Option(help="Fancy or good'ol tsv.")
] = SummaryType.table,
):
"""Get a summary of the number of tasks per status"""
engine = create_database(database)
with Session(engine) as session:
Expand All @@ -126,11 +141,21 @@ def summary(database: str, format: SummaryType = SummaryType.table):

@app.command(
help=(
"Run continuously, checking running jobs "
"Run continuously, checking running tasks "
"and triggering new ones until there are no more pending tasks."
)
)
def daemon(database: str, max_jobs: int, check_script: str, minutes: int = 30):
def daemon(database: str,
check_script: Annotated[
str, typer.Option("--check", "-c", help="The check script path.")
],
max_tasks: Annotated[
int, typer.Option("--max-tasks", "-m", help="Max number of 'running' tasks.")
] = 25,
frequency: Annotated[
int, typer.Option("--frequency", "-f", help="Check frequency in minutes.")
] = 30
):
engine = create_database(database)
while True:
print("Starting.")
Expand All @@ -152,7 +177,7 @@ def daemon(database: str, max_jobs: int, check_script: str, minutes: int = 30):
print(f"There are {running_count} tasks running.")

# Calculate the number of available slots for new jobs
available_slots = max_jobs - running_count
available_slots = max_tasks - running_count

if available_slots > 0:
print(f"There is room for {available_slots} more.")
Expand All @@ -166,7 +191,7 @@ def daemon(database: str, max_jobs: int, check_script: str, minutes: int = 30):
for pending_task in pending_tasks:
pending_task.run()
session.add(pending_task)
print(f"Task for {pending_task.entry.name} running.")
print(f"Task {pending_task.id}:{pending_task.entry.name} running.")
session.commit()

# Get the count of remaining pending tasks
Expand All @@ -184,7 +209,7 @@ def daemon(database: str, max_jobs: int, check_script: str, minutes: int = 30):
print("No more tasks to run.")
break

time.sleep(60 * minutes) # Sleep for {minutes}
time.sleep(60 * frequency) # Sleep for {frequency} minutes


def empty_list() -> list:
Expand Down Expand Up @@ -233,6 +258,16 @@ def init(
session.add(task)
session.commit()

@app.callback()
def common(
ctx: typer.Context,
version: Annotated[
Optional[bool],
typer.Option("--version", callback=version_callback, is_eager=True),
] = None,
):
raise typer.Exit()


if __name__ == "__main__":
app()

0 comments on commit 0b6b526

Please sign in to comment.