Skip to content

Commit

Permalink
[CveXplore-278] finished all cli commands towards the backend for tas…
Browse files Browse the repository at this point in the history
…ks (redis); fixes #280
  • Loading branch information
P-T-I committed Apr 19, 2024
1 parent aafd5b8 commit c2126ec
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 7 deletions.
23 changes: 23 additions & 0 deletions CveXplore/celery_app/cvexplore_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,26 @@ def crt_test(task_slug: str, *args, **kwargs):
return {"status": task_status_codes.OK}
else:
return {"status": task_status_codes.NOK}


@app.task(
autoretry_for=(Exception,),
max_retries=5,
retry_backoff=True,
retry_backoff_max=700,
retry_jitter=True,
ignore_result=True,
)
def crt_atest(task_slug: str, *args, **kwargs):
"""General test task with A random response"""
import random

logger = get_task_logger(__name__)
logger.info("Testing with OK / NOK response")

a = random.randrange(20)

if a % 2 == 0:
return {"status": task_status_codes.OK}
else:
return {"status": task_status_codes.NOK}
71 changes: 71 additions & 0 deletions CveXplore/cli_cmds/tasks_cmds/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
from tabulate import tabulate

from CveXplore.cli_cmds.mutex_options.mutex import Mutex
from CveXplore.core.general.constants import task_status_rev_types
from CveXplore.core.general.utils import (
datetimeToTimestring,
Expand Down Expand Up @@ -161,3 +162,73 @@ def scheduled_cmd(ctx, list, delete, toggle, purge, results):
else:
if ctx.invoked_subcommand is None:
click.echo(tasks_cmd.get_help(ctx))


@tasks_cmd.group(
"create",
invoke_without_command=True,
help="Perform action on tasks that are currently scheduled.",
)
@click.option(
"-n",
"--number",
type=int,
required=True,
help="Create new task of type referred to by task id (id taken from 'cvexplore tasks list' command).",
)
@click.option(
"-s",
"--slug",
required=True,
help="Slug of the task to create.",
)
@click.option(
"-i",
"--interval",
help="Use interval as tasks schedule; interval is in seconds.",
type=int,
cls=Mutex,
not_required_if=["crontab"],
)
@click.option(
"-c",
"--crontab",
help="Use crontab as tasks schedule; use csv value in format 'minute,hour,day_of_week,day_of_month,month_of_year'"
"The crontab entry will be processed from left to right (from minute to month_of_year) with a '*' as a "
"default entry. That means that is you only need to set the minute value to every minute -c */1 will suffice.",
cls=Mutex,
not_required_if=["interval"],
)
@click.pass_context
def scheduled_cmd(ctx, number, slug, interval, crontab):
if interval:
click.echo(ctx.obj["data_source"].task_handler.create_task_by_number(
task_number=number, task_slug=slug, task_interval=interval
))
elif crontab:

the_crontab = {
"minute": "*",
"hour": "*",
"day_of_week": "*",
"day_of_month": "*",
"month_of_year": "*",
}

crontab_list = crontab.split(",")
try:
i = 0
for each in crontab_list:
the_crontab[list(the_crontab.keys())[i]] = each
i += 1

click.echo(ctx.obj["data_source"].task_handler.create_task_by_number(
task_number=number, task_slug=slug, task_crontab=the_crontab
))
except Exception as err:
click.echo(
f"Could not insert crontab; error: {err}",
)
else:
if ctx.invoked_subcommand is None:
click.echo(tasks_cmd.get_help(ctx))
37 changes: 34 additions & 3 deletions CveXplore/core/celery_task_handler/task_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ast
import collections
import inspect
import json
import logging
Expand Down Expand Up @@ -177,7 +178,9 @@ def purge_task_results(self) -> bool:
self.total_run_count = 0
self.upsert_task()

self.redis_backend.unlink(*self.redis_backend.keys(f"runresult_{self.name}*"))
all_run_results = self.redis_backend.keys(f"runresult_{self.name}*")
if len(all_run_results) != 0:
self.redis_backend.unlink(*all_run_results)

self.redis_backend.unlink(f"sortresults_{self.name}")

Expand Down Expand Up @@ -422,8 +425,36 @@ def __init__(self):

self.logger = logging.getLogger(__name__)

def show_available_tasks(self):
return task_descriptions
def show_available_tasks(self) -> collections.OrderedDict:

ordered_dict = collections.OrderedDict()

sorted_tasks = sorted(task_descriptions.keys())

for each in sorted_tasks:
ordered_dict[each] = task_descriptions[each]

return ordered_dict

def create_task_by_number(
self,
task_number: int,
task_slug: str,
task_interval: int = None,
task_crontab: dict = None,
):
all_tasks = self.show_available_tasks()
try:
task_name = list(all_tasks.keys())[task_number - 1]

return self.schedule_task(
task_slug=task_slug,
task_name=task_name,
task_interval=task_interval,
task_crontab=task_crontab,
)
except IndexError:
return "Task number not found; did you specify the correct task number?"

def show_scheduled_tasks(self) -> list[Task]:
tasks = self.redis.zrange("redbeat::schedule", 0, -1, withscores=True)
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Both of them can be easily created on a physical machine or via a docker instanc
please check `cve-search <https://github.com/cve-search/cve-search>`_ or
`CVE-Search-Docker <https://github.com/cve-search/CVE-Search-Docker>`_ for further details.

.. _basic installation:

Installation
------------
Package is hosted on pypi, so to install the minimal core just run:
Expand Down
3 changes: 3 additions & 0 deletions docs/backend/installation.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Installation
------------

The backend assumes that the CveXplore package is installed; check the package :ref:`installation <index:installation>`
paragraph.
2 changes: 0 additions & 2 deletions docs/cli/general.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.. _cli:

General
-------

Expand Down
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
"sphinx.ext.napoleon",
"sphinx.ext.viewcode",
"sphinx.ext.intersphinx",
"sphinx.ext.autosectionlabel",
]

default_role = "any"
autosectionlabel_prefix_document = True

intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
Expand Down
2 changes: 0 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.. _index:

Welcome to CveXplore's documentation!
=====================================

Expand Down

0 comments on commit c2126ec

Please sign in to comment.