def _acquire_method_lock(self, method, this): method_id = id(method) if method.is_static: this_id = id(method.declaring_type) else: this_id = this.object_id thread_id = helpers.get_current_thread_id() while True: event, event_owner = self._locks.get((method_id, this_id), (None, None)) if event: if event_owner == thread_id: event = None break else: event.wait() else: event = eventlet.event.Event() self._locks[(method_id, this_id)] = (event, thread_id) break try: yield finally: if event is not None: del self._locks[(method_id, this_id)] event.send()
def _run_loop(self, kind, event, idle_for_func, initial_delay=None, stop_on_exception=True): func_name = reflection.get_callable_name(self.f) func = self.f if stop_on_exception else _safe_wrapper(self.f, kind, func_name) if initial_delay: greenthread.sleep(initial_delay) try: watch = timeutils.StopWatch() while self._running: watch.restart() result = func(*self.args, **self.kw) watch.stop() if not self._running: break idle = idle_for_func(result, watch.elapsed()) LOG.trace('%(kind)s %(func_name)r sleeping ' 'for %(idle).02f seconds', {'func_name': func_name, 'idle': idle, 'kind': kind}) greenthread.sleep(idle) except LoopingCallDone as e: event.send(e.retvalue) except Exception: exc_info = sys.exc_info() try: LOG.error(_LE('%(kind)s %(func_name)r failed'), {'kind': kind, 'func_name': func_name}, exc_info=exc_info) event.send_exception(*exc_info) finally: del exc_info return else: event.send(True)
def _acquire_method_lock(self, method, this): method_id = id(method) if method.is_static: this_id = id(method.declaring_type) else: this_id = this.object_id thread_id = helpers.get_current_thread_id() while True: event, event_owner = self._locks.get( (method_id, this_id), (None, None)) if event: if event_owner == thread_id: event = None break else: event.wait() else: event = eventlet.event.Event() self._locks[(method_id, this_id)] = (event, thread_id) break try: yield finally: if event is not None: del self._locks[(method_id, this_id)] event.send()
def _invoke_method_implementation(self, method, this, murano_class, context, params): body = method.body if not body: return None current_thread = eventlet.greenthread.getcurrent() if not hasattr(current_thread, '_murano_dsl_thread_marker'): thread_marker = current_thread._murano_dsl_thread_marker = \ uuid.uuid4().hex else: thread_marker = current_thread._murano_dsl_thread_marker method_id = id(body) this_id = this.object_id event, marker = self._locks.get((method_id, this_id), (None, None)) if event: if marker == thread_marker: return self._invoke_method_implementation_gt( body, this, params, murano_class, context) event.wait() event = eventlet.event.Event() self._locks[(method_id, this_id)] = (event, thread_marker) gt = eventlet.spawn(self._invoke_method_implementation_gt, body, this, params, murano_class, context, thread_marker) result = gt.wait() del self._locks[(method_id, this_id)] event.send() return result
def _acquire_method_lock(self, func, this): method_id = id(func) if isinstance(this, dsl_types.MuranoClass): this_id = id(this) else: this_id = this.object_id thread_id = helpers.get_current_thread_id() while True: event, event_owner = self._locks.get( (method_id, this_id), (None, None)) if event: if event_owner == thread_id: event = None break else: event.wait() else: event = eventlet.event.Event() self._locks[(method_id, this_id)] = (event, thread_id) break try: yield finally: if event is not None: del self._locks[(method_id, this_id)] event.send()
def _run_loop(self, kind, event, idle_for_func, initial_delay=None): if initial_delay: greenthread.sleep(initial_delay) try: watch = timeutils.StopWatch() while self._running: watch.restart() result = self.f(*self.args, **self.kw) watch.stop() if not self._running: break idle = idle_for_func(result, watch.elapsed()) LOG.debug( "%(kind)s %(func_name)r sleeping " "for %(idle).02f seconds", {"func_name": self.f, "idle": idle, "kind": kind}, ) greenthread.sleep(idle) except LoopingCallDone as e: event.send(e.retvalue) except Exception: exc_info = sys.exc_info() try: LOG.error(_LE("%(kind)s %(func_name)r failed"), {"kind": kind, "func_name": self.f}, exc_info=exc_info) event.send_exception(*exc_info) finally: del exc_info return else: event.send(True)
def _get_data(self, server_tail): print 'Starting data getter for ServerTail %s' % (server_tail.id,) client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) kwargs = {'timeout': 10} if server_tail.username: kwargs['username'] = server_tail.username if server_tail.password: kwargs['password'] = server_tail.password rsa_key = '/home/servertail/.ssh/id_rsa' if os.path.exists(rsa_key): kwargs['key_filename'] = rsa_key client.connect(server_tail.hostname, server_tail.port, **kwargs) command = 'uname -a' stdin, stdout, stderr = client.exec_command(command) if 'Darwin' in stdout.read(): command = 'tail -n%s -F %s' % (self.buffer_limit, server_tail.path) else: command = 'tail -q -s0.08 -n%s -F %s 2>&1' % (self.buffer_limit, server_tail.path) stdin, stdout, stderr = client.exec_command(command) for line in stdout: line_id = str(uuid.uuid1()) self.data[server_tail.id].append({ 'id': line_id, 'line': line.rstrip(), }) truncated = self.data[server_tail.id][-self.buffer_limit:] self.data[server_tail.id] = truncated events = self.events.pop(server_tail.id, []) for event in events: event.send({'error': False, 'id': line_id})
def _invoke_method_implementation(self, method, this, murano_class, context, params): body = method.body if not body: return None current_thread = eventlet.greenthread.getcurrent() if not hasattr(current_thread, "_murano_dsl_thread_marker"): thread_marker = current_thread._murano_dsl_thread_marker = uuid.uuid4().hex else: thread_marker = current_thread._murano_dsl_thread_marker method_id = id(body) this_id = this.object_id event, marker = self._locks.get((method_id, this_id), (None, None)) if event: if marker == thread_marker: return self._invoke_method_implementation_gt(body, this, params, murano_class, context) event.wait() event = eventlet.event.Event() self._locks[(method_id, this_id)] = (event, thread_marker) gt = eventlet.spawn( self._invoke_method_implementation_gt, body, this, params, murano_class, context, thread_marker ) result = gt.wait() del self._locks[(method_id, this_id)] event.send() return result
def _invoke_method_implementation(self, method, this, context, params): body = method.body if not body: return None murano_class = method.murano_class current_thread = eventlet.greenthread.getcurrent() if not hasattr(current_thread, '_muranopl_thread_marker'): thread_marker = current_thread._muranopl_thread_marker = \ uuid.uuid4().hex else: thread_marker = current_thread._muranopl_thread_marker method_id = id(body) this_id = this.object_id while True: event, marker = self._locks.get((method_id, this_id), (None, None)) if event: if marker == thread_marker: return self._invoke_method_implementation_gt( body, this, params, murano_class, context) event.wait() else: break event = eventlet.event.Event() self._locks[(method_id, this_id)] = (event, thread_marker) # noinspection PyProtectedMember method_info = '{0}.{1} ({2})'.format(murano_class.name, method._name, hash((method_id, this_id))) # Prepare caller information caller_ctx = helpers.get_caller_context(context) if caller_ctx: caller_info = trace.compose_stack_frame(caller_ctx) LOG.debug('{0}: Begin execution: {1} called from {2}'.format( thread_marker, method_info, trace.format_frame(caller_info))) else: LOG.debug('{0}: Begin execution: {1}'.format( thread_marker, method_info)) try: gt = eventlet.spawn(self._invoke_method_implementation_gt, body, this, params, murano_class, context, thread_marker) result = gt.wait() except Exception as e: LOG.debug("{0}: End execution: {1} with exception {2}".format( thread_marker, method_info, e)) raise else: LOG.debug("{0}: End execution: {1}".format(thread_marker, method_info)) finally: del self._locks[(method_id, this_id)] event.send() return result
def get_data(self, server_tail): try: return self._get_data(server_tail) except (KeyboardInterrupt, SystemExit): raise except: events = self.events.pop(server_tail.id, []) for event in events: event.send({'error': True})
def _invoke_method_implementation(self, method, this, context, params): body = method.body if not body: return None murano_class = method.murano_class current_thread = eventlet.greenthread.getcurrent() if not hasattr(current_thread, '_muranopl_thread_marker'): thread_marker = current_thread._muranopl_thread_marker = \ uuid.uuid4().hex else: thread_marker = current_thread._muranopl_thread_marker method_id = id(body) this_id = this.object_id while True: event, marker = self._locks.get((method_id, this_id), (None, None)) if event: if marker == thread_marker: return self._invoke_method_implementation_gt( body, this, params, murano_class, context) event.wait() else: break event = eventlet.event.Event() self._locks[(method_id, this_id)] = (event, thread_marker) # noinspection PyProtectedMember method_info = '{0}.{1} ({2})'.format(murano_class.name, method._name, hash((method_id, this_id))) # Prepare caller information caller_ctx = helpers.get_caller_context(context) if caller_ctx: caller_info = trace.compose_stack_frame(caller_ctx) LOG.debug( '{0}: Begin execution: {1} called from {2}'.format( thread_marker, method_info, trace.format_frame( caller_info))) else: LOG.debug( '{0}: Begin execution: {1}'.format( thread_marker, method_info)) gt = eventlet.spawn(self._invoke_method_implementation_gt, body, this, params, murano_class, context, thread_marker) result = gt.wait() del self._locks[(method_id, this_id)] LOG.debug( "{0}: End execution: {1}".format(thread_marker, method_info)) event.send() return result
def _run_loop(self, kind, event, idle_for_func, initial_delay=None, stop_on_exception=True): func_name = reflection.get_callable_name(self.f) func = self.f if stop_on_exception else _safe_wrapper( self.f, kind, func_name) if initial_delay: greenthread.sleep(initial_delay) try: watch = timeutils.StopWatch() while self._running: watch.restart() result = func(*self.args, **self.kw) watch.stop() if not self._running: break idle = idle_for_func(result, watch.elapsed()) LOG.trace( '%(kind)s %(func_name)r sleeping ' 'for %(idle).02f seconds', { 'func_name': func_name, 'idle': idle, 'kind': kind }) greenthread.sleep(idle) except LoopingCallDone as e: event.send(e.retvalue) except Exception: exc_info = sys.exc_info() try: LOG.error(_LE('%(kind)s %(func_name)r failed'), { 'kind': kind, 'func_name': func_name }, exc_info=exc_info) event.send_exception(*exc_info) finally: del exc_info return else: event.send(True)
def _handle_message(self, message): event, method, message = message if method == 'send': self._last_activity = time.time() if message: if isinstance(message, (tuple, list)): message = ''.join(message) self.client.send(message) event.send() elif method == 'disconnect': # check last activity if time.time() - self._last_activity > \ self._inactivity_disconnect: self._disconnect = True self.client.disconnect() event.send(True) else: # warning, there's been activity, abort event.send(False) else: event.throw(Exception('Unknown XMPP Method'))
def _acquire_method_lock(self, method, this, arg_val_dict): if this is None: if not arg_val_dict: # if neither "this" nor argument values are set then no # locking is needed key = None else: # if only the argument values are passed then find the lock # list only by the method key = (None, id(method)) else: if method.is_static: # find the lock list by the type and method key = (id(method.declaring_type), id(method)) else: # find the lock list by the object and method key = (this.object_id, id(method)) thread_id = helpers.get_current_thread_id() while True: event, event_owner = None, None if key is None: # no locking needed break lock_list = self._locks.setdefault(key, []) # lock list contains a list of tuples: # first item of each tuple is a dict with the values of locking # arguments (it is used for argument values comparison), # second item is an event to wait on, # third one is the owner thread id # If this lock list is empty it means no locks on this object and # method at all. for arg_vals, l_event, l_event_owner in lock_list: if arg_vals == arg_val_dict: event = l_event event_owner = l_event_owner break if event: if event_owner == thread_id: # this means a re-entrant lock: the tuple with the same # value of the first element exists in the list, but it was # acquired by the same green thread. We may proceed with # the call in this case event = None break else: event.wait() else: # this means either the lock list was empty or didn't contain a # tuple with the first element equal to arg_val_dict. # Then let's acquire a lock, i.e. create a new tuple and place # it into the list event = eventlet.event.Event() event_owner = thread_id lock_list.append((arg_val_dict, event, event_owner)) break try: yield finally: if event is not None: lock_list.remove((arg_val_dict, event, event_owner)) if len(lock_list) == 0: del self._locks[key] event.send()