Beispiel #1
0
 def cancel(self, watch_id):
     if watch_id is not None:
         self._watch_id_callbacks.pop(watch_id, None)
         cancel_watch = etcdrpc.WatchCancelRequest()
         cancel_watch.watch_id = watch_id
         request = etcdrpc.WatchRequest(cancel_request=cancel_watch)
         self._watch_requests_queue.put((request, None))
Beispiel #2
0
 def _build_watch_request(self,
                          cv,
                          key,
                          range_end=None,
                          start_revision=None,
                          progress_notify=False,
                          filters=None,
                          prev_kv=False):
     cv.acquire()
     create_watch = etcdrpc.WatchCreateRequest()
     create_watch.key = utils.to_bytes(key)
     if range_end is not None:
         create_watch.range_end = utils.to_bytes(range_end)
     if start_revision is not None:
         create_watch.start_revision = start_revision
     if progress_notify:
         create_watch.progress_notify = progress_notify
     if filters is not None:
         create_watch.filters = filters
     if prev_kv:
         create_watch.prev_kv = prev_kv
     create_watch.progress_notify = True
     watch_requests = etcdrpc.WatchRequest(create_request=create_watch)
     yield watch_requests
     cv.wait()
     cv.release()
Beispiel #3
0
    def add_callback(self,
                     key,
                     callback,
                     range_end=None,
                     start_revision=None,
                     progress_notify=False,
                     filters=None,
                     prev_kv=False):
        create_watch = etcdrpc.WatchCreateRequest()
        create_watch.key = utils.to_bytes(key)
        if range_end is not None:
            create_watch.range_end = utils.to_bytes(range_end)
        if start_revision is not None:
            create_watch.start_revision = start_revision
        if progress_notify:
            create_watch.progress_notify = progress_notify
        if filters is not None:
            create_watch.filters = filters
        if prev_kv:
            create_watch.prev_kv = prev_kv
        rq = etcdrpc.WatchRequest(create_request=create_watch)

        with self._lock:
            # Start the callback thread if it is not yet running.
            if not self._callback_thread:
                thread_name = 'etcd3_watch_%x' % (id(self), )
                self._callback_thread = threading.Thread(name=thread_name,
                                                         target=self._run)
                self._callback_thread.daemon = True
                self._callback_thread.start()

            # Only one create watch request can be pending at a time, so if
            # there one already, then wait for it to complete first.
            while self._new_watch:
                self._new_watch_cond.wait()

            # Submit a create watch request.
            new_watch = _NewWatch(callback)
            self._request_queue.put(rq)
            self._new_watch = new_watch

            # Wait for the request to be completed, or timeout.
            self._new_watch_cond.wait(timeout=self.timeout)
            self._new_watch = None

            # If the request not completed yet, then raise a timeout exception.
            if new_watch.id is None and new_watch.err is None:
                raise exceptions.WatchTimedOut()

            # Raise an exception if the watch request failed.
            if new_watch.err:
                raise new_watch.err

            # Wake up threads stuck on add_callback call if any.
            self._new_watch_cond.notify_all()
            return new_watch.id
Beispiel #4
0
 def _create_watch_request(self,
                           key,
                           range_end=None,
                           start_revision=None,
                           progress_notify=False,
                           filters=None,
                           prev_kv=False):
     create_watch = etcdrpc.WatchCreateRequest()
     create_watch.key = utils.to_bytes(key)
     if range_end is not None:
         create_watch.range_end = utils.to_bytes(range_end)
     if start_revision is not None:
         create_watch.start_revision = start_revision
     if progress_notify:
         create_watch.progress_notify = progress_notify
     if filters is not None:
         create_watch.filters = filters
     if prev_kv:
         create_watch.prev_kv = prev_kv
     return etcdrpc.WatchRequest(create_request=create_watch)
Beispiel #5
0
 def add_callback(self, key, callback,
                  range_end=None,
                  start_revision=None,
                  progress_notify=False,
                  filters=None,
                  prev_kv=False):
     with self._watch_id_lock:
         create_watch = etcdrpc.WatchCreateRequest()
         create_watch.key = utils.to_bytes(key)
         if range_end is not None:
             create_watch.range_end = utils.to_bytes(range_end)
         if start_revision is not None:
             create_watch.start_revision = start_revision
         if progress_notify:
             create_watch.progress_notify = progress_notify
         if filters is not None:
             create_watch.filters = filters
         if prev_kv:
             create_watch.prev_kv = prev_kv
         request = etcdrpc.WatchRequest(create_request=create_watch)
         self._watch_requests_queue.put((request, callback))
         return self._watch_id_queue.get(timeout=self.timeout)
Beispiel #6
0
 def _cancel_no_lock(self, watch_id):
     cancel_watch = etcdrpc.WatchCancelRequest()
     cancel_watch.watch_id = watch_id
     rq = etcdrpc.WatchRequest(cancel_request=cancel_watch)
     self._request_queue.put(rq)