Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: task runner enabler with huey #1422

Merged
merged 17 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Check out the online documentation on <https://intuitem.gitbook.io/ciso-assistan
62. Mindeststandard-des-BSI-zur-Nutzung-externer-Cloud-Dienste (Version 2.1) 🇩🇪
63. Formulaire d'évaluation de la maturité - niveau fondamental (DGA) 🇫🇷
64. NIS2 technical and methodological requirements 2024/2690 🇪🇺
65. Saudi Arabian Monetary Authority (SAMA) Cybersecurity Framework 🇸🇦
65. Saudi Arabian Monetary Authority (SAMA) Cybersecurity Framework 🇸🇦
66. Guide de sécurité des données (CNIL) 🇫🇷
67. International Traffic in Arms Regulations (ITAR) 🇺🇸
68. Federal Trade Commission (FTC) Standards for Safeguarding Customer Information 🇺🇸
Expand Down Expand Up @@ -282,6 +282,7 @@ For docker setup on a remote server or hypervisor, checkout the [specific instru
- npm 10.2+
- pnpm 9.0+
- yaml-cpp (brew install yaml-cpp libyaml or apt install libyaml-cpp-dev)
- redis 5+

### Running the backend

Expand Down Expand Up @@ -407,6 +408,12 @@ ln -fs ../../git_hooks/post-commit .
ln -fs ../../git_hooks/post-merge .
```

11. for Huey (tasks runner)

- run redis as a broker `docker run -d -p 6379:6379 redis:alpine`. This should make it available on localhost.
- run `python manage.py run_huey -w 2 -k process` or equivalent in a separate shell.


### Running the frontend

1. cd into the frontend directory
Expand Down Expand Up @@ -551,6 +558,6 @@ See [LICENSE.md](./LICENSE.md) for details. For more details about the commercia

Unless otherwise noted, all files are © intuitem.

## Activity
## Activity

![Alt](https://repobeats.axiom.co/api/embed/83162c6044da29efd7efa28f746b6bee5a3c6a8a.svg "Repobeats analytics image")
24 changes: 13 additions & 11 deletions backend/ciso_assistant/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def set_ciso_assistant_url(_, __, event_dict):
"allauth.socialaccount",
"allauth.socialaccount.providers.saml",
"allauth.mfa",
# "huey.contrib.djhuey",
"huey.contrib.djhuey",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -378,16 +378,6 @@ def set_ciso_assistant_url(_, __, event_dict):
# OTHER SETTINGS
}

# HUEY = {
# "huey_class": "huey.SqliteHuey", # Huey implementation to use.
# "name": "huey-ciso-assistant", # Use db name for huey.
# "results": True, # Store return values of tasks.
# "store_none": False, # If a task returns None, do not save to results.
# "immediate": DEBUG, # If DEBUG=True, run synchronously.
# "utc": True, # Use UTC for all times internally.
# "filename": "db/huey.sqlite3",
# }

# SSO with allauth

ACCOUNT_USER_MODEL_USERNAME_FIELD = None
Expand Down Expand Up @@ -421,3 +411,15 @@ def set_ciso_assistant_url(_, __, event_dict):
"VERIFIED_EMAIL": True,
},
}

# for dev: docker run -d -p 6379:6379 redis:alpine
## Huey settings
HUEY = {
"huey_class": "huey.FileHuey",
# "huey_class": "huey.RedisHuey",
"name": "ciso_assistant",
"results": True,
"immediate": False, # True for local dev, set to False to run in "live" regardless of DEBUG
"path": BASE_DIR / "db" / "huey-tasks",
# "url": os.environ.get("REDIS_URL", "redis://localhost:6379/?db=1"),
}
ab-smith marked this conversation as resolved.
Show resolved Hide resolved
49 changes: 49 additions & 0 deletions backend/core/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from datetime import date, datetime, timezone
from huey import crontab
from huey.contrib.djhuey import periodic_task, task, db_periodic_task, db_task
from core.models import AppliedControl
from django.db.models import Q


# basic placeholders from the official doc
# https://huey.readthedocs.io/en/latest/django.html
#
@task()
def count_beans(number):
print("-- counted %s beans --" % number)
return "Counted %s beans" % number
ab-smith marked this conversation as resolved.
Show resolved Hide resolved


@task()
def send_email():
print("sending an email")
return "email sent"
ab-smith marked this conversation as resolved.
Show resolved Hide resolved


@periodic_task(crontab(minute="*/1"))
def every_min():
print("Every five minutes this will be printed by the consumer")


@db_task()
def do_some_queries():
# This task executes queries. Once the task finishes, the connection
# will be closed.
pass
ab-smith marked this conversation as resolved.
Show resolved Hide resolved


@db_periodic_task(crontab(minute="*/5"))
def every_five_mins_db():
print("Every five minutes this will be printed by the consumer")


# unrralistic, just for testing
@db_periodic_task(crontab(minute="*/1"))
def check_controls_with_expired_eta():
expired_eta_controls = AppliedControl.objects.exclude(status="active").filter(
eta__lt=date.today(), eta__isnull=False
)
print(f"Found {expired_eta_controls.count()} expired controls")
for ac in expired_eta_controls:
print(":: ", ac.name)
send_email()
ab-smith marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 33 additions & 1 deletion backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ python-docx = "^1.1.2"
docxtpl = "^0.19.0"
numpy = "^2.1.3"
matplotlib = "^3.9.3"
redis = "^5.2.1"

[tool.poetry.group.dev.dependencies]
pytest-django = "4.8.0"
Expand Down
Loading