Passed
Push — develop ( 58c12a...6a842a )
by Dean
02:43
created

BackupManager.maintenance()   B

Complexity

Conditions 3

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 8.8593
Metric Value
cc 3
dl 0
loc 28
ccs 2
cts 15
cp 0.1333
crap 8.8593
rs 8.8571
1 1
from plugin.core.backup.maintenance import BackupMaintenanceManager
2 1
from plugin.core.backup.sources import DatabaseBackupSource
3 1
from plugin.core.helpers.thread import spawn
4
5 1
from threading import Lock
6 1
import logging
7
8 1
log = logging.getLogger(__name__)
9
10
11 1
class BackupManager(object):
12 1
    database = DatabaseBackupSource
13
14 1
    maintenance_lock = Lock()
15
16 1
    @classmethod
17 1
    def maintenance(cls, block=True):
18
        """Ensures number of backups in each group matches retention policy, runs consolidation
19
           deletion, and archival tasks if the policy has been exceeded.
20
21
        :param block: Block execution until maintenance is complete
22
        :type block: bool
23
        """
24
25
        if not block:
26
            return spawn(cls.maintenance, block=True)
27
28
        if not cls.maintenance_lock.acquire(False):
29
            log.debug('Backup maintenance already running')
30
            return
31
32
        log.info('Starting backup maintenance...')
33
34
        try:
35
            # Run policy maintenance tasks
36
            maintenance = BackupMaintenanceManager()
37
            maintenance.run()
38
        except Exception, ex:
39
            log.error('Exception raised during backup maintenance: %s', ex, exc_info=True)
40
        finally:
41
            cls.maintenance_lock.release()
42
43
        log.info('Backup maintenance complete')
44