def SetPanicMode(mode, enabled): """Set a panic mode on or off. Args: mode: str, mode to set enabled: bool, to enable or disable the mode """ if mode not in PANIC_MODES: raise ValueError(mode) q = models.KeyValueCache.get_by_key_name('%s%s' % (PANIC_MODE_PREFIX, mode)) if enabled: if not q: q = models.KeyValueCache(key_name='%s%s' % (PANIC_MODE_PREFIX, mode)) q.text_value = '1' q.put() else: if q: q.delete() models.KeyValueCache.ResetMemcacheWrap('%s%s' % (PANIC_MODE_PREFIX, mode))
def _GenerateInstallCounts(): """Generates a dictionary of all installs names and the count of each.""" # Obtain a lock. lock_name = 'pkgs_list_cron_lock' lock = gae_util.ObtainLock(lock_name) if not lock: logging.warning('GenerateInstallCounts: lock found; exiting.') return # Get a list of all packages that have previously been pushed. pkgs, unused_dt = models.ReportsCache.GetInstallCounts() # Generate a query of all InstallLog entites that haven't been read yet. query = models.InstallLog.all().order('server_datetime') cursor_obj = models.KeyValueCache.get_by_key_name('pkgs_list_cursor') if cursor_obj: query.with_cursor(cursor_obj.text_value) # Loop over new InstallLog entries. try: installs = query.fetch(1000) except: installs = None if not installs: models.ReportsCache.SetInstallCounts(pkgs) gae_util.ReleaseLock(lock_name) return for install in installs: pkg_name = install.package if pkg_name not in pkgs: pkgs[pkg_name] = { 'install_count': 0, 'install_fail_count': 0, 'applesus': install.applesus, } if install.IsSuccess(): pkgs[pkg_name]['install_count'] = ( pkgs[pkg_name].setdefault('install_count', 0) + 1) # (re)calculate avg_duration_seconds for this package. if 'duration_seconds_avg' not in pkgs[pkg_name]: pkgs[pkg_name]['duration_count'] = 0 pkgs[pkg_name]['duration_total_seconds'] = 0 pkgs[pkg_name]['duration_seconds_avg'] = None # only proceed if entity has "duration_seconds" property != None. if getattr(install, 'duration_seconds', None) is not None: pkgs[pkg_name]['duration_count'] += 1 pkgs[pkg_name]['duration_total_seconds'] += ( install.duration_seconds) pkgs[pkg_name]['duration_seconds_avg'] = int( pkgs[pkg_name]['duration_total_seconds'] / pkgs[pkg_name]['duration_count']) else: pkgs[pkg_name]['install_fail_count'] = ( pkgs[pkg_name].setdefault('install_fail_count', 0) + 1) # Update any changed packages. models.ReportsCache.SetInstallCounts(pkgs) if not cursor_obj: cursor_obj = models.KeyValueCache(key_name='pkgs_list_cursor') cursor_txt = str(query.cursor()) cursor_obj.text_value = cursor_txt cursor_obj.put() # Delete the lock. gae_util.ReleaseLock(lock_name) deferred.defer(_GenerateInstallCounts)