def check_passive_url_monitors(): request = HttpRequest() request.META["HTTP_X_CELERY_CRON"] = "true" modules = Module.objects.filter(module_type="url_check") sending_modules = [] for i in xrange(len(modules)): module = modules[i] passive_key = CHECK_HOST_KEY % module.id if memcache.get(passive_key, False): # this means that the check is still running for this module logging.critical("Module id %s is still running" % module.id) continue memcache.set(passive_key, module, CELERY_CACHE_TIMEOUT) sending_modules.append(module.id) if (i != 0 or len(modules) == 1) and ( ((i % settings.PASSIVE_URL_CHECK_BATCH) == 0) or (i == (len(modules) - 1)) ): # Don't run when it is the first iteration, unless only one module to monitor # Run in batch sizes defined by the settings and run the remaning at the end of # the loop even if batch size isn't met. check_passive_url_task.apply_async((request, sending_modules)) sending_modules = [] return True
def send_notifications(): notifications = Notification.objects.filter(sent_at=None, send=True).order_by('-created_at') for notification in notifications: if not notification.send: logging.info("Auto-send is disabled for notification %s."%notification.id) continue not_key = CHECK_NOTIFICATION_KEY % notification.id if memcache.get(not_key,False): #this means that the check is already running logging.critical("Module id %s is already running"%notification.id) continue request = HttpRequest() request.META['HTTP_X_CELERY_CRON'] = 'true' memcache.set(not_key,notification, CELERY_CACHE_TIMEOUT) send_notification_task.apply_async((request, notification.id)) return True #class CheckNotifications(Job): # run_every = 120 #seconds # # # def job(self): # check_notifications(HttpRequest()) #cronScheduler.register(CheckPassiveHosts) #cronScheduler.register(CheckNotifications)
def get_day_status(self, day): day_status = memcache.get(MODULE_DAY_STATUS_KEY % (day.month, day.day, self.id), False) if day_status: return day_status day_status = DailyModuleStatus.objects.\ filter(module=self).\ filter(created_at__gte=datetime.datetime(\ day.year, day.month, day.day, 0, 0, 0)).\ filter(created_at__lte=datetime.datetime(\ day.year, day.month, day.day, 23, 59, 59, 999999)).\ filter(site_config=self.site_config) if day_status: memcache.set(MODULE_DAY_STATUS_KEY % (day.month, day.day, self.id), day_status[0]) return day_status[0] day_status = DailyModuleStatus() day_status.created_at = day day_status.updated_at = day day_status.module = self day_status.statuses = self.status day_status.status = self.status day_status.site_config = self.site_config day_status.save() memcache.set(MODULE_DAY_STATUS_KEY % (day.month, day.day, self.id), day_status) return day_status
def check_passive_url_monitors(): modules = Module.objects.filter(module_type='url_check') request = HttpRequest() request.META['HTTP_X_CELERY_CRON'] = 'true' for module in modules: passive_key = CHECK_HOST_KEY % module.id if memcache.get(passive_key,False): #this means that the check is already running logging.critical("Module id %s is already running"%module.id) continue memcache.set(passive_key,module,CELERY_CACHE_TIMEOUT) check_passive_url_task.apply_async((request, module.id))
def send_notifications(): notifications = Notification.objects.filter(sent_at=None, send=True).order_by("-created_at") for notification in notifications: if not notification.send: logging.info("Auto-send is disabled for notification %s." % notification.id) continue not_key = CHECK_NOTIFICATION_KEY % notification.id if memcache.get(not_key, False): # this means that the check is already running logging.critical("Module id %s is already running" % notification.id) continue request = HttpRequest() request.META["HTTP_X_CELERY_CRON"] = "true" memcache.set(not_key, notification, CELERY_CACHE_TIMEOUT) send_notification_task.apply_async((request, notification.id)) return True