cubicweb/hooks/logstats.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Wed, 29 Mar 2017 11:46:17 +0200
branch3.25
changeset 12113 669aaed98678
parent 11767 432f87a63057
permissions -rw-r--r--
[hooks] Do not register "logstats" if repository has no scheduler
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10308
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     1
# copyright 2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     3
#
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     5
#
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     9
# any later version.
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    10
#
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    14
# details.
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    15
#
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    18
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    19
"""looping task for dumping instance's stats in a file
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    20
"""
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    21
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    22
10308
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    23
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    24
from datetime import datetime
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    25
import json
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    26
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    27
from cubicweb.server import hook
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    28
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    29
class LogStatsStartHook(hook.Hook):
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    30
    """register task to regularly dump instance's stats in a file
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    31
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    32
    data are stored as one json entry per row
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    33
    """
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    34
    __regid__ = 'cubicweb.hook.logstats.start'
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    35
    events = ('server_startup',)
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    36
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    37
    def __call__(self):
12113
669aaed98678 [hooks] Do not register "logstats" if repository has no scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11767
diff changeset
    38
        if not self.repo.has_scheduler():
669aaed98678 [hooks] Do not register "logstats" if repository has no scheduler
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11767
diff changeset
    39
            return
10308
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    40
        interval = self.repo.config.get('logstat-interval', 0)
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    41
        if interval <= 0:
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    42
            return            
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    43
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    44
        def dump_stats(repo):
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    45
            statsfile = repo.config.get('logstat-file')
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    46
            with repo.internal_cnx() as cnx:
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    47
                stats = cnx.call_service('repo_stats')
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    48
                gcstats = cnx.call_service('repo_gc_stats', nmax=5)
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    49
                
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    50
            allstats = {'resources': stats,
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    51
                        'memory': gcstats,
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    52
                        'timestamp': datetime.utcnow().isoformat(),
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    53
                       }
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    54
            try:
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    55
                with open(statsfile, 'ab') as ofile:
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    56
                    json.dump(allstats, ofile)
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    57
                    ofile.write('\n')
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    58
            except IOError:
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    59
                repo.warning('Cannot open stats file for writing: %s', statsfile)
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    60
                    
3f94034cc972 [hooks] add a looping task that dumps the stats regularly in a file
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    61
        self.repo.looping_task(interval, dump_stats, self.repo)