def schedule(ident, minute='*', hour='*', day_of_week='*', day_of_month='*', month_of_year='*'): '''Schedule an harvesting on a source given a crontab''' source = get_source(ident) if source.periodic_task: raise ValueError('Source {0} is already scheduled'.format(source.name)) source.periodic_task = PeriodicTask.objects.create( task='harvest', name='Harvest {0}'.format(source.name), description='Periodic Harvesting', enabled=True, args=[str(source.id)], crontab=PeriodicTask.Crontab(minute=str(minute), hour=str(hour), day_of_week=str(day_of_week), day_of_month=str(day_of_month), month_of_year=str(month_of_year)), ) source.save() signals.harvest_source_scheduled.send(source) return source
def schedule(ident, cron=None, minute='*', hour='*', day_of_week='*', day_of_month='*', month_of_year='*'): '''Schedule an harvesting on a source given a crontab''' source = get_source(ident) if cron: minute, hour, day_of_month, month_of_year, day_of_week = cron.split() crontab = PeriodicTask.Crontab(minute=str(minute), hour=str(hour), day_of_week=str(day_of_week), day_of_month=str(day_of_month), month_of_year=str(month_of_year)) if source.periodic_task: source.periodic_task.modify(crontab=crontab) else: source.modify(periodic_task=PeriodicTask.objects.create( task='harvest', name='Harvest {0}'.format(source.name), description='Periodic Harvesting', enabled=True, args=[str(source.id)], crontab=crontab, )) signals.harvest_source_scheduled.send(source) return source
def test_unschedule(self): periodic_task = PeriodicTask.objects.create( task='harvest', name=faker.name(), description=faker.sentence(), enabled=True, crontab=PeriodicTask.Crontab()) source = HarvestSourceFactory(periodic_task=periodic_task) with assert_emit(signals.harvest_source_unscheduled): actions.unschedule(str(source.id)) source.reload() self.assertEqual(len(PeriodicTask.objects), 0) self.assertIsNone(source.periodic_task)
def test_unschedule_source_is_admin_only(self, api): '''It should only allow admins to unschedule a source''' api.login() periodic_task = PeriodicTask.objects.create( task='harvest', name=faker.name(), description=faker.sentence(), enabled=True, crontab=PeriodicTask.Crontab()) source = HarvestSourceFactory(periodic_task=periodic_task) url = url_for('api.schedule_harvest_source', ident=str(source.id)) response = api.delete(url) assert403(response) source.reload() assert source.periodic_task is not None
def test_unschedule_source(self): '''It should allow to unschedule a source if admin''' self.login(AdminFactory()) periodic_task = PeriodicTask.objects.create( task='harvest', name=faker.name(), description=faker.sentence(), enabled=True, crontab=PeriodicTask.Crontab()) source = HarvestSourceFactory(periodic_task=periodic_task) url = url_for('api.schedule_harvest_source', ident=str(source.id)) response = self.delete(url) self.assert204(response) source.reload() self.assertIsNone(source.periodic_task)
def test_purge_sources(self): periodic_task = PeriodicTask.objects.create( task='harvest', name=faker.name(), description=faker.sentence(), enabled=True, crontab=PeriodicTask.Crontab() ) now = datetime.now() to_delete = HarvestSourceFactory.create_batch(2, deleted=now) to_delete.append( HarvestSourceFactory(periodic_task=periodic_task, deleted=now) ) to_keep = HarvestSourceFactory.create_batch(2) harvest_job = HarvestJobFactory(source=to_delete[0]) result = actions.purge_sources() assert result == len(to_delete) assert len(HarvestSource.objects) == len(to_keep) assert PeriodicTask.objects.filter(id=periodic_task.id).count() == 0 assert HarvestJob.objects(id=harvest_job.id).count() == 0