1

2
# Django
3 0
from django.core.management.base import BaseCommand
4 0
from django.db import transaction
5

6
# CyBorgBackup
7 0
from cyborgbackup.main.models import Job, Repository
8

9

10 0
class Command(BaseCommand):
11
    '''
12
    Management command to clean orphan running jobs.
13
    '''
14

15 0
    help = 'Remove old jobs from the database.'
16

17 0
    def add_arguments(self, parser):
18 0
        parser.add_argument('--dry-run', dest='dry_run', action='store_true',
19
                            default=False, help='Dry run mode (show items that would '
20
                            'be removed)')
21

22 0
    def cleanup_jobs(self):
23
        # Sanity check: Is there already a running job on the System?
24 0
        jobs = Job.objects.filter(status="running")
25

26 0
        counter = 0
27 0
        if jobs.exists():
28 0
            for job in jobs:
29 0
                if not self.dry_run:
30 0
                    job.status = 'error'
31 0
                    job.save()
32 0
                counter += 1
33 0
        return 0, counter
34

35 0
    @transaction.atomic
36
    def handle(self, *args, **options):
37 0
        self.dry_run = bool(options.get('dry_run', False))
38

39 0
        model_names = ('jobs',)
40 0
        models_to_cleanup = set()
41 0
        for m in model_names:
42 0
            if options.get('only_%s' % m, False):
43 0
                models_to_cleanup.add(m)
44 0
        if not models_to_cleanup:
45 0
            models_to_cleanup.update(model_names)
46

47 0
        for m in model_names:
48 0
            if m in models_to_cleanup:
49 0
                skipped, updated = getattr(self, 'cleanup_%s' % m)()
50 0
                if self.dry_run:
51 0
                    print('{}: {} would be updated, {} would be skipped.'.format(m.replace('_', ' '),
52
                                    updated, skipped))
53
                else:
54 0
                    print('{}: {} updated, {} skipped.'.format(m.replace('_', ' '), updated, skipped))

Read our documentation on viewing source code .

Loading