Skip to content

Commit

Permalink
Fix up management commands
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSheps committed Oct 2, 2024
1 parent 27c4ed6 commit b96e53b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
19 changes: 14 additions & 5 deletions netbox_config_backup/jobs/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ def clean_stale_jobs(self):
job.data.update({'error': 'Process terminated'})
job.save()

def schedule_jobs(self):
backups = Backup.objects.filter(status=StatusChoices.STATUS_ACTIVE, device__isnull=False)
def schedule_jobs(self, backup=None, device=None):
if backup:
logging.debug(f'Scheduling backup for backup: {backup}')
backups = Backup.objects.filter(pk=backup.pk, status=StatusChoices.STATUS_ACTIVE, device__isnull=False)
elif device:
logging.debug(f'Scheduling backup for device: {device}')
backups = Backup.objects.filter(device=device, status=StatusChoices.STATUS_ACTIVE, device__isnull=False)
else:
logging.debug(f'Scheduling all backups')
backups = Backup.objects.filter(status=StatusChoices.STATUS_ACTIVE, device__isnull=False)

for backup in backups:
if can_backup(backup):
logger.debug(f'Queuing device {backup.device} for backup')
Expand Down Expand Up @@ -92,7 +101,7 @@ def schedule_jobs(self):
job.save()

def run_processes(self):
for job in BackupJob.objects.filter(status=JobStatusChoices.STATUS_SCHEDULED):
for job in BackupJob.objects.filter(runner=self.job, status=JobStatusChoices.STATUS_SCHEDULED):
try:
process = self.fork_process(job)
process.join(1)
Expand Down Expand Up @@ -134,10 +143,10 @@ def handle_processes(self):
job.data.update({'error': 'Process terminated'})
job.save()

def run(self, *args, **kwargs):
def run(self, backup=None, device=None, *args, **kwargs):
try:
self.clean_stale_jobs()
self.schedule_jobs()
self.schedule_jobs(backup=backup, device=device)
self.run_processes()
while(True):
self.handle_processes()
Expand Down
20 changes: 10 additions & 10 deletions netbox_config_backup/management/commands/runbackup.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import uuid

from django.core.management.base import BaseCommand
from django.db import transaction
from django.utils import timezone

from netbox_config_backup.models import BackupJob
from netbox_config_backup.utils.rq import can_backup
from netbox_config_backup.jobs.backup import BackupRunner
from netbox_config_backup.models import Backup


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--time', dest='time', help="time")
parser.add_argument('--device', dest='device', help="Device Name")

def run_backup(self, backup=None):
BackupRunner.enqueue(backup=backup, immediate=True)

def handle(self, *args, **options):
from netbox_config_backup.models import Backup
if options['device']:
print(f'Running:{options.get("device")}| ')
print(f'Running backup for: {options.get("device")}')
backup = Backup.objects.filter(device__name=options['device']).first()
if not backup:
backup = Backup.objects.filter(name=options['device']).first()
if backup:
self.run_backup(backup)
else:
raise Exception('Device not found')
else:
for backup in Backup.objects.all():
self.run_backup(backup)
pass
#self.run_backup()

0 comments on commit b96e53b

Please sign in to comment.