Esempio n. 1
0
 def start(self):
     self.done = event.Event()
     
     def _inner():
         """ query the file_name size and get the progress"""
         self._running = True
         while self._running:
             try:
                 transferred_size = self.threadSafePipe.transferred
                 LOG.debug("liuling  transferred_size %d " , transferred_size )
                 LOG.debug("liuling  total_size  %d" ,self.total_size )
                 if transferred_size >= self.total_size:
                     self.stop()
                     self.done.send(True)
                     continue
                 progress="%.0f%%" % (transferred_size/self.total_size * 100)
                 task_state  = self.task_state +'(' + progress + ')'
                 vm_uuid=self.volume['instance_uuid']
                 if vm_uuid:
                     instance = self.nova_client.servers.get(vm_uuid)
                     self.nova_client.servers.set_meta(instance,{'task_state':task_state})
                 greenthread.sleep(PROGRESS_REPORT_THREAD_SLEEP_TIME)
             except Exception as exc:
                 self.stop()
                 LOG.exception(exc)
                 self.done.send_exception(exc)
                 
                 
     greenthread.spawn(_inner)
     return self.done
    def start(self, initial_delay=None, periodic_interval_max=None):
        self._running = True
        done = event.Event()

        def _inner():
            if initial_delay:
                greenthread.sleep(initial_delay)

            try:
                while self._running:
                    idle = self.f(*self.args, **self.kw)
                    if not self._running:
                        break

                    if periodic_interval_max is not None:
                        idle = min(idle, periodic_interval_max)
                    LOG.debug(_('Dynamic looping call sleeping for %.02f '
                                'seconds'), idle)
                    greenthread.sleep(idle)
            except LoopingCallDone as e:
                self.stop()
                done.send(e.retvalue)
            except Exception:
                LOG.exception(_('in dynamic looping call'))
                done.send_exception(*sys.exc_info())
                return
            else:
                done.send(True)

        self.done = done

        greenthread.spawn(_inner)
        return self.done
Esempio n. 3
0
    def start(self, interval, initial_delay=None):
        self._running = True
        done = event.Event()

        def _inner():
            if initial_delay:
                greenthread.sleep(initial_delay)

            try:
                while self._running:
                    self.f(*self.args, **self.kw)
                    if not self._running:
                        break
                    greenthread.sleep(interval)
            except LoopingCallDone as e:
                self.stop()
                done.send(e.retvalue)
            except Exception:
                LOG.exception(_('in looping call'))
                done.send_exception(*sys.exc_info())
                return
            else:
                done.send(True)

        self.done = done

        greenthread.spawn(_inner)
        return self.done
Esempio n. 4
0
    def start(self, initial_delay=None, periodic_interval_max=None):
        self._running = True
        done = event.Event()

        def _inner():
            if initial_delay:
                greenthread.sleep(initial_delay)

            try:
                while self._running:
                    idle = self.f(*self.args, **self.kw) # callback

                    if not self._running:
                        break
                    if periodic_interval_max is not None:
                        idle = min(idle, periodic_interval_max)
                    logger.debug("Dynamic looping call sleeping for %d seconds", idle)
                    greenthread.sleep(idle)
            except LoopingCallDone as e:
                logger.info('DynamicLoopingCall _inner: Exception1')
                self.stop()
                done.send(e.retvalue)
            except Exception as ex:
                logger.info('DynamicLoopingCall _inner: Exception2')
                logger.debug(ex)
                done.send_exception(*sys.exc_info())
                return
            else:
                done.send(True)

        self._done_ = done

        greenthread.spawn(_inner)
        logger.debug('DynamicLoopingCall: Exiting...')
        return self._done_
Esempio n. 5
0
 def start(self):
     self.done = event.Event()
     
     def _inner():
         """ query the file_name size and get the progress"""
         self._running = True
         while self._running:
             try:
                 transferred_size = self.threadSafePipe.transferred
                
                 if transferred_size >= self.total_size:
                     self.stop()
                     self.done.send(True)
                     continue
                 progress="%.0f%%" % (transferred_size/self.total_size * 100)
                 self.instance.task_state  = self.task_state +'(' + progress + ')'
                 self.instance.save()
                 greenthread.sleep(PROGRESS_REPORT_THREAD_SLEEP_TIME)
             except Exception as exc:
                 self.stop()
                 LOG.exception(exc)
                 self.done.send_exception(exc)
                 
                 
     greenthread.spawn(_inner)
     return self.done
Esempio n. 6
0
    def start(self, initial_delay=None, periodic_interval_max=None):
        self._running = True
        done = event.Event()

        def _inner():
            if initial_delay:
                greenthread.sleep(initial_delay)

            try:
                while self._running:
                    idle = self.f(*self.args, **self.kw)
                    if not self._running:
                        break

                    if periodic_interval_max is not None:
                        idle = min(idle, periodic_interval_max)
                    pass
                    greenthread.sleep(idle)
            except LoopingCallDone as e:
                self.stop()
                done.send(e.retvalue)
            except Exception:
                pass
                done.send_exception(*sys.exc_info())
                return
            else:
                done.send(True)

        self.done = done

        greenthread.spawn(_inner)
        return self.done
Esempio n. 7
0
    def start(self, initial_delay=None, periodic_interval_max=None):
        self._running = True
        done = event.Event()

        def _inner():
            if initial_delay:
                greenthread.sleep(initial_delay)

            try:
                while self._running:
                    idle = self.f(*self.args, **self.kw)
                    if not self._running:
                        break

                    if periodic_interval_max is not None:
                        idle = min(idle, periodic_interval_max)
                    LOG.debug(
                        "Dynamic looping call %(func_name)s sleeping " "for %(idle).02f seconds",
                        {"func_name": repr(self.f), "idle": idle},
                    )
                    greenthread.sleep(idle)
            except LoopingCallDone as e:
                self.stop()
                done.send(e.retvalue)
            except Exception:
                LOG.exception(_LE("in dynamic looping call"))
                done.send_exception(*sys.exc_info())
                return
            else:
                done.send(True)

        self.done = done

        greenthread.spawn(_inner)
        return self.done
Esempio n. 8
0
    def start(self, interval, initial_delay=None):
        self._running = True
        done = event.Event()

        def _inner():
            if initial_delay:
                greenthread.sleep(initial_delay)
            cycle_counter = 0
            try:
                while self._running:
                    self.f(*self.args, **self.kw)
                    if not self._running:
                        break
                    if self.cycle_index > 0:
                        cycle_counter += 1
                        if cycle_counter >= self.cycle_index:
                            raise exceptions.AttemptsFailure()
                    greenthread.sleep(interval)
            except LoopingCallDone as e:
                self.stop()
                done.send(e.retvalue)
            except Exception:
                LOG.exception('in looping call')
                done.send_exception(*sys.exc_info())
                return
            else:
                done.send(True)

        self.done = done

        greenthread.spawn(_inner)
        return self.done
Esempio n. 9
0
 def test_spawn_is_not_cancelled(self):
     def func():
         greenthread.spawn(self.lst.pop)
         # exiting immediatelly, but self.lst.pop must be called
     greenthread.spawn(func)
     greenthread.sleep(0.1)
     assert self.lst == [], self.lst
Esempio n. 10
0
    def start(self):
        self.done = event.Event()

        def _inner():
            """Read data from input and write the same to output."""
            self._running = True
            while self._running:
                try:
                    data = self.input_file.read(None)
                    if not data:
                        self.stop()
                        self.done.send(True)
                    self.output_file.write(data)
                    if hasattr(self.input_file, "update_progress"):
                        self.input_file.update_progress()
                    if hasattr(self.output_file, "update_progress"):
                        self.output_file.update_progress()
                    greenthread.sleep(IO_THREAD_SLEEP_TIME)
                except Exception as exc:
                    self.stop()
                    LOG.exception(exc)
                    self.done.send_exception(exc)

        greenthread.spawn(_inner)
        return self.done
Esempio n. 11
0
    def test_timer_cancelled_upon_greenlet_exit(self):
        def func():
            greenthread.spawn_after_local(0.1, self.lst.pop)

        greenthread.spawn(func)
        assert self.lst == [1], self.lst
        greenthread.sleep(0.2)
        assert self.lst == [1], self.lst
Esempio n. 12
0
    def test_timer_fired(self):
        def func():
            greenthread.spawn_after_local(0.1, self.lst.pop)
            greenthread.sleep(0.2)

        greenthread.spawn(func)
        assert self.lst == [1], self.lst
        greenthread.sleep(0.3)
        assert self.lst == [], self.lst
Esempio n. 13
0
    def start(self):
        self.done = event.Event()

        def _inner():
            """Initiate write thread.

            Function to do the image data transfer through an update
            and thereon checks if the state is 'active'.
            """
            LOG.debug("Initiating image service update on image: %(image)s "
                      "with meta: %(meta)s" % {'image': self.image_id,
                                               'meta': self.image_meta})

            try:
                self.image_service.update(self.context,
                                          self.image_id,
                                          self.image_meta,
                                          data=self.input_file)

                self._running = True
                while self._running:
                    image_meta = self.image_service.show(self.context,
                                                         self.image_id)
                    image_status = image_meta.get('status')
                    if image_status == 'active':
                        self.stop()
                        LOG.debug("Glance image: %s is now active." %
                                  self.image_id)
                        self.done.send(True)
                    # If the state is killed, then raise an exception.
                    elif image_status == 'killed':
                        self.stop()
                        msg = (_("Glance image: %s is in killed state.") %
                               self.image_id)
                        LOG.error(msg)
                        excep = error_util.ImageTransferException(msg)
                        self.done.send_exception(excep)
                    elif image_status in ['saving', 'queued']:
                        greenthread.sleep(GLANCE_POLL_INTERVAL)
                    else:
                        self.stop()
                        msg = _("Glance image %(id)s is in unknown state "
                                "- %(state)s") % {'id': self.image_id,
                                                  'state': image_status}
                        LOG.error(msg)
                        excep = error_util.ImageTransferException(msg)
                        self.done.send_exception(excep)
            except Exception as ex:
                self.stop()
                msg = (_("Error occurred while writing to image: %s") %
                       self.image_id)
                LOG.exception(msg)
                excep = error_util.ImageTransferException(ex)
                self.done.send_exception(excep)

        greenthread.spawn(_inner)
        return self.done
Esempio n. 14
0
    def start(self, initial_delay=None, starting_interval=1, timeout=300,
              max_interval=300, jitter=0.75):
        self._running = True
        done = event.Event()

        def _inner():
            interval = starting_interval
            error_time = 0

            if initial_delay:
                greenthread.sleep(initial_delay)

            try:
                while self._running:
                    no_error = self.f(*self.args, **self.kw)
                    if not self._running:
                        break
                    random_jitter = random.gauss(jitter, 0.1)
                    if no_error:
                        # Reset error state
                        error_time = 0
                        interval = starting_interval
                        idle = interval * random_jitter
                    else:
                        # Backoff
                        interval = min(interval * 2 * random_jitter,
                                       max_interval)
                        idle = interval

                        # Don't go over timeout, end early if necessary. If
                        # timeout is 0, keep going.
                        if timeout > 0 and error_time + idle > timeout:
                            raise LoopingCallTimeOut(
                                'Looping call timed out after %.02f seconds'
                                % error_time)
                        error_time += idle

                    LOG.debug('Dynamic looping call sleeping for %.02f '
                              'seconds', idle)
                    greenthread.sleep(idle)
            except loopingcall.LoopingCallDone as e:
                self.stop()
                done.send(e.retvalue)
            except Exception:
                LOG.exception('in dynamic looping call')
                done.send_exception(*sys.exc_info())
                return
            else:
                done.send(True)

        self.done = done
        greenthread.spawn(_inner)
        return self.done
Esempio n. 15
0
 def start(self, interval, initial_delay=None):
     self._running = True
     done = event.Event()
     def _inner():
         try:
             while self._running:
                 print "In loop"
                 greenthread.sleep(interval)
                 print "loop done"
         except Exception as e:
             print('in looping call')
             return
     self.done = done
     greenthread.spawn(_inner)
     return self.done
Esempio n. 16
0
    def start(self):

        def _inner():
            """Read data from the input and write the same to the output
            until the transfer completes.
            """
            try:
                self.callback(*self.args, **self.kwargs)
                self._status = TASK_SUCCESS
            except Exception as e:
                LOG.exception(e)
                self._status = TASK_ERROR

        greenthread.spawn(_inner)
        return self
Esempio n. 17
0
    def start(self):
        self.done = event.Event()

        def _inner():
            """Function to do the image data transfer through an update
            and thereon checks if the state is 'active'.
            """
            try:
                self.image_service.update(self.context,
                                          self.image_id,
                                          self.image_meta,
                                          data=self.input)
                self._running = True
            except exception.ImageNotAuthorized as exc:
                self.done.send_exception(exc)

            while self._running:
                try:
                    image_meta = self.image_service.show(self.context,
                                                         self.image_id)
                    image_status = image_meta.get("status")
                    if image_status == "active":
                        self.stop()
                        self.done.send(True)
                    # If the state is killed, then raise an exception.
                    elif image_status == "killed":
                        self.stop()
                        msg = (_("Glance image %s is in killed state") %
                                 self.image_id)
                        LOG.error(msg)
                        self.done.send_exception(exception.CinderException(msg))
                    elif image_status in ["saving", "queued"]:
                        greenthread.sleep(GLANCE_POLL_INTERVAL)
                    else:
                        self.stop()
                        msg = _("Glance image "
                                    "%(image_id)s is in unknown state "
                                    "- %(state)s") % {
                                            "image_id": self.image_id,
                                            "state": image_status}
                        LOG.error(msg)
                        self.done.send_exception(exception.CinderException(msg))
                except Exception as exc:
                    self.stop()
                    self.done.send_exception(exc)

        greenthread.spawn(_inner)
        return self.done
def test_memory_leak():
    # The RequestScope holds references to GreenThread objects which would
    # cause memory leak
    app = Flask(__name__)

    FlaskInjector(app)

    @app.route('/')
    def index():
        return 'test'

    def get_request():
        with app.test_client() as c:
            c.get('/')

    green_thread = greenthread.spawn(get_request)
    green_thread.wait()
    # Delete green_thread so the GreenThread object is dereferenced
    del green_thread
    # Force run garbage collect to make sure GreenThread object is collected if
    # there is no memory leak
    gc.collect()
    greenthread_count = len([
        obj for obj in gc.get_objects()
        if type(obj) is greenthread.GreenThread])

    eq_(greenthread_count, 0)
Esempio n. 19
0
    def start(self, interval=None):
        def _inner():
            self.run()

        def _loopingcall_callback():
            self._monitor_busy = True
            try:
                self._check_pending_tasks()
            except Exception:
                LOG.exception(_("Exception in _check_pending_tasks"))
            self._monitor_busy = False

        if self._thread is not None:
            return self

        if interval is None or interval == 0:
            interval = self._interval

        self._stopped = False
        self._thread = greenthread.spawn(_inner)
        self._monitor = loopingcall.FixedIntervalLoopingCall(
            _loopingcall_callback)
        self._monitor.start(interval / 1000.0,
                            interval / 1000.0)
        # To allow the created thread start running
        greenthread.sleep(0)

        return self
Esempio n. 20
0
    def dispatch_lb(self, d_context, *args, **kwargs):
        item = d_context.item
        event = d_context.event
        n_context = d_context.n_context
        chain = d_context.chain

        item_id = item["id"]
        if event in self.handlers:
            for f in self.handlers[event]:
                first_run = False
                if item_id not in self.sync_items:
                    self.sync_items[item_id] = [queue.Queue()]
                    first_run = True
                self.sync_items[item_id][0].put(
                    ctx.OperationContext(event, n_context, item, chain, f,
                                         args, kwargs))
                if first_run:
                    t = greenthread.spawn(self._consume_lb,
                                          item_id,
                                          self.sync_items[item_id][0],
                                          self._driver,
                                          self._async)
                    self.sync_items[item_id].append(t)
                if not self._async:
                    t = self.sync_items[item_id][1]
                    t.wait()
Esempio n. 21
0
    def dispatch_l3(self, d_context, args=(), kwargs={}):
        item = d_context.item
        event = d_context.event
        n_context = d_context.n_context
        chain = d_context.chain

        item_id = item["id"]
        handlers = router_operations.handlers
        if event in handlers:
            for f in handlers[event]:
                first_run = False
                if item_id not in self.sync_items:
                    self.sync_items[item_id] = (queue.Queue(),)
                    first_run = True
                self.sync_items[item_id][0].put(
                    ctx.OperationContext(event, n_context, item, chain, f,
                                         args, kwargs))
                t = None
                if first_run:
                    t = greenthread.spawn(self._consume_l3,
                                          item_id,
                                          self.sync_items[item_id][0],
                                          self._plugin,
                                          self._async)
                    self.sync_items[item_id] += (t,)
                if not self._async:
                    t = self.sync_items[item_id][1]
                    t.wait()
Esempio n. 22
0
    def spawn(self, function, *args, **kwargs):
        """Run the *function* with its arguments in its own green thread.
        Returns the :class:`GreenThread <eventlet.greenthread.GreenThread>`
        object that is running the function, which can be used to retrieve the
        results.

        If the pool is currently at capacity, ``spawn`` will block until one of
        the running greenthreads completes its task and frees up a slot.

        This function is reentrant; *function* can call ``spawn`` on the same
        pool without risk of deadlocking the whole thing.
        """
        # if reentering an empty pool, don't try to wait on a coroutine freeing
        # itself -- instead, just execute in the current coroutine
        current = greenthread.getcurrent()
        if self.sem.locked() and current in self.coroutines_running:
            # a bit hacky to use the GT without switching to it
            gt = greenthread.GreenThread(current)
            gt.main(function, args, kwargs)
            return gt
        else:
            self.sem.acquire()
            gt = greenthread.spawn(function, *args, **kwargs)
            if not self.coroutines_running:
                self.no_coros_running = event.Event()
            self.coroutines_running.add(gt)
            gt.link(self._spawn_done)
        return gt
Esempio n. 23
0
    def start(self, interval=None):
        def _inner():
            self.run()

        def _loopingcall_callback():
            try:
                self._monitor_busy = True
                self._check_pending_tasks()
                self._monitor_busy = False
                if self._monitor_stop:
                    self._monitor_stop.send()
            except Exception:
                LOG.exception(_("Exception in _check_pending_tasks"))

        if self._thread:
            return self

        if interval is None or interval == 0:
            interval = self._interval

        self._thread = greenthread.spawn(_inner)
        self._monitor = loopingcall.FixedIntervalLoopingCall(_loopingcall_callback)
        self._monitor.start(interval / 1000.0, interval / 1000.0)

        return self
Esempio n. 24
0
    def start(self):
        self.done = event.Event()

        def _inner():
            """Initiate write thread.

            Function to do the image data transfer through an update
            and thereon checks if the state is 'active'.
            """
            LOG.debug(
                "Initiating image service update on image: %(image)s "
                "with meta: %(meta)s" % {"image": self.image_id, "meta": self.image_meta}
            )
            self.image_service.update(self.context, self.image_id, self.image_meta, data=self.input_file)
            self._running = True
            while self._running:
                try:
                    image_meta = self.image_service.show(self.context, self.image_id)
                    image_status = image_meta.get("status")
                    if image_status == "active":
                        self.stop()
                        LOG.debug("Glance image: %s is now active." % self.image_id)
                        self.done.send(True)
                    # If the state is killed, then raise an exception.
                    elif image_status == "killed":
                        self.stop()
                        msg = _("Glance image: %s is in killed state.") % self.image_id
                        LOG.error(msg)
                        excep = exception.CinderException(msg)
                        self.done.send_exception(excep)
                    elif image_status in ["saving", "queued"]:
                        greenthread.sleep(GLANCE_POLL_INTERVAL)
                    else:
                        self.stop()
                        msg = _("Glance image %(id)s is in unknown state " "- %(state)s") % {
                            "id": self.image_id,
                            "state": image_status,
                        }
                        LOG.error(msg)
                        excep = exception.CinderException(msg)
                        self.done.send_exception(excep)
                except Exception as exc:
                    self.stop()
                    self.done.send_exception(exc)

        greenthread.spawn(_inner)
        return self.done
Esempio n. 25
0
 def test_kill_meth(self):
     gt = greenthread.spawn(passthru, 6)
     gt.kill()
     self.assert_dead(gt)
     greenthread.sleep(0.001)
     self.assertEquals(_g_results, [])
     gt.kill()
     self.assert_dead(gt)
Esempio n. 26
0
 def _start(self, idle_for, initial_delay=None):
     if self._thread is not None:
         raise RuntimeError(self._RUN_ONLY_ONE_MESSAGE)
     self._running = True
     self.done = event.Event()
     self._thread = greenthread.spawn(self._run_loop, self._KIND, self.done, idle_for, initial_delay=initial_delay)
     self._thread.link(self._on_done)
     return self.done
Esempio n. 27
0
    def test_event_race(self):
        event = eventletutils.EventletEvent()

        def thread_a():
            self.assertTrue(event.wait(2))

        a = greenthread.spawn(thread_a)

        def thread_b():
            eventlet.sleep(0.1)
            event.clear()
            event.set()
            a.wait()

        b = greenthread.spawn(thread_b)
        with eventlet.timeout.Timeout(0.5):
            b.wait()
Esempio n. 28
0
    def start(self):
        self.done = event.Event()

        def _inner():
            """Initiate write thread.

            Function to do the image data transfer through an update
            and thereon checks if the state is 'active'.
            """
            self.image_service.update(self.context,
                                      self.image_id,
                                      self.image_meta,
                                      data=self.input)
            self._running = True
            while self._running:
                try:
                    image_meta = self.image_service.show(self.context,
                                                         self.image_id)
                    image_status = image_meta.get('status')
                    if image_status == 'active':
                        self.stop()
                        self.done.send(True)
                    # If the state is killed, then raise an exception.
                    elif image_status == 'killed':
                        self.stop()
                        msg = (_("Glance image: %s is in killed state.") %
                               self.image_id)
                        LOG.error(msg)
                        excep = exception.CinderException(msg)
                        self.done.send_exception(excep)
                    elif image_status in ['saving', 'queued']:
                        greenthread.sleep(GLANCE_POLL_INTERVAL)
                    else:
                        self.stop()
                        msg = _("Glance image %(id)s is in unknown state "
                                "- %(state)s") % {'id': self.image_id,
                                                  'state': image_status}
                        LOG.error(msg)
                        excep = exception.CinderException(msg)
                        self.done.send_exception(excep)
                except Exception as exc:
                    self.stop()
                    self.done.send_exception(exc)

        greenthread.spawn(_inner)
        return self.done
Esempio n. 29
0
    def start(self, interval):
        self._running = True

        def _inner():
            try:
                while self._running:
                    self.func(*self.args, **self.kwargs)
                    """It is important to check `self._running`'s value, if it is False, break."""
                    if not self._running:
                        break

                    greenthread.sleep(interval)
            except Exception:
                LOG.error("Periodic task %s running failed..." %
                          self.func.__name__)
                self.done.send_exception(*sys.exec_info())

        greenthread.spawn(_inner)
Esempio n. 30
0
    def start(self):

        def _inner():
            """Read data from the input and write the same to the output
            until the transfer completes.
            """
            try:
                LOG.debug("starting doing task")
                self.callback(*self.args, **self.kwargs)
                self._code = self.TASK_SUCCESS
                LOG.debug("ending doing task")
            except Exception as e:
                LOG.exception(e)
                self._code = self.TASK_ERROR
                self._msg = str(e.message)

        greenthread.spawn(_inner)
        return self
Esempio n. 31
0
 def _do_spawn(self, gtransport, protocol):
     greenthread.spawn(self._run_handler, gtransport, protocol)
Esempio n. 32
0
 def _wrapped_spawn_n(*args, **kwargs):
     rv = greenthread.spawn(*args, **kwargs)
     self._services.append(rv)
Esempio n. 33
0
    def start(self):
        """Start the image write task.

        :returns: the event indicating the status of the write task
        """
        self._done = event.Event()

        def _inner():
            """Task performing the image write operation.

            This method performs image data transfer through an update call.
            After the update, it waits until the image state becomes
            'active', 'killed' or unknown. If the final state is not 'active'
            an instance of ImageTransferException is thrown.

            :raises: ImageTransferException
            """
            LOG.debug(_("Calling image service update on image: %(image)s "
                        "with meta: %(meta)s"),
                      {'image': self._image_id,
                       'meta': self._image_meta})

            try:
                self._image_service.update(self._context,
                                           self._image_id,
                                           self._image_meta,
                                           data=self._input_file)
                self._running = True
                while self._running:
                    LOG.debug(_("Retrieving status of image: %s."),
                              self._image_id)
                    image_meta = self._image_service.show(self._context,
                                                          self._image_id)
                    image_status = image_meta.get('status')
                    if image_status == 'active':
                        self.stop()
                        LOG.debug(_("Image: %s is now active."),
                                  self._image_id)
                        self._done.send(True)
                    elif image_status == 'killed':
                        self.stop()
                        excep_msg = (_("Image: %s is in killed state.") %
                                     self._image_id)
                        LOG.error(excep_msg)
                        excep = exceptions.ImageTransferException(excep_msg)
                        self._done.send_exception(excep)
                    elif image_status in ['saving', 'queued']:
                        LOG.debug(_("Image: %(image)s is in %(state)s state; "
                                    "sleeping for %(sleep)d seconds."),
                                  {'image': self._image_id,
                                   'state': image_status,
                                   'sleep': IMAGE_SERVICE_POLL_INTERVAL})
                        greenthread.sleep(IMAGE_SERVICE_POLL_INTERVAL)
                    else:
                        self.stop()
                        excep_msg = _("Image: %(image)s is in unknown state: "
                                      "%(state)s.") % {'image': self._image_id,
                                                       'state': image_status}
                        LOG.error(excep_msg)
                        excep = exceptions.ImageTransferException(excep_msg)
                        self._done.send_exception(excep)
            except Exception as excep:
                self.stop()
                excep_msg = (_("Error occurred while writing image: %s") %
                             self._image_id)
                LOG.exception(excep_msg)
                excep = exceptions.ImageTransferException(excep_msg, excep)
                self._done.send_exception(excep)

        LOG.debug(_("Starting image write task for image: %(image)s with"
                    " source: %(source)s."),
                  {'source': self._input_file,
                   'image': self._image_id})
        greenthread.spawn(_inner)
        return self._done
Esempio n. 34
0
 def start(self, *args):
     self.event = threading.Event()
     self.is_running = True
     self.thread = greenthread.spawn(self.run)
Esempio n. 35
0
    def start(self):
        self.done = event.Event()

        def _inner():
            """Initiate write thread.

            Function to do the image data transfer through an update
            and thereon checks if the state is 'active'.
            """
            LOG.debug("Initiating image service update on image: %(image)s "
                      "with meta: %(meta)s" % {
                          'image': self.image_id,
                          'meta': self.image_meta
                      })

            try:
                self.image_service.update(self.context,
                                          self.image_id,
                                          self.image_meta,
                                          data=self.input_file)

                self._running = True
                while self._running:
                    image_meta = self.image_service.show(
                        self.context, self.image_id)
                    image_status = image_meta.get('status')
                    if image_status == 'active':
                        self.stop()
                        LOG.debug("Glance image: %s is now active." %
                                  self.image_id)
                        self.done.send(True)
                    # If the state is killed, then raise an exception.
                    elif image_status == 'killed':
                        self.stop()
                        msg = (_("Glance image: %s is in killed state.") %
                               self.image_id)
                        LOG.error(msg)
                        excep = error_util.ImageTransferException(msg)
                        self.done.send_exception(excep)
                    elif image_status in ['saving', 'queued']:
                        greenthread.sleep(GLANCE_POLL_INTERVAL)
                    else:
                        self.stop()
                        msg = _("Glance image %(id)s is in unknown state "
                                "- %(state)s") % {
                                    'id': self.image_id,
                                    'state': image_status
                                }
                        LOG.error(msg)
                        excep = error_util.ImageTransferException(msg)
                        self.done.send_exception(excep)
            except Exception as ex:
                self.stop()
                msg = (_("Error occurred while writing to image: %s") %
                       self.image_id)
                LOG.exception(msg)
                excep = error_util.ImageTransferException(ex)
                self.done.send_exception(excep)

        greenthread.spawn(_inner)
        return self.done
Esempio n. 36
0
def start_timer():
    return spawn(feed_timer)
Esempio n. 37
0
def feed_timer():
    while True:
        global current_feed
        current_feed = spawn(get_feed).wait()
        sleep(30)
Esempio n. 38
0
 def start_audit(self):
     LOG.info('Auditing interval %s' % CONF.certmon.audit_interval)
     utils.init_keystone_auth_opts()
     self.audit_thread = greenthread.spawn(self.audit_cert)
     self.on_start_audit()
Esempio n. 39
0
    def container_update(self, op, account, container, obj, request,
                         headers_out, objdevice, policy):
        """
        Update the container when objects are updated.

        :param op: operation performed (ex: 'PUT', or 'DELETE')
        :param account: account name for the object
        :param container: container name for the object
        :param obj: object name
        :param request: the original request object driving the update
        :param headers_out: dictionary of headers to send in the container
                            request(s)
        :param objdevice: device name that the object is in
        :param policy:  the BaseStoragePolicy instance
        """
        headers_in = request.headers
        conthosts = [
            h.strip()
            for h in headers_in.get('X-Container-Host', '').split(',')
        ]
        contdevices = [
            d.strip()
            for d in headers_in.get('X-Container-Device', '').split(',')
        ]
        contpartition = headers_in.get('X-Container-Partition', '')

        if len(conthosts) != len(contdevices):
            # This shouldn't happen unless there's a bug in the proxy,
            # but if there is, we want to know about it.
            self.logger.error(
                _('ERROR Container update failed: different '
                  'numbers of hosts and devices in request: '
                  '"%s" vs "%s"') % (headers_in.get('X-Container-Host', ''),
                                     headers_in.get('X-Container-Device', '')))
            return

        if contpartition:
            updates = zip(conthosts, contdevices)
        else:
            updates = []

        headers_out['x-trans-id'] = headers_in.get('x-trans-id', '-')
        headers_out['referer'] = request.as_referer()
        headers_out['X-Backend-Storage-Policy-Index'] = int(policy)
        update_greenthreads = []
        for conthost, contdevice in updates:
            gt = spawn(self.async_update,
                       op,
                       account,
                       container,
                       obj,
                       conthost,
                       contpartition,
                       contdevice,
                       headers_out,
                       objdevice,
                       policy,
                       logger_thread_locals=self.logger.thread_locals)
            update_greenthreads.append(gt)
        # Wait a little bit to see if the container updates are successful.
        # If we immediately return after firing off the greenthread above, then
        # we're more likely to confuse the end-user who does a listing right
        # after getting a successful response to the object create. The
        # `container_update_timeout` bounds the length of time we wait so that
        # one slow container server doesn't make the entire request lag.
        try:
            with Timeout(self.container_update_timeout):
                for gt in update_greenthreads:
                    gt.wait()
        except Timeout:
            # updates didn't go through, log it and return
            self.logger.debug(
                'Container update timeout (%.4fs) waiting for %s',
                self.container_update_timeout, updates)
Esempio n. 40
0
                while self._running:
                    self.f(*self.args, **self.kw)
                    greenthread.sleep(interval)
            except LoopingCallDone, e:
                self.stop()
                done.send(e.retvalue)
            except Exception:
                logging.exception('in looping call')
                done.send_exception(*sys.exc_info())
                return
            else:
                done.send(True)

        self.done = done

        greenthread.spawn(_inner)
        return self.done

    def stop(self):
        self._running = False

    def wait(self):
        return self.done.wait()


def xhtml_escape(value):
    """Escapes a string so it is valid within XML or XHTML.

    Code is directly from the utf8 function in
    http://github.com/facebook/tornado/blob/master/tornado/escape.py
Esempio n. 41
0
class GlanceWriteThread(object):
    """Ensures that image data is written to in the glance client and that
    it is in correct ('active')state."""
    def __init__(self,
                 context,
                 input,
                 image_service,
                 image_id,
                 image_meta=None):
        if not image_meta:
            image_meta = {}

        self.context = context
        self.input = input
        self.image_service = image_service
        self.image_id = image_id
        self.image_meta = image_meta
        self._running = False

    def start(self):
        self.done = event.Event()

        def _inner():
            """Function to do the image data transfer through an update
            and thereon checks if the state is 'active'."""
            self.image_service.update(self.context,
                                      self.image_id,
                                      self.image_meta,
                                      data=self.input)
            self._running = True
            while self._running:
                try:
                    image_meta = self.image_service.show(
                        self.context, self.image_id)
                    image_status = image_meta.get("status")
                    if image_status == "active":
                        self.stop()
                        self.done.send(True)
                    # If the state is killed, then raise an exception.
                    elif image_status == "killed":
                        self.stop()
                        msg = (_("Glance image %s is in killed state") %
                               self.image_id)
                        LOG.error(msg)
                        self.done.send_exception(exception.NovaException(msg))
                    elif image_status in ["saving", "queued"]:
                        greenthread.sleep(GLANCE_POLL_INTERVAL)
                    else:
                        self.stop()
                        msg = _("Glance image "
                                "%(image_id)s is in unknown state "
                                "- %(state)s") % {
                                    "image_id": self.image_id,
                                    "state": image_status
                                }
                        LOG.error(msg)
                        self.done.send_exception(exception.NovaException(msg))
                except Exception, exc:
                    self.stop()
                    self.done.send_exception(exc)

        greenthread.spawn(_inner)
        return self.done
Esempio n. 42
0
 def _do_spawn(self, gtransport, protocol):
     g = greenthread.spawn(self._run_handler, gtransport, protocol)
     self.greenlets.add(g)
     g.link(lambda *_: self.greenlets.remove(g))