Commit e3ab8f26 authored by Elia Ponzio's avatar Elia Ponzio
Browse files

finish ping task

parent 50d5e08e
from django.contrib import admin
from cynomys.models import Deltas
from cynomys.models import Delta
# Register your models here.
class DeltaAdmin(admin.ModelAdmin):
model = Deltas
model = Delta
list_display = ('task', 'success', 'timestamp', 'latest_check', 'return_value')
admin.site.register(Deltas, DeltaAdmin)
admin.site.register(Delta, DeltaAdmin)
# Generated by Django 3.2.4 on 2021-06-24 17:57
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('django_celery_beat', '0016_auto_20210624_1313'),
('cynomys', '0002_deltas_return_value'),
]
operations = [
migrations.CreateModel(
name='Delta',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('timestamp', models.DateTimeField(editable=False)),
('success', models.BooleanField(blank=True, null=True)),
('latest_check', models.DateTimeField()),
('return_value', models.CharField(blank=True, max_length=100)),
('task', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='django_celery_beat.periodictask')),
],
),
migrations.DeleteModel(
name='Deltas',
),
]
from django.db import models
from django_celery_beat.models import PeriodicTask
from django.utils import timezone
# Create your models here.
class Deltas(models.Model):
class Delta(models.Model):
task = models.ForeignKey(PeriodicTask, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(editable=False)
success = models.BooleanField(null=True, blank=True)
latest_check = models.DateTimeField(auto_now=True)
latest_check = models.DateTimeField()
return_value = models.CharField(max_length=100, blank=True)
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.timestamp = timezone.now()
self.latest_check = timezone.now()
return super(Delta, self).save(*args, **kwargs)
from celery import shared_task
from django_celery_beat.models import PeriodicTask
import subprocess
from django.utils import timezone
from django_celery_beat.models import PeriodicTask
from cynomys.models import Delta
@shared_task
def ping(host, timeout, **kwargs):
......@@ -12,5 +14,27 @@ def ping(host, timeout, **kwargs):
print("the parent task: " + parent_task.name)
if response.returncode:
pass
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)
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