def run(self): # Delete the cleanup tenant_leases = self.domain_mgr.get_all_tenant_leases() for t_lease in tenant_leases: self._delete_vms_for_tenant(t_lease) # Sleep again for sleep_seconds spawn_after(self.sleep_seconds, self.run)
def _event_emit_delayed(self, event): """Emit events - possibly delayed.""" def event_cleanup(gt, *args, **kwargs): """Callback function for greenthread. Called to cleanup the _events_delayed dictionary when an event was called. """ event = args[0] self._events_delayed.pop(event.uuid, None) # Cleanup possible delayed stop events. if event.uuid in self._events_delayed.keys(): self._events_delayed[event.uuid].cancel() self._events_delayed.pop(event.uuid, None) LOG.debug("Removed pending event for %s due to " "lifecycle event", event.uuid) if event.transition == virtevent.EVENT_LIFECYCLE_STOPPED: # Delay STOPPED event, as they may be followed by a STARTED # event in case the instance is rebooting id_ = greenthread.spawn_after(self._lifecycle_delay, self._event_emit, event) self._events_delayed[event.uuid] = id_ # add callback to cleanup self._events_delayed dict after # event was called id_.link(event_cleanup, event) else: self._event_emit(event)
def _reschedule_pod(self, client, pod): ''' Reschedule the pod by marking the node to be unscheduable and delete the pod ''' fname = "_reschedule_pod" if pod.NodeName: if not self._close_nodes([pod.NodeName]): return False LOG.warn( _("[%s] %s: node <%s> is marked to unscheduable" % (fname, self.stack.name, pod.NodeName))) gthread = greenthread.spawn_after( self.pod_fail_node_schedule_pause, self._open_nodes, [pod.NodeName]) try: client.DeletePods(pod.Name, namespace=self.ns) except KubernetesError, e: LOG.warn( _("[%s] %s: failed to delete pod <%s>" % (fname, self.stack.name, pod.Name))) return False LOG.warn( _("[%s] %s: pod <%s> on host <%s> is rescheduled" % (fname, self.stack.name, pod.Name, pod.NodeName)))
def _try_shrink_oldrc(self, client, action, num_update): fname = '_try_shrink_oldrc' if action is 'resize': LOG.info( _("[%s] %s: Resize the rc <%s> to <%d> replicas." % (self.stack.name, fname, self.old_rc_name, num_update))) node_list_before = self._get_nodes_of_rc(client, rc_name=self.old_rc_name) client.ResizeReplicationController(name=self.old_rc_name, replicas=num_update, namespace=self.ns) # maybe the following sleep is not necessary greenthread.sleep(5) node_list_after = self._get_nodes_of_rc(client, rc_name=self.old_rc_name) node_list = list(set(node_list_before) - set(node_list_after)) if self.upd_pod_node_schedule_pause > 0: # mark all nodes unscheduable for all deleted pods for node in node_list: if self._close_nodes([node]): LOG.info( "[%s] %s: node <%s> is marked as unschedulable" % (self.stack.name, fname, node)) else: LOG.warn( "[%s] %s: node <%s> can not be marked as unschedulable" % (self.stack.name, fname, node)) greenthread.spawn_after(self.upd_pod_node_schedule_pause, self._open_nodes, node_list) self._pause_between_batch() else: LOG.info( _("[%s] %s: Delete the rc <%s>" % (self.stack.name, fname, self.old_rc_name))) client.DeleteReplicationController(name=self.old_rc_name, namespace=self.ns)
def _register_delayed_event(self, pvm_state, inst): # Cancel out the current delay event. Can happen as it goes # from SHUTTING_DOWN to NOT_ACTIVATED, multiple delayed events # can come in at once. Only want the last. if inst.uuid in self._delayed_event_threads: self._delayed_event_threads[inst.uuid].cancel() # Spawn in the background elem = greenthread.spawn_after(15, self._emit_event, pvm_state, inst, False) self._delayed_event_threads[inst.uuid] = elem
def process(self, inst, pvm_uuid): """Emits the event, or adds it to the queue for delayed emission. :param inst: The nova instance. May be None. :param pvm_uuid: The PowerVM LPAR UUID. """ # Cancel out the current delay event. Can happen as it goes # from SHUTTING_DOWN to NOT_ACTIVATED, multiple delayed events # can come in at once. Only want the last. if pvm_uuid in self._delayed_event_threads: self._delayed_event_threads[pvm_uuid].cancel() # Spawn in the background elem = greenthread.spawn_after(15, self._emit_event, pvm_uuid, inst) self._delayed_event_threads[pvm_uuid] = elem
def _schedule_expiration(self): """ Sets up a timer that will call _expire_old_connections when the oldest connection currently in the free pool is ready to expire. This is the earliest possible time that a connection could expire, thus, the timer will be running as infrequently as possible without missing a possible expiration. If this function is called when a timer is already scheduled, it does nothing. If max_age or max_idle is 0, _schedule_expiration likewise does nothing. """ if self.max_age is 0 or self.max_idle is 0: # expiration is unnecessary because all connections will be expired # on put return if ( self._expiration_timer is not None and not getattr(self._expiration_timer, "called", False) and not getattr(self._expiration_timer, "cancelled", False) ): # the next timer is already scheduled return try: now = time.time() self._expire_old_connections(now) # the last item in the list, because of the stack ordering, # is going to be the most-idle idle_delay = (self.free_items[-1][0] - now) + self.max_idle oldest = min([t[1] for t in self.free_items]) age_delay = (oldest - now) + self.max_age next_delay = min(idle_delay, age_delay) except (IndexError, ValueError): # no free items, unschedule ourselves self._expiration_timer = None return if next_delay > 0: # set up a continuous self-calling loop self._expiration_timer = greenthread.spawn_after(next_delay, self._schedule_expiration)
def _schedule_expiration(self): """ Sets up a timer that will call _expire_old_connections when the oldest connection currently in the free pool is ready to expire. This is the earliest possible time that a connection could expire, thus, the timer will be running as infrequently as possible without missing a possible expiration. If this function is called when a timer is already scheduled, it does nothing. If max_age or max_idle is 0, _schedule_expiration likewise does nothing. """ if self.max_age is 0 or self.max_idle is 0: # expiration is unnecessary because all connections will be expired # on put return if (self._expiration_timer is not None and not getattr(self._expiration_timer, 'called', False) and not getattr(self._expiration_timer, 'cancelled', False)): # the next timer is already scheduled return try: now = time.time() self._expire_old_connections(now) # the last item in the list, because of the stack ordering, # is going to be the most-idle idle_delay = (self.free_items[-1][0] - now) + self.max_idle oldest = min([t[1] for t in self.free_items]) age_delay = (oldest - now) + self.max_age next_delay = min(idle_delay, age_delay) except (IndexError, ValueError): # no free items, unschedule ourselves self._expiration_timer = None return if next_delay > 0: # set up a continuous self-calling loop self._expiration_timer = greenthread.spawn_after( next_delay, self._schedule_expiration)
def _event_emit_delayed(self, event): """Emit events - possibly delayed.""" def event_cleanup(gt, *args, **kwargs): """Callback function for greenthread. Called to cleanup the _events_delayed dictionary when a event was called. """ event = args[0] self._events_delayed.pop(event.uuid, None) if self._lifecycle_delay > 0: if event.uuid not in self._events_delayed.keys(): id_ = greenthread.spawn_after(self._lifecycle_delay, self._event_emit, event) self._events_delayed[event.uuid] = id_ # add callback to cleanup self._events_delayed dict after # event was called id_.link(event_cleanup, event) else: self._event_emit(event)
def test_basic(self): gt = greenthread.spawn_after(0.1, passthru, 20) self.assertEquals(gt.wait(), ((20,), {}))
def spawn_later(self, seconds, func, *args, **kwargs): """ """ func = functools.partial(self.spawn, func, *args, **kwargs) return greenthread.spawn_after(seconds, func)
from eventlet import event from eventlet import greenthread evt = event.Event() def jocker(): i = 0 while i < 3: print 'jocker: can anybody catch me?' i += 1 time.sleep(1) print evt.wait() print 'fighting!' time.sleep(3) return 'jocker: sorry, you got missed!' def batman(): evt.send('batman: i am coming for you!') return 'batman; I am back, and your day is coming!' gt1 = greenthread.spawn(jocker) gt2 = greenthread.spawn_after(5, batman) print gt1.wait() time.sleep(1) print gt2.wait() time.sleep(1) print 'to be continued.............'
def test_kill_already_started(self): gt = greenthread.spawn_after(0, waiter, 22) greenthread.sleep(0) gt.kill() self.assert_dead(gt)
def test_cancel_already_started(self): gt = greenthread.spawn_after(0, waiter, 22) greenthread.sleep(0) gt.cancel() self.assertEquals(gt.wait(), 22)
def test_cancel(self): gt = greenthread.spawn_after(0.1, passthru, 21) gt.cancel() self.assert_dead(gt)
def test_basic(self): gt = greenthread.spawn_after(0.1, passthru, 20) self.assertEquals(gt.wait(), ((20, ), {}))
def start(self): spawn_after(self.sleep_seconds, self.run)