Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

134

135

136

137

138

139

140

141

# Copyright (C) 2018 Bloomberg LP 

# 

# Licensed under the Apache License, Version 2.0 (the "License"); 

# you may not use this file except in compliance with the License. 

# You may obtain a copy of the License at 

# 

# <http://www.apache.org/licenses/LICENSE-2.0> 

# 

# Unless required by applicable law or agreed to in writing, software 

# distributed under the License is distributed on an "AS IS" BASIS, 

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

# See the License for the specific language governing permissions and 

# limitations under the License. 

# 

# Disable broad exception catch 

# pylint: disable=broad-except 

 

 

import asyncio 

import logging 

import threading 

 

from functools import partial 

 

import grpc 

 

from buildgrid._enums import LeaseState 

from buildgrid._exceptions import BotError 

 

 

class Tenant: 

 

def __init__(self, lease): 

"""Initialises a new :class:`Tenant`. 

 

Args: 

lease (:class:`Lease`) : A new lease. 

 

Raises: 

ValueError: If `lease` is not in a `PENDING` state. 

""" 

 

if lease.state != LeaseState.PENDING.value: 

raise ValueError("Lease state not `PENDING`") 

 

self._lease = lease 

self.__logger = logging.getLogger(__name__) 

self.__lease_cancelled = False 

self.__tenant_completed = False 

 

@property 

def lease(self): 

"""Returns the lease""" 

return self._lease 

 

@property 

def tenant_completed(self): 

"""Returns `True` if the work has completed or sucessfully stopped its work.""" 

return self.__tenant_completed 

 

@property 

def lease_cancelled(self): 

"""Returns `True` if the lease has been cancelled.""" 

return self.__lease_cancelled 

 

def cancel_lease(self): 

"""Cancel the lease.""" 

self.__lease_cancelled = True 

self.update_lease_state(LeaseState.CANCELLED) 

 

def get_lease_state(self): 

"""Returns the :class:`LeaseState`.""" 

return LeaseState(self._lease.state) 

 

def update_lease_result(self, result): 

"""Update the lease result. 

 

Args: 

result (:class:`Any`) : The result of the lease.""" 

self._lease.result.CopyFrom(result) 

 

def update_lease_state(self, state): 

"""Update the lease state. 

 

Args: 

state (:class:`LeaseState`) : State of the lease. 

""" 

self._lease.state = state.value 

 

def update_lease_status(self, status): 

"""Update the lease status. 

 

Args: 

status (:class:`Status`) : Status of the lease. 

""" 

self._lease.status.CopyFrom(status) 

 

async def run_work(self, work, context=None, executor=None): 

"""Runs work. 

 

Work is run in an executor. 

 

Args: 

work (func) : Work to do. 

context (context) : Context for work. 

executor (:class:`ThreadPoolExecutor`) : Thread pool for work to run on. 

""" 

self.__logger.debug("Work created. Lease_id=[%s]", self._lease.id) 

 

loop = asyncio.get_event_loop() 

 

try: 

event = threading.Event() 

lease = await loop.run_in_executor(executor, partial(work, self._lease, context, event)) 

 

except asyncio.CancelledError: 

self.__logger.error("Lease cancelled: lease_id=[%s]", self._lease.id) 

event.set() 

self.__tenant_completed = True 

# Propagate error to task wrapper 

raise 

 

except grpc.RpcError as e: 

self.__logger.error(e) 

self.__tenant_completed = True 

raise 

 

except BotError as e: 

self.__logger.error(e) 

self.__tenant_completed = True 

raise 

 

except Exception as e: 

self.__logger.error(e) 

self.__tenant_completed = True 

raise 

 

self.__tenant_completed = True 

self.__logger.debug("Work completed: lease_id=[%s]", lease.id) 

 

return lease