| Total Complexity | 8 |
| Total Lines | 44 |
| Duplicated Lines | 0 % |
| Coverage | 17.39% |
| Changes | 0 | ||
| 1 | 1 | from plugin.models import SyncResult |
|
| 14 | 1 | class SyncIntervalHandler(Handler): |
|
| 15 | 1 | key = 'sync.interval' |
|
| 16 | |||
| 17 | 1 | def check(self, job): |
|
| 18 | last_result = SyncIntervalOption.get_last_result(job.account, SyncMode.Full) |
||
| 19 | |||
| 20 | if last_result is None or last_result.started_at <= job.due_at: |
||
| 21 | return True |
||
| 22 | |||
| 23 | # Re-schedule job |
||
| 24 | job.due_at = job.next_at(last_result.started_at) |
||
| 25 | |||
| 26 | log.debug('Job re-scheduled to %r', job.due_at) |
||
| 27 | |||
| 28 | # Check if a trigger is required |
||
| 29 | if job.due_at <= datetime.utcnow(): |
||
| 30 | return True |
||
| 31 | |||
| 32 | # Trigger ignored, update job |
||
| 33 | job.save() |
||
| 34 | |||
| 35 | log.debug('Ignoring scheduled sync interval (already triggered)') |
||
| 36 | return False |
||
| 37 | |||
| 38 | 1 | def run(self, job): |
|
| 39 | # Ensure sync hasn't already been triggered |
||
| 40 | if not self.check(job): |
||
| 41 | return False |
||
| 42 | |||
| 43 | try: |
||
| 44 | # Queue sync |
||
| 45 | Sync.queue( |
||
| 46 | account=job.account, |
||
| 47 | mode=SyncMode.Full, |
||
| 48 | |||
| 49 | priority=100, |
||
| 50 | trigger=SyncResult.Trigger.Schedule |
||
| 51 | ) |
||
| 52 | except QueueError, ex: |
||
| 53 | log.info('Queue error: %s', ex) |
||
| 54 | except Exception, ex: |
||
| 55 | log.error('Unable to queue sync: %s', ex, exc_info=True) |
||
| 56 | |||
| 57 | return True |
||
| 58 |