def handle(self, *args, **options): now = datetime.now() lock_time = now - settings.FSCAN_TDELTA['max'] orphan_proc = Scan.objects.filter( status=settings.SCAN_STATUS['in_process'], last_updated__lte=lock_time) for obj in orphan_proc: obj.unlock_task('find_scans call unlock task') for scan_task in ScanTask.objects.filter( status=settings.TASK_STATUS['free']): min_time_allowed = now - settings.FSCAN_TDELTA['max'] # start at fixed time if scan_task.start: now_delta = now - scan_task.start if (now_delta < settings.FSCAN_TDELTA['max'] and now_delta > settings.FSCAN_TDELTA['min']): if not Scan.objects.filter(scan_task=scan_task.id, finish__gte=min_time_allowed): logger.info('Found waiting time scan: %s' % scan_task.target) scan_task.run() # cron-schedule start if scan_task.cron: job = CronExpression(smart_str(scan_task.cron)) if job.check_trigger((now.year, now.month, now.day, now.hour, now.minute)): if not Scan.objects.filter(scan_task=scan_task.id, finish__gte=min_time_allowed): logger.info('Found waiting cron scan: %s' % scan_task.target) scan_task.run()
class Cron(InputModule): '''**Generates an event at the defined time** Generates an event with the defined payload at the chosen time. Time is in crontab format. Parameters:: - native_events(bool)(False) | Whether to expect incoming events to be native Wishbone events - cron(string)("*/10 * * * *") | The cron expression. - payload(str)("wishbone") | The content of <destination>. - destination(str)("data") | The location to write <payload> to. Queues:: - outbox | Outgoing messges ''' def __init__(self, actor_config, native_events=False, cron="*/10 * * * *", payload="wishbone", destination="data"): Actor.__init__(self, actor_config) self.pool.createQueue("outbox") self.cron = CronExpression("%s wishbone" % self.kwargs.cron) def preHook(self): self.sendToBackground(self.timer) def timer(self): while self.loop(): if self.cron.check_trigger(time.localtime(time.time())[:5]): self.logging.info("Cron executed.") for chunk in [self.kwargs_raw["payload"], None]: for payload in self.decode(chunk): event = self.generateEvent( payload, self.kwargs.destination ) self.submit( event, "outbox" ) sleep(60)
class Cron(Actor): '''**Generates an event at the defined time** Generates an event with the defined payload at the chosen time. Time is in crontab format. Parameters: - cron(string)("*/10 * * * *") | The cron expression. - payload(str)("wishbone") | The content of <field>. - field(str)("@data") | The location to write <payload> to. Queues: - outbox | Outgoing messges ''' def __init__(self, actor_config, cron="*/10 * * * *", payload="wishbone", field="@data"): Actor.__init__(self, actor_config) self.pool.createQueue("outbox") self.cron = CronExpression("%s wishbone" % self.kwargs.cron) def preHook(self): self.sendToBackground(self.timer) def timer(self): while self.loop(): if self.cron.check_trigger(time.localtime(time.time())[:5]): self.logging.info("Cron fired.") e = Event() e.set(self.kwargs.payload, self.kwargs.field) self.submit(e, self.pool.queue.outbox) sleep(60)