Commit 48bef770 authored by Elia Ponzio's avatar Elia Ponzio
Browse files

Updates to tasks

parent a88d5ec7
from celery import shared_task
import subprocess
from django.utils import timezone
import requests
from django_celery_beat.models import PeriodicTask
from cynomys.models import Delta
@shared_task
def ping(host, timeout, **kwargs):
def check_ping(host, timeout, **kwargs):
"""This Check will ping a target. You are required to pass the task_id within the kwargs.
Variables: host (Domain or IP) and timeout (Seconds).
"""
response = subprocess.run(["ping", "-c", "1", "-w", timeout, host])
print("ping from " + host + " with return value: " + str(response.returncode))
......@@ -14,6 +20,8 @@ def ping(host, timeout, **kwargs):
print("the parent task: " + parent_task.name)
## Figure out if a delta occured, if not, update the last check time
if response.returncode == 0:
return_value = "Success!"
obj, created = Delta.objects.get_or_create(task=parent_task, success=True,
......@@ -38,3 +46,45 @@ def ping(host, timeout, **kwargs):
print("Updated delta for " + parent_task.name)
@shared_task
def check_http(host, timeout, httpcode, **kwargs):
"""This Check will verify a HTTP endpoint. You are required to pass the task_id within the kwargs.
Variables: host (Domain or IP) and timeout (Seconds), httpcode (Expected response).
"""
parent_task = PeriodicTask.objects.get(id=kwargs['task_id'])
print("the parent task: " + parent_task.name)
## Figure out if a delta occured, if not, update the last check time
if response.returncode == 0:
return_value = "Success!"
obj, created = Delta.objects.get_or_create(task=parent_task, success=True,
return_value=return_value)
if obj.success:
obj.save()
else:
obj.success = True
obj.save()
else:
return_value = "Failure to Ping!"
obj, created = Delta.objects.get_or_create(task=parent_task, success=False,
return_value=return_value)
if not obj.success:
obj.save()
else:
obj.success = False
obj.save()
print("Updated delta for " + parent_task.name)
## TODO
#@shared_task
# def l4_check():
# pass
......@@ -25,15 +25,6 @@ def debug_task(self):
print(f'Request: {self.request!r}')
@app.task()
def helloworld():
print("Oh Hai world!")
@app.task()
def http_check():
pass
@app.task()
def l4_check():
pass
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment