~launchpad-pqm/launchpad/db-devel

7675.3447.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
7675.1287.7 by Karl Fogel
Add the copyright header block to more files.
2
#
7675.6558.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
3
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
7675.1287.7 by Karl Fogel
Add the copyright header block to more files.
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
2816.1.1 by Stuart Bishop
Stub garbage collector cron script
6
"""Librarian garbage collector.
7
8
This script is run on the Librarian server to merge duplicate files,
9
remove expired files from the file system and clean up unreachable
10
rows in the database.
11
"""
12
13
__metaclass__ = type
14
7675.7103.5 by William Grant
Reformat
15
import _pythonpath
16
7675.6558.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
17
import logging
18
7675.7100.1 by Curtis Hovey
Moved canonical.config to lp.services.
19
from lp.services.config import config
7675.9111.1 by Steve Kowalik
Destroy all the callsites I could of IStoreSelector.get(), and kill lp.services.database.lpstorm, moving all of the interfaces to interfaces.
20
from lp.services.database.interfaces import IStore
7675.7078.1 by William Grant
Move librarian stuff from canonical.launchpad to lp.services.librarian. canonical.librarian remains untouched.
21
from lp.services.librarian.model import LibraryFileAlias
7675.7102.5 by William Grant
Move the rest of canonical.librarian to lp.services.librarianserver.
22
from lp.services.librarianserver import librariangc
7675.768.1 by Leonard Richardson
Partial move.
23
from lp.services.scripts.base import LaunchpadCronScript
4264.2.1 by James Henstridge
add a LaunchpadCronScript subclass, and make cronscripts/*.py use it
24
25
26
class LibrarianGC(LaunchpadCronScript):
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
27
    def add_my_options(self):
28
        self.parser.add_option(
29
                '', "--skip-duplicates", action="store_true", default=False,
30
                dest="skip_duplicates",
31
                help="Skip duplicate LibraryFileContent merging"
32
                )
33
        self.parser.add_option(
34
                '', "--skip-aliases", action="store_true", default=False,
35
                dest="skip_aliases",
36
                help="Skip unreferenced LibraryFileAlias removal"
37
                )
38
        self.parser.add_option(
39
                '', "--skip-content", action="store_true", default=False,
40
                dest="skip_content",
41
                help="Skip unreferenced LibraryFileContent removal"
42
                )
43
        self.parser.add_option(
44
                '', "--skip-blobs", action="store_true", default=False,
45
                dest="skip_blobs",
46
                help="Skip removing expired TemporaryBlobStorage rows"
47
                )
48
        self.parser.add_option(
49
                '', "--skip-files", action="store_true", default=False,
50
                dest="skip_files",
51
                help="Skip removing files on disk with no database references"
7500.2.1 by Stuart Bishop
When all aliases are expired, flag a LibraryFileContent as deleted
52
                     " or flagged for deletion."
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
53
                )
7675.1953.10 by Stuart Bishop
Expire expired LibraryFileAliases
54
        self.parser.add_option(
55
                '', "--skip-expiry", action="store_true", default=False,
56
                dest="skip_expiry",
57
                help="Skip expiring aliases with an expiry date in the past."
58
                )
59
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
60
    def main(self):
61
        librariangc.log = self.logger
62
63
        if self.options.loglevel <= logging.DEBUG:
64
            librariangc.debug = True
65
7675.6496.4 by William Grant
librarian-gc no longer uses ZTM.conn().
66
        # XXX wgrant 2011-09-18 bug=853066: Using Storm's raw connection
67
        # here is wrong. We should either create our own or use
68
        # Store.execute or cursor() and the transaction module.
69
        conn = IStore(LibraryFileAlias)._connection._raw_connection
3691.357.6 by Stuart Bishop
Updates based on review feedback
70
71
        # Refuse to run if we have significant clock skew between the
72
        # librarian and the database.
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
73
        librariangc.confirm_no_clock_skew(conn)
3691.357.6 by Stuart Bishop
Updates based on review feedback
74
3322.1.11 by Stuart Bishop
Refactor librarian garbage collection to only use one connection
75
        # Note that each of these next steps will issue commit commands
76
        # as appropriate to make this script transaction friendly
7675.1953.10 by Stuart Bishop
Expire expired LibraryFileAliases
77
        if not self.options.skip_expiry:
78
            librariangc.expire_aliases(conn)
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
79
        if not self.options.skip_content:
7675.6558.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
80
            # First sweep.
81
            librariangc.delete_unreferenced_content(conn)
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
82
        if not self.options.skip_blobs:
83
            librariangc.delete_expired_blobs(conn)
84
        if not self.options.skip_duplicates:
85
            librariangc.merge_duplicates(conn)
86
        if not self.options.skip_aliases:
87
            librariangc.delete_unreferenced_aliases(conn)
88
        if not self.options.skip_content:
7675.6558.1 by Jeroen Vermeulen
Fix lots of lint in recently-changed files.
89
            # Second sweep.
90
            librariangc.delete_unreferenced_content(conn)
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
91
        if not self.options.skip_files:
92
            librariangc.delete_unwanted_files(conn)
2816.1.7 by Stuart Bishop
Work in progress
93
2816.1.1 by Stuart Bishop
Stub garbage collector cron script
94
95
if __name__ == '__main__':
7675.9281.28 by Stuart Bishop
Remove unnecessary imports
96
    script = LibrarianGC('librarian-gc', dbuser=config.librarian_gc.dbuser)
7675.6540.2 by William Grant
LaunchpadScript no longer uses initZopeless.
97
    script.lock_and_run(isolation='autocommit')