def _acquire_lock(self): LOCK_TIMEOUT = timedelta(minutes=120) WAIT_STEP = 15 vms = Internals.get(name='virtual_machines') if vms is None: vms = Internals({'name': 'virtual_machines'}) vms.save() locked_vm = False while not locked_vm: for i, label in enumerate(self.labels): self._use_vm(i) last_locked = "{}.last_locked".format(self.vm_record) if vms.update_value([self.vm_record, 'locked'], True): vms.update_value([self.vm_record, 'last_locked'], datetime.now()) locked_vm = True break expired_date = datetime.now() - LOCK_TIMEOUT if vms._update({'$set': { last_locked: datetime.now() }}, {last_locked: { '$lt': expired_date }}): vms.update_value([self.vm_record, 'locked'], True) locked_vm = True break if not locked_vm: sleep(WAIT_STEP)
def _acquire_lock(self): LOCK_TIMEOUT = timedelta(minutes=120) WAIT_STEP = 15 vms = Internals.get(name='virtual_machines') if vms is None: vms = Internals({'name': 'virtual_machines'}) vms.save() last_locked = "{}.last_locked".format(self.vm_record) while True: if vms.update_value([self.vm_record, 'locked'], True): vms.update_value([self.vm_record, 'last_locked'], datetime.now()) break expired_date = datetime.now() - LOCK_TIMEOUT if vms._update({'$set': { last_locked: datetime.now() }}, {last_locked: { '$lt': expired_date }}): vms.update_value([self.vm_record, 'locked'], True) break sleep(WAIT_STEP)
def update_queue(module, new_queue): if new_queue == '': flash('queue cannot be empty', 'danger') return validation_error() elif module['queue'] != new_queue: module.update_setting_value('queue', new_queue) updates = Internals( get_or_404(Internals.get_collection(), name="updates")) updates.update_value("last_update", time()) flash( 'Workers will reload once they are done with their current tasks', 'success')
def reload(self): """Reload the workers .. :quickref: Module; Reload workers Requires the `manage_modules` permission. Returns "ok". """ for repository in Repository.get_collection().find(): dispatcher.update_modules(Repository(repository)) updates = Internals( get_or_404(Internals.get_collection(), name="updates")) updates.update_value("last_update", time()) flash( 'Workers will reload once they are done with their current tasks', 'success') return redirect('ok', url_for('ModulesView:index'))
def configure(self, id): """Configure a module. .. :quickref: Module; Configure a module Requires the `manage_modules` permission. For each configuration available, you should set the value in a form parameter named ``config_NAME``. For boolean values, any value not ``0`` or ``False`` is considered to be ``True``. If the setting should be an option (be available per analysis), you have to set ``config_NAME_option`` to any value but ``0`` or ``False``. If successful, will return the module in ``module``. Otherwise, errors will be available in ``errors``. :param id: id of the named configuration. :form acts_on: comma-delimited list of FAME types this module can act on (only for Processing modules). :form triggered_by: comma-delimited list of triggers (only for Processing modules). :form queue: name of the queue to use for this module (only for Processing modules). """ module = ModuleInfo(get_or_404(ModuleInfo.get_collection(), _id=id)) if request.method == "POST": if module['type'] == 'Processing': if 'acts_on' in request.form: module.update_setting_value( 'acts_on', request.form.get('acts_on', '')) if 'triggered_by' in request.form: module.update_setting_value( 'triggered_by', request.form.get('triggered_by', '')) if 'queue' in request.form: new_queue = request.form.get('queue') if module['queue'] == '': flash('queue cannot be empty', 'danger') return validation_error() else: if module['queue'] != new_queue: module.update_setting_value('queue', new_queue) updates = Internals( get_or_404(Internals.get_collection(), name="updates")) updates.update_value("last_update", time()) flash( 'Workers will reload once they are done with their current tasks', 'success') errors = update_config(module['config'], options=(module['type'] == 'Processing')) if errors is not None: return errors module.save() dispatcher.reload() return redirect({'module': clean_modules(module)}, url_for('ModulesView:index')) else: return render({'module': clean_modules(module)}, 'modules/module_configuration.html')