~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/apachelogparser/base.py

[r=gmb][ui=none][bug=588288] log parser should not read entire
        remaining file contents into memory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
from contrib import apachelog
13
13
 
14
 
from lp.services.apachelogparser.model.parsedapachelog import ParsedApacheLog
 
14
from canonical.config import config
15
15
from canonical.launchpad.interfaces.geoip import IGeoIP
16
16
from canonical.launchpad.webapp.interfaces import (
17
17
    IStoreSelector, MAIN_STORE, DEFAULT_FLAVOR)
 
18
from lp.services.apachelogparser.model.parsedapachelog import ParsedApacheLog
18
19
 
19
20
 
20
21
parser = apachelog.parser(apachelog.formats['extended'])
84
85
    """
85
86
    # Seek file to given position, read all lines.
86
87
    fd.seek(start_position)
87
 
    lines = fd.readlines()
88
 
    # Always skip the last line as it may be truncated since we're rsyncing
89
 
    # live logs.
90
 
    last_line = lines.pop(-1)
 
88
    line = fd.readline()
 
89
 
91
90
    parsed_bytes = start_position
92
 
    if len(lines) == 0:
93
 
        # This probably means we're dealing with a logfile that has been
94
 
        # rotated already, so it should be safe to parse its last line.
95
 
        lines = [last_line]
96
91
 
97
92
    geoip = getUtility(IGeoIP)
98
93
    downloads = {}
99
 
    for line in lines:
100
 
        try:
 
94
    parsed_lines = 0
 
95
 
 
96
    # Check for an optional max_parsed_lines config option.
 
97
    max_parsed_lines = getattr(
 
98
        config.launchpad, 'logparser_max_parsed_lines', None)
 
99
 
 
100
    while line:
 
101
        if max_parsed_lines is not None and parsed_lines >= max_parsed_lines:
 
102
            break
 
103
 
 
104
        # Always skip the last line as it may be truncated since we're
 
105
        # rsyncing live logs, unless there is only one line for us to
 
106
        # parse, in which case This probably means we're dealing with a
 
107
        # logfile that has been rotated already, so it should be safe to
 
108
        # parse its last line.
 
109
        next_line = ''
 
110
        try:
 
111
            next_line = fd.next()
 
112
        except StopIteration:
 
113
            if parsed_lines > 0:
 
114
                break
 
115
 
 
116
        try:
 
117
            parsed_lines += 1
101
118
            parsed_bytes += len(line)
102
119
            host, date, status, request = get_host_date_status_and_request(
103
120
                line)
143
160
            parsed_bytes -= len(line)
144
161
            logger.error('Error (%s) while parsing "%s"' % (e, line))
145
162
            break
 
163
 
 
164
        line = next_line
146
165
    return downloads, parsed_bytes
147
166
 
148
167