34
34
app .wsgi_app = ProxyFix (app .wsgi_app , x_proto = 1 )
35
35
# Rate limiter to prevent spam/overloading stuff
36
36
limiter = flask_limiter .Limiter (
37
- app ,
37
+ app = app ,
38
38
key_func = flask_limiter .util .get_remote_address ,
39
39
default_limits = ["10/second" ],
40
40
headers_enabled = True , # Send headers with info about how much time has left until unlocking
@@ -75,11 +75,21 @@ def _roll_rate_limit():
75
75
('120/hour' if c < 8 else ('60/hour' if c < 30 else '30/hour' ))
76
76
77
77
78
+ def _result_ttl ():
79
+ """A helper function that if-else-es what result TTL to set right now
80
+ It's made so that if there aren't a lot of results in db, it will be very very long (maybe even few hours),
81
+ but if there are, it will start to get much more strict
82
+ """
83
+ c = queue_vision .finished_job_registry .count
84
+ return '72h' if c < 150 else ('6h' if c < 250 else ('30m' if c < 500 else '5m' ))
85
+
86
+
78
87
@app .route (API1 + 'roll/' )
79
88
@limiter .limit (_roll_rate_limit ())
80
89
def roll ():
81
- image_job = queue_images .enqueue (roll_and_take_image , job_timeout = '15s' , result_ttl = '60s' , ttl = '5h' )
82
- vision_job = queue_vision .enqueue (process_image , depends_on = image_job , job_timeout = '2m' , result_ttl = '5m' , ttl = '5h' )
90
+ image_job = queue_images .enqueue (roll_and_take_image , job_timeout = '15s' , result_ttl = '90s' , ttl = '5h' )
91
+ vision_job = queue_vision .enqueue (
92
+ process_image , depends_on = image_job , job_timeout = '1m' , result_ttl = _result_ttl (), ttl = '5h' )
83
93
return vision_job .id
84
94
85
95
@@ -121,7 +131,7 @@ def info(job_id):
121
131
status = _handle_status (job , lambda : "FINISHED" )[0 ]
122
132
# How much time has left for results to be available
123
133
if status == "FINISHED" :
124
- ttl = job .result ['finished_time' ] + job .result_ttl
134
+ ttl = job .return_value () ['finished_time' ] + job .result_ttl
125
135
elif status in ["EXPIRED" , "FAILED" ]:
126
136
ttl = 0.0
127
137
else :
@@ -133,7 +143,7 @@ def info(job_id):
133
143
# 4.56 is average time from my calculations
134
144
'eta' : (datetime .datetime .now () + datetime .timedelta (seconds = left * 4.56 )).timestamp (),
135
145
'ttl' : ttl ,
136
- 'result' : None if status != "FINISHED" else job .result ['number' ]
146
+ 'result' : None if status != "FINISHED" else job .return_value () ['number' ]
137
147
}
138
148
139
149
@@ -142,7 +152,7 @@ def result(job_id):
142
152
job = queue_vision .fetch_job (str (job_id ))
143
153
return _handle_status (
144
154
job ,
145
- lambda : str (job .result ['number' ])
155
+ lambda : str (job .return_value () ['number' ])
146
156
)
147
157
148
158
@@ -153,9 +163,12 @@ def image(job_id):
153
163
return _handle_status (
154
164
job ,
155
165
lambda : send_file (
156
- io .BytesIO (job .result ['original_image' ]),
166
+ io .BytesIO (job .return_value () ['original_image' ]),
157
167
mimetype = 'image/jpeg' ,
158
- attachment_filename = f'{ id } .jpg'
168
+ download_name = f'{ id } .jpg' ,
169
+ etag = job .id ,
170
+ last_modified = job .ended_at ,
171
+ max_age = 31536000 ,
159
172
)
160
173
)
161
174
@@ -167,9 +180,12 @@ def anal_image(job_id):
167
180
return _handle_status (
168
181
job ,
169
182
lambda : send_file (
170
- io .BytesIO (job .result ['kp_image' ]),
183
+ io .BytesIO (job .return_value () ['kp_image' ]),
171
184
mimetype = 'image/jpeg' ,
172
- attachment_filename = f'{ id } .jpg'
185
+ download_name = f'{ id } .jpg' ,
186
+ etag = job .id ,
187
+ last_modified = job .ended_at ,
188
+ max_age = 31536000 ,
173
189
)
174
190
)
175
191
0 commit comments