1
1
#!/usr/bin/env python3
2
2
3
- """Interraction with the swatbot Django server."""
3
+ """Interaction with the swatbot Django server."""
4
4
5
5
import enum
6
6
import json
10
10
import requests
11
11
12
12
from . import utils
13
- from .webrequests import RefreshPolicy , Session
13
+ from .webrequests import Session
14
14
15
15
logger = logging .getLogger (__name__ )
16
16
19
19
REST_BASE_URL = f"{ BASE_URL } /rest"
20
20
21
21
22
+ class RefreshPolicy (enum .Enum ):
23
+ """A swatbot cache refresh policy."""
24
+
25
+ NO = enum .auto ()
26
+ FORCE = enum .auto ()
27
+ FORCE_FAILURES = enum .auto ()
28
+ AUTO = enum .auto ()
29
+
30
+
31
+ class RefreshManager :
32
+ """A refresh manager for the swatbot REST API."""
33
+
34
+ _instance = None
35
+
36
+ FAILURES_AUTO_REFRESH_S = 60 * 60 * 4
37
+ AUTO_REFRESH_S = 60 * 60 * 24 * 30
38
+
39
+ # pylint: disable=duplicate-code
40
+ # pylint complains because of duplicate code in singleton init. We might do
41
+ # better, but keep it that way for now.
42
+ def __new__ (cls , * args , ** kwargs ):
43
+ if not isinstance (cls ._instance , cls ):
44
+ cls ._instance = super ().__new__ (cls , * args , ** kwargs )
45
+ cls ._instance .initialized = False
46
+ return cls ._instance
47
+
48
+ def __init__ (self ):
49
+ if self ._instance .initialized :
50
+ return
51
+
52
+ self .refresh_policy = RefreshPolicy .AUTO
53
+
54
+ self ._instance .initialized = True
55
+
56
+ def set_policy (self , policy : RefreshPolicy ):
57
+ """Set the global refresh policy."""
58
+ self .refresh_policy = policy
59
+
60
+ def set_policy_by_name (self , policy_name : str ):
61
+ """Set the global refresh policy from policy name."""
62
+ self .set_policy (RefreshPolicy [policy_name .upper ()])
63
+
64
+ def get_refresh_max_age (self ,
65
+ refresh_override : Optional [RefreshPolicy ] = None ,
66
+ failures : bool = False
67
+ ) -> int :
68
+ """Get the maximum age before refresh for a given policy."""
69
+ policy = refresh_override if refresh_override else self .refresh_policy
70
+ if policy == RefreshPolicy .FORCE_FAILURES :
71
+ policy = RefreshPolicy .FORCE if failures else RefreshPolicy .AUTO
72
+
73
+ if policy == RefreshPolicy .FORCE :
74
+ return 0
75
+ if policy == RefreshPolicy .NO :
76
+ return - 1
77
+
78
+ if failures :
79
+ return self .FAILURES_AUTO_REFRESH_S
80
+ return self .AUTO_REFRESH_S
81
+
82
+
22
83
class TriageStatus (enum .IntEnum ):
23
84
"""A status to set on a failure."""
24
85
@@ -35,10 +96,6 @@ def from_str(status: str) -> 'TriageStatus':
35
96
CANCELLED = 5
36
97
37
98
38
- FAILURES_AUTO_REFRESH_S = 60 * 60 * 4
39
- AUTO_REFRESH_S = 60 * 60 * 24 * 30
40
-
41
-
42
99
def _get_csrftoken () -> str :
43
100
return Session ().session .cookies ['csrftoken' ]
44
101
@@ -83,15 +140,14 @@ def _get_json(path: str, max_cache_age: int = -1):
83
140
84
141
def get_build (buildid : int , refresh_override : Optional [RefreshPolicy ] = None ):
85
142
"""Get info on a given build."""
86
- maxage = Session ().refresh_policy_max_age ( AUTO_REFRESH_S , refresh_override )
143
+ maxage = RefreshManager ().get_refresh_max_age ( refresh_override )
87
144
return _get_json (f"/build/{ buildid } /" , maxage )['data' ]
88
145
89
146
90
147
def get_build_collection (collectionid : int , refresh_override :
91
148
Optional [RefreshPolicy ] = None ):
92
149
"""Get info on a given build collection."""
93
- maxage = Session ().refresh_policy_max_age (AUTO_REFRESH_S ,
94
- refresh_override )
150
+ maxage = RefreshManager ().get_refresh_max_age (refresh_override )
95
151
return _get_json (f"/buildcollection/{ collectionid } /" , maxage )['data' ]
96
152
97
153
@@ -106,16 +162,15 @@ def invalidate_stepfailures_cache():
106
162
107
163
def get_stepfailures (refresh_override : Optional [RefreshPolicy ] = None ):
108
164
"""Get info on all failures."""
109
- maxage = Session ().refresh_policy_max_age ( FAILURES_AUTO_REFRESH_S ,
110
- refresh_override )
165
+ maxage = RefreshManager ().get_refresh_max_age ( refresh_override ,
166
+ failures = True )
111
167
return _get_json ("/stepfailure/" , maxage )['data' ]
112
168
113
169
114
170
def get_stepfailure (failureid : int ,
115
171
refresh_override : Optional [RefreshPolicy ] = None ):
116
172
"""Get info on a given failure."""
117
- maxage = Session ().refresh_policy_max_age (FAILURES_AUTO_REFRESH_S ,
118
- refresh_override )
173
+ maxage = RefreshManager ().get_refresh_max_age (refresh_override )
119
174
return _get_json (f"/stepfailure/{ failureid } /" , maxage )['data' ]
120
175
121
176
0 commit comments