当前位置: 首页>>代码示例>>Python>>正文


Python TimerService.stopService方法代码示例

本文整理汇总了Python中twisted.application.internet.TimerService.stopService方法的典型用法代码示例。如果您正苦于以下问题:Python TimerService.stopService方法的具体用法?Python TimerService.stopService怎么用?Python TimerService.stopService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twisted.application.internet.TimerService的用法示例。


在下文中一共展示了TimerService.stopService方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: stopService

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
 def stopService(self):
     """
     Stop this service. This will release buckets partitions it holds
     """
     TimerService.stopService(self)
     if self.kz_partition.acquired:
         return self.kz_partition.finish()
开发者ID:MariaAbrahms,项目名称:otter,代码行数:9,代码来源:scheduler.py

示例2: test_pickleTimerServiceNotPickleLoopFinished

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
 def test_pickleTimerServiceNotPickleLoopFinished(self):
     """
     When pickling L{internet.TimerService}, it won't pickle
     L{internet.TimerService._loopFinished}.
     """
     # We need a pickleable callable to test pickling TimerService. So we
     # can't use self.timer
     timer = TimerService(1, fakeTargetFunction)
     timer.startService()
     dumpedTimer = pickle.dumps(timer)
     timer.stopService()
     loadedTimer = pickle.loads(dumpedTimer)
     nothing = object()
     value = getattr(loadedTimer, "_loopFinished", nothing)
     self.assertIdentical(nothing, value)
开发者ID:alfonsjose,项目名称:international-orders-app,代码行数:17,代码来源:test_internet.py

示例3: stopService

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
 def stopService(self):
     if self.log_file is None:
         return defer.succeed(None)
     else:
         self.log_file.close()
         self.log_file = None
         return TimerService.stopService(self)
开发者ID:nickmerwin,项目名称:il2ds-middleware,代码行数:9,代码来源:service.py

示例4: RestoreRequestProcessor

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
class RestoreRequestProcessor(object):
    """
    The processor for the externally initiated restore requests.
    """
    __slots__ = ('__server_process', '__restore_timer')


    def __init__(self, server_process):
        assert isinstance(server_process, ServerProcess), \
               repr(server_process)
        self.__server_process = server_process

        self.__restore_timer = \
            TimerService(WEB_RESTORE_PERIOD.total_seconds(),
                         exceptions_logged(logger)(callInThread),
                         self.__poll_restore_requests_in_thread)


    def __poll_restore_requests_in_thread(self):
        """Perform another iteration of polling the restore requests."""
        assert not in_main_thread()

        poll_uuid = gen_uuid()
        logger.debug('Polling restore requests (%s)', poll_uuid)

        restore_request = True
        while restore_request is not None:
            with ds.FDB() as fdbw:
                restore_request = \
                    FDBQueries.RestoreRequests \
                              .atomic_start_oldest_restore_request(fdbw=fdbw)

            logger.debug('Poll (%s) returned %r', poll_uuid, restore_request)
            if restore_request is not None:
                # We've indeed have some restore request that needs processing.

                # Create new "virtual" dataset with all the data
                # to be restored.
                with db.RDB() as rdbw:
                    new_ds_uuid = \
                        Queries.Datasets.restore_files_to_dataset_clone(
                            restore_request.base_ds_uuid,
                            restore_request.paths,
                            restore_request.ts_start,
                            rdbw)

                # Now we know the new dataset to be restored.
                # Btw, write it into the docstore.
                # Doesn't need to be atomic, as only a single node
                # may be processing it at a time.
                with ds.FDB() as fdbw:
                    FDBQueries.RestoreRequests.set_ds_uuid(
                        _id=restore_request._id,
                        new_ds_uuid=new_ds_uuid,
                        fdbw=fdbw)

                # After creating the dataset, let's restore it to all host
                # which are alive.
                _syncer = self.__server_process.app.syncer
                _syncer.restore_dataset_to_lacking_hosts(
                    me=self.__server_process.me,
                    host=None,
                    ds_uuid=new_ds_uuid)

        logger.debug('Polling restore requests (%s) - done', poll_uuid)


    def start(self):
        """Start the processor."""
        assert in_main_thread()
        self.__restore_timer.startService()


    def stop(self):
        """Stop the processor."""
        assert in_main_thread()
        self.__restore_timer.stopService()
开发者ID:shvar,项目名称:redfs,代码行数:79,代码来源:restore_request_processor.py

示例5: MetricsService

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
class MetricsService(Service, object):
    """
    Service collects metrics on continuous basis
    """

    def __init__(self, reactor, config, log, clock=None, collect=None):
        """
        Initialize the service by connecting to Cassandra and setting up
        authenticator

        :param reactor: Twisted reactor for connection purposes
        :param dict config: All the config necessary to run the service.
            Comes from config file
        :param IReactorTime clock: Optional reactor for timer purpose
        """
        self._client = connect_cass_servers(reactor, config['cassandra'])
        self.log = log
        self.reactor = reactor
        self._divergent_groups = {}
        self.divergent_timeout = get_in(
            ['metrics', 'divergent_timeout'], config, 3600)
        self._service = TimerService(
            get_in(['metrics', 'interval'], config, default=60),
            collect or self.collect,
            reactor,
            config,
            self.log,
            client=self._client,
            authenticator=generate_authenticator(reactor, config['identity']))
        self._service.clock = clock or reactor

    @defer.inlineCallbacks
    def collect(self, *a, **k):
        try:
            metrics = yield collect_metrics(*a, **k)
            self._divergent_groups, to_log = unchanged_divergent_groups(
                self.reactor, self._divergent_groups, self.divergent_timeout,
                metrics)
            for group, duration in to_log:
                self.log.err(
                    ValueError(""),  # Need to give an exception to log err
                    ("Group {group_id} of {tenant_id} remains diverged "
                     "and unchanged for {divergent_time}"),
                    tenant_id=group.tenant_id, group_id=group.group_id,
                    desired=group.desired, actual=group.actual,
                    pending=group.pending,
                    divergent_time=str(timedelta(seconds=duration)))
        except Exception:
            self.log.err(None, "Error collecting metrics")

    def startService(self):
        """
        Start this service by starting internal TimerService
        """
        Service.startService(self)
        return self._service.startService()

    def stopService(self):
        """
        Stop service by stopping the timerservice and disconnecting cass client
        """
        Service.stopService(self)
        d = self._service.stopService()
        return d.addCallback(lambda _: self._client.disconnect())
开发者ID:dragorosson,项目名称:otter,代码行数:66,代码来源:metrics.py

示例6: AcmeIssuingService

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]

#.........这里部分代码省略.........
            Key(key.private_bytes(
                encoding=serialization.Encoding.PEM,
                format=serialization.PrivateFormat.TraditionalOpenSSL,
                encryption_algorithm=serialization.NoEncryption()))]

        def answer_and_poll(authzr):
            def got_challenge(stop_responding):
                return (
                    poll_until_valid(authzr, self._clock, client)
                    .addBoth(tap(lambda _: stop_responding())))
            return (
                answer_challenge(authzr, client, self._responders)
                .addCallback(got_challenge))

        def got_cert(certr):
            objects.append(
                Certificate(
                    x509.load_der_x509_certificate(
                        certr.body, default_backend())
                    .public_bytes(serialization.Encoding.PEM)))
            return certr

        def got_chain(chain):
            for certr in chain:
                got_cert(certr)
            log.info(
                'Received certificate for {server_name!r}.',
                server_name=server_name)
            return objects

        return (
            client.request_challenges(fqdn_identifier(server_name))
            .addCallback(answer_and_poll)
            .addCallback(lambda ign: client.request_issuance(
                CertificateRequest(
                    csr=csr_for_names([server_name], key))))
            .addCallback(got_cert)
            .addCallback(client.fetch_chain)
            .addCallback(got_chain)
            .addCallback(partial(self.cert_store.store, server_name)))

    def _ensure_registered(self):
        """
        Register if needed.
        """
        if self._registered:
            return succeed(None)
        else:
            return self._with_client(self._register)

    def _register(self, client):
        """
        Register and agree to the TOS.
        """
        def _registered(regr):
            self._regr = regr
            self._registered = True
        regr = messages.NewRegistration.from_data(email=self._email)
        return (
            client.register(regr)
            .addCallback(client.agree_to_tos)
            .addCallback(_registered))

    def when_certs_valid(self):
        """
        Get a notification once the startup check has completed.

        When the service starts, an initial check is made immediately; the
        deferred returned by this function will only fire once reissue has been
        attempted for any certificates within the panic interval.

        ..  note:: The reissue for any of these certificates may not have been
            successful; the panic callback will be invoked for any certificates
            in the panic interval that failed reissue.

        :rtype: ``Deferred``
        :return: A deferred that fires once the initial check has resolved.
        """
        if self.ready:
            return succeed(None)
        d = Deferred()
        self._waiting.append(d)
        return d

    def startService(self):
        Service.startService(self)
        self._registered = False
        self._timer_service = TimerService(
            self.check_interval.total_seconds(), self._check_certs)
        self._timer_service.clock = self._clock
        self._timer_service.startService()

    def stopService(self):
        Service.stopService(self)
        self.ready = False
        self._registered = False
        for d in list(self._waiting):
            d.cancel()
        self._waiting = []
        return self._timer_service.stopService()
开发者ID:mithrandi,项目名称:txacme,代码行数:104,代码来源:service.py

示例7: TimerServiceTests

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
class TimerServiceTests(TestCase):
    """
    Tests for L{twisted.application.internet.TimerService}.

    @type timer: L{TimerService}
    @ivar timer: service to test

    @type clock: L{task.Clock}
    @ivar clock: source of time

    @type deferred: L{Deferred}
    @ivar deferred: deferred returned by L{TimerServiceTests.call}.
    """

    def setUp(self):
        """
        Set up a timer service to test.
        """
        self.timer = TimerService(2, self.call)
        self.clock = self.timer.clock = task.Clock()
        self.deferred = Deferred()


    def call(self):
        """
        Function called by L{TimerService} being tested.

        @returns: C{self.deferred}
        @rtype: L{Deferred}
        """
        return self.deferred


    def test_startService(self):
        """
        When L{TimerService.startService} is called, it marks itself
        as running, creates a L{task.LoopingCall} and starts it.
        """
        self.timer.startService()
        self.assertTrue(self.timer.running, "Service is started")
        self.assertIsInstance(self.timer._loop, task.LoopingCall)
        self.assertIdentical(self.clock, self.timer._loop.clock)
        self.assertTrue(self.timer._loop.running, "LoopingCall is started")


    def test_startServiceRunsCallImmediately(self):
        """
        When L{TimerService.startService} is called, it calls the function
        immediately.
        """
        result = []
        self.timer.call = (result.append, (None,), {})
        self.timer.startService()
        self.assertEqual([None], result)


    def test_startServiceUsesGlobalReactor(self):
        """
        L{TimerService.startService} uses L{internet._maybeGlobalReactor} to
        choose the reactor to pass to L{task.LoopingCall}
        uses the global reactor.
        """
        otherClock = task.Clock()
        def getOtherClock(maybeReactor):
            return otherClock
        self.patch(internet, "_maybeGlobalReactor", getOtherClock)
        self.timer.startService()
        self.assertIdentical(otherClock, self.timer._loop.clock)


    def test_stopServiceWaits(self):
        """
        When L{TimerService.stopService} is called while a call is in progress.
        the L{Deferred} returned doesn't fire until after the call finishes.
        """
        self.timer.startService()
        d = self.timer.stopService()
        self.assertNoResult(d)
        self.assertEqual(True, self.timer.running)
        self.deferred.callback(object())
        self.assertIdentical(self.successResultOf(d), None)


    def test_stopServiceImmediately(self):
        """
        When L{TimerService.stopService} is called while a call isn't in progress.
        the L{Deferred} returned has already been fired.
        """
        self.timer.startService()
        self.deferred.callback(object())
        d = self.timer.stopService()
        self.assertIdentical(self.successResultOf(d), None)


    def test_failedCallLogsError(self):
        """
        When function passed to L{TimerService} returns a deferred that errbacks,
        the exception is logged, and L{TimerService.stopService} doesn't raise an error.
        """
        self.timer.startService()
#.........这里部分代码省略.........
开发者ID:alfonsjose,项目名称:international-orders-app,代码行数:103,代码来源:test_internet.py

示例8: BackupScheduler

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
class BackupScheduler(object):
    """The container for the backup schedules.

    Internally, it keeps the list of schedules in always-sorted state;
    they are sorted by the expected time of fire (then by UUID;
    but two schedules with the same UUID always match).

    @todo: The scheduler must be dynamically updated:
           1. if some "backup scheduled" settings are changed;
           2. if some "timezone" setting is changed.

    @note: schedule support is disabled, so this class is not used anymore.
    """
    __slots__ = ('server_process', 'lock',
                 '__schedules', '__schedules_by_host_uuid',
                 '__schedule_check_timer', '__last_reread_from_db')


    @exceptions_logged(logger)
    def __init__(self, server_process):
        assert isinstance(server_process, ServerProcess), \
               repr(server_process)

        self.server_process = server_process
        self.lock = RLock()

        self.__schedule_check_timer = \
            TimerService(BACKUP_SCHEDULES_CHECK_PERIOD.total_seconds(),
                         self.__on_schedule_check_timer)

        self.reread_cache()


    def __repr__(self):
        return u'<BackupScheduler: {} schedule(s)>'\
                   .format(len(self.__schedules))


    @property
    def app(self):
        """
        @rtype: NodeApp
        """
        return self.server_process.app


    @exceptions_logged(logger)
    def __reset(self):
        self.__schedules = []
        self.__schedules_by_host_uuid = defaultdict(set)


    @exceptions_logged(logger)
    def start(self):
        self.__schedule_check_timer.startService()


    @exceptions_logged(logger)
    def stop(self):
        """
        @todo: It is nowhere stopped at the moment, should it be?
        """
        self.__schedule_check_timer.stopService()


    @contract_epydoc
    def add(self, schedule):
        """
        @type schedule: BackupSchedule
        """
        with self.lock:
            assert schedule not in self.__schedules, repr(schedule)
            bisect.insort(self.__schedules, schedule)
            self.__schedules_by_host_uuid[schedule.host_uuid].add(schedule)
            assert self.__schedules == sorted(self.__schedules), 'Not sorted!'


    @contract_epydoc
    def remove(self, schedule):
        """
        @type schedule: BackupSchedule
        """
        with self.lock:
            assert schedule in self.__schedules, repr(schedule)
            index = bisect.bisect(self.__schedules, schedule) - 1
            assert schedule == self.__schedules[index], \
                   (index, schedule, self.__schedules)
            del self.__schedules[index]
            self.__schedules_by_host_uuid[schedule.host_uuid].remove(schedule)
            assert self.__schedules == sorted(self.__schedules), 'Not sorted!'


    @contract_epydoc
    def get_schedules_by_host_uuid(self, host_uuid):
        """
        @type host_uuid: UUID
        @rtype: frozenset
        """
        return frozenset(self.__schedules_by_host_uuid[host_uuid])

#.........这里部分代码省略.........
开发者ID:shvar,项目名称:redfs,代码行数:103,代码来源:backup_scheduler.py

示例9: stopService

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
 def stopService(self):
     log.msg("Shutting down script runner service ...")
     return TimerService.stopService(self)
开发者ID:jonobacon,项目名称:ubuntu-accomplishments-daemon,代码行数:5,代码来源:service.py

示例10: TrafficMeter

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
class TrafficMeter(object):
    """
    Traffic meter, measuring all the traffic to some direction.

    @note: At the moment, restart of the service (after it has been started)
           is not supported.

    @ivar __start_time: the time of the first registered traffic "tick".
    @type __start_time: datetime

    @ivar __ticks: the sequence of TrafficTick objects
                   (always sorted by the time, increasing!)
                   registering each successfully passed bunch of bytes.
    @invariant: consists_of(self.__ticks, TrafficTick)
    """
    __slots__ = ('__direction_incoming', '__total_bytes', '__ticks',
                 '__ticks_lock', '__start_time', '__timer_service',
                 '__started')


    def __init__(self, is_direction_incoming):
        """
        @param is_direction_incoming: whether the measured direction
            is incoming (otherwise outgoing).
        @type is_direction_incoming: bool
        """
        self.__direction_incoming = is_direction_incoming
        self.__total_bytes = 0
        self.__ticks = deque()
        self.__ticks_lock = Lock()
        self.__start_time = None
        self.__timer_service = TimerService(
                                   TRAFFIC_NOTIFICATION_PERIOD.total_seconds(),
                                   self.__on_timer)
        self.__started = False


    @property
    def started(self):
        return self.__started


    def start(self):
        """
        Start monitoring and reporting.
        """
        with self.__ticks_lock:
            assert not self.__started
            self.__timer_service.startService()
            self.__started = True
            logger.debug('%s traffic meter started', self._name)


    @property
    def _name(self):
        return 'Inbound' if self.__direction_incoming else 'Outbound'


    def stop(self):
        """
        Stop monitoring and reporting.
        """
        with self.__ticks_lock:
            assert self.__started
            self.__started = False
            self.__timer_service.stopService()
            logger.debug('%s traffic meter stopped', self._name)


    @exceptions_logged(logger)
    def __on_timer(self):
        _now = datetime.utcnow()
        _oldest_time_to_consider = _now - THROUGHPUT_SMA_PERIOD

        # Even if the timer is started, we don't report throughput
        # until the first ticks are registered.
        if self.__ticks:
            with self.__ticks_lock:
                # Remove (leftmost) ticks which are too old to consider.
                while (self.__ticks and
                       self.__ticks[0].time < _oldest_time_to_consider):
                    self.__ticks.popleft()

                # All the remaining ticks now serve for the throughput
                # calculation, and comprise the current SMA window.
                sma_kilobytes = sum(i.bytes for i in self.__ticks) / 1000.0

                # (btw, done with the lock, remaining calculation
                # can be unlocked)

            # How much time passed since the traffic meter has been started?
            uptime_td = TimeDeltaEx.from_timedelta(_now - self.__start_time)

            # If the meter has just been started,
            # we calculate the throughput dividing to the actual uptime
            # rather than the period window.
            sma_td = min(uptime_td, THROUGHPUT_SMA_PERIOD)
            sma_seconds = sma_td.in_seconds()

            kBps = sma_kilobytes / sma_seconds
#.........这里部分代码省略.........
开发者ID:shvar,项目名称:redfs,代码行数:103,代码来源:traffic_meter.py

示例11: FeedPollerService

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
class FeedPollerService(Service):
    """
    Polls AtomHopper feeds
    """
    def __init__(self, agent, url, event_listeners, interval=DEFAULT_INTERVAL,
                 state_store=None,
                 TimerService=TimerService, coiterate=coiterate):
        """
        :param agent: a :class:`twisted.web.client.Agent` to use to poll

        :param url: the url to poll

        :param event_listeners: listeners that handle a particular event
        :type event_listeners: `iterable` of `callables` that take an event
            as an argument

        :param interval: how often to poll, given in seconds - defaults to 10
        :type interval: ``int`` or ``float``

        :param state_store: where to store the current polling state
        :type state_store: :class:`otter.indexer.state.IStateStore` provider

        :param TimerService: factory (not instance) that produces something
            like a :class:`twisted.application.internet.TimerService` -
            defaults to :class:`twisted.application.internet.TimerService`
            (this parameter is mainly used for dependency injection for
            testing)
        :type TimerService: ``callable``

        :param coiterate: function that is used to coiterate tasks - defaults
            to :func:`twisted.internet.task.coiterate` - (this parameter is
            mainly used for dependency injection for testing)
        :type coiterate: ``callable``
        """
        self._url = url
        self._interval = interval

        self._timer_service = TimerService(interval, self._do_poll)

        self._next_url = None

        self._agent = agent
        self._state_store = state_store or DummyStateStore()

        self._event_listeners = event_listeners
        self._poll_timer = timer('FeedPollerService.poll.{0}'.format(url))
        self._fetch_timer = timer('FeedPollerService.fetch.{0}'.format(url))

        self._coiterate = coiterate

    def startService(self):
        """
        Start the feed polling service - called by the twisted
        application when starting up
        """
        self._timer_service.startService()

    def stopService(self):
        """
        Stop the feed polling service - called by the twisted
        application when shutting down

        :return: ``Deferred``
        """
        return self._timer_service.stopService()

    def _fetch(self, url):
        """
        Get atom feed from AtomHopper url
        """
        def _parse(data):
            e = parse(data)
            return e

        def _gotResponse(resp):
            br = _BodyReceiver()

            resp.deliverBody(br)

            return br.finish

        log.msg(format="Fetching url: %(url)r", url=url)
        d = self._agent.request('GET', url, Headers({}), None)
        d.addCallback(_gotResponse)
        d.addCallback(_parse)

        return d

    def _do_poll(self):
        """
        Do one interation of polling AtomHopper.
        """
        start = time.time()

        def _get_next_url(feed):
            self._fetch_timer.update(time.time() - start)
            # next is previous, because AtomHopper is backwards in time
            next_url = previous_link(feed)

            if next_url is not None:
#.........这里部分代码省略.........
开发者ID:MariaAbrahms,项目名称:otter,代码行数:103,代码来源:poller.py

示例12: stopService

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]
 def stopService(self):
    ## bug in TimerService if you stop when hasnt yet started...
    ## because of condition "not hasattr(self, '_loop')"
    if self.running:
       return TimerService.stopService(self)
开发者ID:istobran,项目名称:eaEmu,代码行数:7,代码来源:timer.py

示例13: migrate_chunks

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]

#.........这里部分代码省略.........

        timer_service = TimerService(1.0, timercb)

        # If the task takes more than 3 seconds, start notifying
        # about the progress
        _callLater = reactor.callLater  # pylint:disable=E1101,C0103
        # Won't worry about deferToThread here, cause it is very fast.
        long_task_timer = _callLater(3.0, timer_service.startService)

        with self.__chunk_op_lock:
            try:
                # What chunk files are present on the FS,
                # and what are the chunk UUIDs?
                present_chunk_uuids_iter = self.__get_chunk_uuids_on_fs()

                with db.RDB() as rdbw:
                    dummy_chunks_in_db = \
                        frozenset(HostQueries.HostChunks
                                             .get_all_dummy_chunk_uuids(
                                                  rdbw=rdbw))

                # First, remove all the dummy chunks
                removed_dummy_chunks = []
                for dummy_chunk_uuid in dummy_chunks_in_db:
                    try:
                        assert self.__get_chunk_file_path(dummy_chunk_uuid,
                                                          is_dummy=True,
                                                          dir_path=old_path) \
                               == self.__get_chunk_file_path(dummy_chunk_uuid,
                                                             is_dummy=True)

                        _path = self.__get_chunk_file_path(dummy_chunk_uuid,
                                                           is_dummy=True)
                        if os.path.exists(_path):
                            os.unlink(_path)
                        # If we removed the file successfully, let's append it
                        # to the list of the chunks which are to be removed
                        # from the DB.
                        removed_dummy_chunks.append(dummy_chunk_uuid)
                    except Exception as e:
                        logger.error('Cannot remove dummy chunk %s: %s',
                                     dummy_chunk_uuid, e)
                HostQueries.HostChunks \
                           .delete_dummy_chunks(removed_dummy_chunks)

                # This dictionary maps the chunk UUID
                # to a tuple of the old filename and the new filename.
                #
                # Btw, no need to convert present_chunk_uuids_iter to set
                # and do the set difference, as it is the same complexity
                # as for ... if not in.
                uuid_to_filenames = \
                    {u: (self.__get_chunk_file_path(u,
                                                    is_dummy=False,
                                                    dir_path=old_path),
                         self.__get_chunk_file_path(u,
                                                    is_dummy=False,
                                                    dir_path=new_path))
                         for u in present_chunk_uuids_iter
                         if u not in dummy_chunks_in_db}

                # Now, move the files to the new directory.
                of = len(uuid_to_filenames)
                for u, (old_filename, new_filename) \
                        in uuid_to_filenames.iteritems():

                    logger.debug('Moving chunk %s from %s to %s',
                                 u, old_filename, new_filename)

                    try:
                        with open(old_filename, 'rb') as rfh:
                            with open_wb(new_filename) as wfh:
                                wfh.write(rfh.read())
                    except Exception:
                        logger.error('Cannot move chunk %s from %s to %s',
                                     u, old_filename, new_filename)
                    else:
                        try:
                            os.unlink(old_filename)
                        except Exception:
                            logger.error('Cannot remove chunk file %s',
                                         old_filename)

                    num += 1

            except Exception as e:
                logger_status_chunks_op_error.error(
                    'The chunks migration failed: %r',
                    e,
                    extra={'_type': 'chunks_migration.error',
                           '_exc': e,
                           '_tb': traceback.format_exc()})

            finally:
                if (not long_task_timer.called and
                    not long_task_timer.cancelled):
                    long_task_timer.cancel()

                if timer_service.running:
                    timer_service.stopService()
开发者ID:shvar,项目名称:redfs,代码行数:104,代码来源:chunk_storage_fs.py

示例14: update_dummy_chunks_size

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]

#.........这里部分代码省略.........
        timer_service = TimerService(1.0, timercb)

        # If the task takes more than 3 seconds,
        # start notifying about the progress
        _callLater = reactor.callLater  # pylint:disable=E1101,C0103
        # Won't worry about deferToThread here, cause it is very fast.
        long_task_timer = _callLater(3.0, timer_service.startService)

        logger.debug('Resizing dummy chunk set from %s to %s',
                     old_limit_mib, new_limit_mib)

        with self.__chunk_op_lock:
            try:
                # Check for dummy chunks before the check for present files,
                # as the check for dummy chunks also may remove some
                # of the files.

                # What dummy chunks are available?
                # list, so it can be used twice.
                # TODO: do we need to use it twice?
                dummy_chunk_uuids = list(self.__get_dummy_chunk_uuids_on_fs())
                how_many_dummy_chunks = len(dummy_chunk_uuids)

                # What chunk files are present on the FS,...
                present_chunk_filenames_iter = \
                    self.__get_chunk_filenames_on_fs(self.__chunk_dir)
                # ... and what are the chunk UUIDs?
                # present_chunk_uuids = \
                #     self.__class__.convert_chunk_filenames_to_uuids(
                #         present_chunk_filenames)

                # How many bytes/MiB do we need to have preallocated?
                reserved_mib = long(new_limit_mib)
                reserved_size = reserved_mib * 0x100000

                # How many bytes/MiB is preallocated already?
                present_chunk_size = \
                    sum(os.stat(f).st_size
                            for f in present_chunk_filenames_iter)
                del present_chunk_filenames_iter  # help GC
                present_chunks_in_mib = \
                    round_up_to_multiply(present_chunk_size,
                                         0x100000) // 0x100000

                if reserved_mib > present_chunks_in_mib:
                    # Add new chunks
                    how_many_new_chunks = reserved_mib - present_chunks_in_mib
                    of = how_many_new_chunks
                    _operation = 'allocation'

                    for u in self.__create_some_dummy_chunks(
                                 how_many_new_chunks):
                        num += 1

                elif reserved_mib < present_chunks_in_mib:
                    # Try to remove some dummy chunks...
                    how_many_chunks_try_to_remove = \
                        present_chunks_in_mib - reserved_mib

                    # But we cannot remove more than len(dummy_chunk_uuids)!
                    if how_many_dummy_chunks < how_many_chunks_try_to_remove:
                        logger.debug('Trying to remove %i chunks, '
                                         'but only %i dummy chunks available!',
                                     how_many_chunks_try_to_remove,
                                     how_many_dummy_chunks)

                    how_many_chunks_to_remove = \
                        min(how_many_chunks_try_to_remove,
                            how_many_dummy_chunks)
                    of = how_many_chunks_to_remove
                    _operation = 'removing'

                    chunk_uuids_to_delete = take(how_many_chunks_to_remove,
                                                 dummy_chunk_uuids)

                    for u in self.delete_some_dummy_chunks(
                                 how_many_chunks_to_remove,
                                 chunk_uuids_to_delete):
                        num += 1

            except Exception as e:
                logger_status_chunks_op_error.error(
                    'The chunk %s failed: %r',
                    _operation, e,
                    extra={'_type': 'chunks_allocation.error',
                           '_exc': e,
                           '_tb': traceback.format_exc()})

            finally:
                # We've done with the chunks allocation.
                # Now stop the timer, and manually report that 100%
                # of the work is done.
                if (not long_task_timer.called and
                    not long_task_timer.cancelled):
                    long_task_timer.cancel()

                if timer_service.running:
                    timer_service.stopService()

                timercb()
开发者ID:shvar,项目名称:redfs,代码行数:104,代码来源:chunk_storage_fs.py

示例15: AcmeIssuingService

# 需要导入模块: from twisted.application.internet import TimerService [as 别名]
# 或者: from twisted.application.internet.TimerService import stopService [as 别名]

#.........这里部分代码省略.........
    def _issue_cert(self, server_name):
        """
        Issue a new cert for a particular name.
        """
        key = self._generate_key()
        objects = [
            Key(key.private_bytes(
                encoding=serialization.Encoding.PEM,
                format=serialization.PrivateFormat.TraditionalOpenSSL,
                encryption_algorithm=serialization.NoEncryption()))]

        def answer_and_poll(authzr):
            def got_challenge(r):
                responder, response = r

                def stop_responding(result):
                    return responder.stop_responding(response)

                return (
                    poll_until_valid(authzr, self._clock, self._client)
                    .addBoth(tap(stop_responding)))
            return (
                answer_challenge(authzr, self._client, self._responders)
                .addCallback(got_challenge))

        def got_cert(certr):
            objects.append(
                Certificate(
                    x509.load_der_x509_certificate(
                        certr.body, default_backend())
                    .public_bytes(serialization.Encoding.PEM)))
            return certr

        def got_chain(chain):
            for certr in chain:
                got_cert(certr)
            return objects

        return (
            self._client.request_challenges(fqdn_identifier(server_name))
            .addCallback(answer_and_poll)
            .addCallback(lambda ign: self._client.request_issuance(
                CertificateRequest(
                    csr=csr_for_names([server_name], key))))
            .addCallback(got_cert)
            .addCallback(self._client.fetch_chain)
            .addCallback(got_chain)
            .addCallback(partial(self.cert_store.store, server_name)))

    def _register(self):
        """
        Register if needed.
        """
        def _registered(ign):
            self._registered = True
        if self._registered:
            return succeed(None)
        else:
            return (
                self._client.register()
                .addCallback(self._client.agree_to_tos)
                .addCallback(_registered))

    def when_certs_valid(self):
        """
        Get a notification once the startup check has completed.

        When the service starts, an initial check is made immediately; the
        deferred returned by this function will only fire once reissue has been
        attempted for any certificates within the panic interval.

        ..  note:: The reissue for any of these certificates may not have been
            successful; the panic callback will be invoked for any certificates
            in the panic interval that failed reissue.

        :rtype: ``Deferred``
        :return: A deferred that fires once the initial check has resolved.
        """
        if self.ready:
            return succeed(None)
        d = Deferred()
        self._waiting.append(d)
        return d

    def startService(self):
        Service.startService(self)
        self._registered = False
        self._timer_service = TimerService(
            self.check_interval.total_seconds(), self._check_certs)
        self._timer_service.clock = self._clock
        self._timer_service.startService()

    def stopService(self):
        Service.stopService(self)
        self.ready = False
        self._registered = False
        for d in list(self._waiting):
            d.cancel()
        self._waiting = []
        return self._timer_service.stopService()
开发者ID:habnabit,项目名称:txacme,代码行数:104,代码来源:service.py


注:本文中的twisted.application.internet.TimerService.stopService方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。