~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=E0611,W0212

"""Database classes including and related to CodeImportMachine."""

__metaclass__ = type

__all__ = [
    'CodeImportMachine',
    'CodeImportMachineSet',
    ]

from sqlobject import (
    SQLMultipleJoin,
    StringCol,
    )
from zope.component import getUtility
from zope.interface import implements

from canonical.config import config
from canonical.database.constants import (
    DEFAULT,
    UTC_NOW,
    )
from canonical.database.datetimecol import UtcDateTimeCol
from canonical.database.enumcol import EnumCol
from canonical.database.sqlbase import SQLBase
from lp.code.enums import (
    CodeImportMachineOfflineReason,
    CodeImportMachineState,
    )
from lp.code.interfaces.codeimportevent import ICodeImportEventSet
from lp.code.interfaces.codeimportmachine import (
    ICodeImportMachine,
    ICodeImportMachineSet,
    )


class CodeImportMachine(SQLBase):
    """See `ICodeImportMachine`."""

    _defaultOrder = ['hostname']

    implements(ICodeImportMachine)

    date_created = UtcDateTimeCol(notNull=True, default=DEFAULT)

    hostname = StringCol(default=None)
    state = EnumCol(enum=CodeImportMachineState, notNull=True,
        default=CodeImportMachineState.OFFLINE)
    heartbeat = UtcDateTimeCol(notNull=False)

    current_jobs = SQLMultipleJoin(
        'CodeImportJob', joinColumn='machine',
        orderBy=['date_started', 'id'])

    events = SQLMultipleJoin(
        'CodeImportEvent', joinColumn='machine',
        orderBy=['-date_created', '-id'])

    def shouldLookForJob(self, worker_limit):
        """See `ICodeImportMachine`."""
        job_count = self.current_jobs.count()

        if self.state == CodeImportMachineState.OFFLINE:
            return False
        self.heartbeat = UTC_NOW
        if self.state == CodeImportMachineState.QUIESCING:
            if job_count == 0:
                self.setOffline(
                    CodeImportMachineOfflineReason.QUIESCED)
            return False
        elif self.state == CodeImportMachineState.ONLINE:
            return job_count < worker_limit
        else:
            raise AssertionError(
                "Unknown machine state %r??" % self.state)

    def setOnline(self, user=None, message=None):
        """See `ICodeImportMachine`."""
        if self.state not in (CodeImportMachineState.OFFLINE,
                              CodeImportMachineState.QUIESCING):
            raise AssertionError(
                "State of machine %s was %s."
                % (self.hostname, self.state.name))
        self.state = CodeImportMachineState.ONLINE
        getUtility(ICodeImportEventSet).newOnline(self, user, message)

    def setOffline(self, reason, user=None, message=None):
        """See `ICodeImportMachine`."""
        if self.state not in (CodeImportMachineState.ONLINE,
                              CodeImportMachineState.QUIESCING):
            raise AssertionError(
                "State of machine %s was %s."
                % (self.hostname, self.state.name))
        self.state = CodeImportMachineState.OFFLINE
        getUtility(ICodeImportEventSet).newOffline(
            self, reason, user, message)

    def setQuiescing(self, user, message=None):
        """See `ICodeImportMachine`."""
        if self.state != CodeImportMachineState.ONLINE:
            raise AssertionError(
                "State of machine %s was %s."
                % (self.hostname, self.state.name))
        self.state = CodeImportMachineState.QUIESCING
        getUtility(ICodeImportEventSet).newQuiesce(self, user, message)


class CodeImportMachineSet(object):
    """See `ICodeImportMachineSet`."""

    implements(ICodeImportMachineSet)

    def getAll(self):
        """See `ICodeImportMachineSet`."""
        return CodeImportMachine.select()

    def getByHostname(self, hostname):
        """See `ICodeImportMachineSet`."""
        return CodeImportMachine.selectOneBy(hostname=hostname)

    def new(self, hostname, state=CodeImportMachineState.OFFLINE):
        """See `ICodeImportMachineSet`."""
        machine = CodeImportMachine(hostname=hostname, heartbeat=None)
        if state == CodeImportMachineState.ONLINE:
            machine.setOnline()
        elif state != CodeImportMachineState.OFFLINE:
            raise AssertionError(
                "Invalid machine creation state: %r." % state)
        return machine