Changeset e424270 in trunk


Ignore:
Timestamp:
2019-03-08T12:46:36Z (6 years ago)
Author:
GitHub <noreply@…>
Branches:
master
Children:
c45b91e
Parents:
c8a3dbf (diff), 39694fc (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Jean-Paul Calderone <exarkun@…> (2019-03-08 12:46:36)
git-committer:
GitHub <noreply@…> (2019-03-08 12:46:36)
Message:

Merge pull request #563 from tahoe-lafs/2987.log_call_deferred

Add Eliot logging helper

Fixes: ticket:2987

Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/frontends/magic_folder.py

    rc8a3dbf re424270  
    17651765        return d.addActionFinish()
    17661766
     1767    @eliotutil.log_call_deferred(SCAN_REMOTE_COLLECTIVE.action_type)
    17671768    def _scan_remote_collective(self, scan_self=False):
    17681769        scan_batch = {}  # path -> [(filenode, metadata)]
    1769         with SCAN_REMOTE_COLLECTIVE().context():
    1770             d = DeferredContext(self._collective_dirnode.list())
     1770        d = DeferredContext(self._collective_dirnode.list())
    17711771        def scan_collective(dirmap):
    17721772            d2 = DeferredContext(defer.succeed(None))
     
    18071807
    18081808        d.addCallback(_filter_batch_to_deque)
    1809         return d.addActionFinish()
     1809        return d.result
    18101810
    18111811    def _scan_delay(self):
    18121812        return self._poll_interval
    18131813
     1814    @eliotutil.log_call_deferred(PERFORM_SCAN.action_type)
    18141815    @eliotutil.inline_callbacks
    18151816    def _perform_scan(self):
    1816         with PERFORM_SCAN():
    1817             try:
    1818                 yield self._scan_remote_collective()
    1819                 self._status_reporter(
    1820                     True, 'Magic folder is working',
    1821                     'Last scan: %s' % self.nice_current_time(),
    1822                 )
    1823             except Exception as e:
    1824                 write_traceback()
    1825                 self._status_reporter(
    1826                     False, 'Remote scan has failed: %s' % str(e),
    1827                     'Last attempted at %s' % self.nice_current_time(),
    1828                 )
     1817        try:
     1818            yield self._scan_remote_collective()
     1819            self._status_reporter(
     1820                True, 'Magic folder is working',
     1821                'Last scan: %s' % self.nice_current_time(),
     1822            )
     1823        except Exception as e:
     1824            write_traceback()
     1825            self._status_reporter(
     1826                False, 'Remote scan has failed: %s' % str(e),
     1827                'Last attempted at %s' % self.nice_current_time(),
     1828            )
    18291829
    18301830    def _process(self, item):
  • TabularUnified src/allmydata/test/test_eliotutil.py

    rc8a3dbf re424270  
    2222from testtools.matchers import (
    2323    Is,
     24    IsInstance,
    2425    MatchesStructure,
    2526    Equals,
     
    2930    has_no_result,
    3031    succeeded,
     32    failed,
    3133)
    3234
     
    5658    eliot_friendly_generator_function,
    5759    inline_callbacks,
     60    log_call_deferred,
    5861    _parse_destination_description,
    5962    _EliotLogging,
     
    528531            ),
    529532        )
     533
     534class LogCallDeferredTests(TestCase):
     535    """
     536    Tests for ``log_call_deferred``.
     537    """
     538    @capture_logging(
     539        lambda self, logger:
     540        assertHasAction(self, logger, u"the-action", succeeded=True),
     541    )
     542    def test_return_value(self, logger):
     543        """
     544        The decorated function's return value is passed through.
     545        """
     546        result = object()
     547        @log_call_deferred(action_type=u"the-action")
     548        def f():
     549            return result
     550        self.assertThat(f(), succeeded(Is(result)))
     551
     552    @capture_logging(
     553        lambda self, logger:
     554        assertHasAction(self, logger, u"the-action", succeeded=False),
     555    )
     556    def test_raise_exception(self, logger):
     557        """
     558        An exception raised by the decorated function is passed through.
     559        """
     560        class Result(Exception):
     561            pass
     562        @log_call_deferred(action_type=u"the-action")
     563        def f():
     564            raise Result()
     565        self.assertThat(
     566            f(),
     567            failed(
     568                AfterPreprocessing(
     569                    lambda f: f.value,
     570                    IsInstance(Result),
     571                ),
     572            ),
     573        )
  • TabularUnified src/allmydata/test/test_magic_folder.py

    rc8a3dbf re424270  
    4242from ..util.eliotutil import (
    4343    inline_callbacks,
     44    log_call_deferred,
    4445)
    4546
     
    524525        self._fake_inotify = inject_events
    525526
     527    @log_call_deferred(action_type=u"fileops:move")
    526528    def move(self, from_path_u, to_path_u):
    527529        from_fname = from_path_u
     
    535537        return d
    536538
     539    @log_call_deferred(action_type=u"fileops:write")
    537540    def write(self, path_u, contents):
    538541        fname = path_u
     
    547550        return d
    548551
     552    @log_call_deferred(action_type=u"fileops:mkdir")
    549553    def mkdir(self, path_u):
    550554        fname = path_u
     
    554558        return d
    555559
     560    @log_call_deferred(action_type=u"fileops:delete")
    556561    def delete(self, path_u):
    557562        fname = path_u
  • TabularUnified src/allmydata/util/eliotutil.py

    rc8a3dbf re424270  
    5454    ILogger,
    5555    Message,
     56    Field,
    5657    FileDestination,
    5758    add_destinations,
    5859    remove_destination,
    5960    write_traceback,
    60 )
    61 from eliot import (
    62     Field,
     61    start_action,
    6362)
    6463from eliot._validation import (
    6564    ValidationError,
    6665)
     66from eliot.twisted import DeferredContext
    6767
    6868from twisted.python.usage import (
     
    8282from twisted.internet.defer import (
    8383    inlineCallbacks,
     84    maybeDeferred,
    8485)
    8586from twisted.application.service import Service
     
    471472
    472473_parse_destination_description = _DestinationParser().parse
     474
     475def log_call_deferred(action_type):
     476    """
     477    Like ``eliot.log_call`` but for functions which return ``Deferred``.
     478    """
     479    def decorate_log_call_deferred(f):
     480        @wraps(f)
     481        def logged_f(*a, **kw):
     482            # Use the action's context method to avoid ending the action when
     483            # the `with` block ends.
     484            with start_action(action_type=action_type).context():
     485                # Use addActionFinish so that the action finishes when the
     486                # Deferred fires.
     487                d = maybeDeferred(f, *a, **kw)
     488                return DeferredContext(d).addActionFinish()
     489        return logged_f
     490    return decorate_log_call_deferred
Note: See TracChangeset for help on using the changeset viewer.