7
7
8
8
print ("Initializing " , file = sys .stderr )
9
9
import flask
10
- from util .gcp_utils import increment_invocation_count , invocation_count , memray_filename
10
+ from util .gcp_utils import (
11
+ increment_invocation_count ,
12
+ count_invocations_by_path ,
13
+ memray_filename ,
14
+ )
11
15
from util .detect_gae import detect_gae
12
16
13
17
app = flask .Flask (__name__ )
66
70
logging .info (
67
71
"env GAE_USE_SOCKETS_HTTPLIB is %s" , os .environ .get ("GAE_USE_SOCKETS_HTTPLIB" )
68
72
)
73
+ logging .info (
74
+ "env PYTHONMALLOC is %s" , os .environ .get ("PYTHONMALLOC" )
75
+ )
69
76
70
77
PluginHolder .init ()
71
78
74
81
def list_memray ():
75
82
s = ""
76
83
for f in os .listdir (tmp_dir ()):
77
- s += f"<a href='/tmpfile?filename={ f } '></a><br>"
78
- return s , 200
84
+ s += f"<a href='/memrayfiles/{ f } '>{ f } </a><br>\n "
85
+ if not s :
86
+ s = "NONE FOUND"
87
+ return Response (s , mimetype = "text/html" , status = 200 )
79
88
80
89
81
- @app .route ("/tmpfile " )
82
- def tmpfile ( ):
83
- fn = flask . request . args [ 'filename' ]
84
- with open (f"{ tmp_dir ()} /{ fn } " , 'b' ) as f :
90
+ @app .route ("/memrayfiles/<filename> " )
91
+ def memrayfiles ( filename ):
92
+
93
+ with open (f"{ tmp_dir ()} /{ filename } " , "rb" ) as f :
85
94
content = f .read ()
86
- return Response (content , mimetype = 'application/octet-stream' , status = 200 )
95
+
96
+ return Response (content , mimetype = "application/octet-stream" , status = 200 )
87
97
88
98
89
99
@app .route ("/" )
90
100
def index ():
91
- with memray .Tracker (memray_filename ()):
101
+ with memray .Tracker (memray_filename ("index" )):
92
102
increment_invocation_count ("index" )
93
103
with gae_memory_logging ("index" ):
94
104
msg = iris_homepage_text ()
95
105
if config_utils .is_test_or_dev_configuration ():
96
106
msg += "\n I'm running in test or dev mode."
97
107
98
- logging .info ("index(); invocations of GAE instance : %s" , invocation_count ())
108
+ logging .info (
109
+ "index(); invocations of GAE instance : %s" , count_invocations_by_path ()
110
+ )
99
111
return Response (msg , mimetype = "text/plain" , status = 200 )
100
112
101
113
102
114
@app .route ("/_ah/warmup" )
103
115
def warmup ():
104
- with memray .Tracker (memray_filename ()):
105
-
116
+ with memray .Tracker (memray_filename ("warmup" )):
106
117
increment_invocation_count ("warmup" )
107
118
with gae_memory_logging ("warmup" ):
108
119
logging .info ("warmup() called" )
109
- try :
110
- 1 / 0
111
- except Exception :
112
- logging .exception ("eeeeee" )
120
+
113
121
return "" , 200 , {}
114
122
115
123
@@ -119,7 +127,7 @@ def schedule():
119
127
"""
120
128
Send out a message per-plugin per-project to label all objects of that type and project.
121
129
"""
122
- with memray .Tracker (memray_filename ):
130
+ with memray .Tracker (memray_filename ( "schedule" ) ):
123
131
124
132
increment_invocation_count ("schedule" )
125
133
with gae_memory_logging ("schedule" ):
@@ -162,9 +170,9 @@ def __get_enabled_projects():
162
170
raise Exception ("No projects enabled at all" )
163
171
164
172
if (
165
- not detect_gae ()
166
- or is_test_or_dev_configuration ()
167
- or is_in_test_or_dev_project (current_project_id ())
173
+ not detect_gae ()
174
+ or is_test_or_dev_configuration ()
175
+ or is_in_test_or_dev_project (current_project_id ())
168
176
):
169
177
max_proj_in_dev = 3
170
178
if len (enabled_projs ) > max_proj_in_dev :
@@ -173,8 +181,8 @@ def __get_enabled_projects():
173
181
+ f"to avoid accidentally flooding the system."
174
182
+ f"{ max_proj_in_dev } projects are available, which exceeds that."
175
183
+ f"To avoid this limit, use config.yaml rather than config-dev.yaml or config-test.yaml,"
176
- f"edit test_or_dev_project_markers in the config file,"
177
- f"and run in the cloud rather than locally."
184
+ f"edit test_or_dev_project_markers in the config file,"
185
+ f"and run in the cloud rather than locally."
178
186
)
179
187
return enabled_projs
180
188
@@ -184,9 +192,9 @@ def __send_pubsub_per_projectplugin(configured_projects):
184
192
for project_id in configured_projects :
185
193
for plugin_cls in PluginHolder .plugins :
186
194
if (
187
- not plugin_cls .is_labeled_on_creation ()
188
- or plugin_cls .relabel_on_cron ()
189
- or config_utils .label_all_on_cron ()
195
+ not plugin_cls .is_labeled_on_creation ()
196
+ or plugin_cls .relabel_on_cron ()
197
+ or config_utils .label_all_on_cron ()
190
198
):
191
199
pubsub_utils .publish (
192
200
msg = json .dumps (
@@ -210,7 +218,7 @@ def __send_pubsub_per_projectplugin(configured_projects):
210
218
211
219
@app .route ("/label_one" , methods = ["POST" ])
212
220
def label_one ():
213
- with memray .Tracker (memray_filename ()):
221
+ with memray .Tracker (memray_filename ("label_one" )):
214
222
215
223
increment_invocation_count ("label_one" )
216
224
with gae_memory_logging ("label_one" ):
@@ -261,7 +269,9 @@ def label_one():
261
269
# since most errors are unrecoverable.
262
270
return "OK" , 200
263
271
except Exception :
264
- project_id = data .get ("resource" , {}).get ("labels" , {}).get ("project_id" )
272
+ project_id = (
273
+ data .get ("resource" , {}).get ("labels" , {}).get ("project_id" )
274
+ )
265
275
logging .exception ("Error on label_one %s %s" , plugins_found , project_id )
266
276
return "Error" , 500
267
277
@@ -320,14 +330,16 @@ def __extract_pubsub_content() -> Dict:
320
330
321
331
@app .route ("/do_label" , methods = ["POST" ])
322
332
def do_label ():
323
- with memray .Tracker (memray_filename ()):
333
+ with memray .Tracker (memray_filename ("do_label" )):
324
334
increment_invocation_count ("do_label" )
325
335
with gae_memory_logging ("do_label" ):
326
336
327
337
"""Receive a push message from PubSub, sent from schedule() above,
328
338
with instructions to label all objects of a given plugin and project_id.
329
339
"""
330
- project_id = "" # set up variables to allow logging in Exception block at end
340
+ project_id = (
341
+ "" # set up variables to allow logging in Exception block at end
342
+ )
331
343
plugin_class_name = ""
332
344
try :
333
345
data = __extract_pubsub_content ()
@@ -344,7 +356,9 @@ def do_label():
344
356
project_id = data ["project_id" ]
345
357
with timing (f"do_label { plugin_class_name } { project_id } " ):
346
358
logging .info (
347
- "do_label() for %s in %s" , plugin .__class__ .__name__ , project_id
359
+ "do_label() for %s in %s" ,
360
+ plugin .__class__ .__name__ ,
361
+ project_id ,
348
362
)
349
363
plugin .label_all (project_id )
350
364
logging .info ("OK on do_label %s %s" , plugin_class_name , project_id )
@@ -354,7 +368,9 @@ def do_label():
354
368
355
369
return "OK" , 200
356
370
except Exception :
357
- logging .exception ("Error on do_label %s %s" , plugin_class_name , project_id )
371
+ logging .exception (
372
+ "Error on do_label %s %s" , plugin_class_name , project_id
373
+ )
358
374
return "Error" , 500
359
375
360
376
0 commit comments