Skip to content

Commit

Permalink
bring back checking CPU credit balance
Browse files Browse the repository at this point in the history
originally added in 353af35, accidentally removed in e6a09e4
  • Loading branch information
snarfed committed Jun 18, 2024
1 parent 99978b0 commit 11d8f77
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from b2sdk.account_info.in_memory import InMemoryAccountInfo
from b2sdk.api import B2Api
from b2sdk.progress import AbstractProgressListener
import boto.ec2.cloudwatch
import requests
import webob
import webob.exc
Expand Down Expand Up @@ -85,6 +86,23 @@ def application(environ, start_response):
'Sorry, this content is not currently supported due to copyright.')(
environ, start_response)

# check that our CPU credit balance isn't too low
try:
cloudwatch = boto.ec2.cloudwatch.connect_to_region('us-east-2')
for metric in cloudwatch.list_metrics(metric_name='CPUCreditBalance'):
if metric.name == 'CPUCreditBalance':
stats = metric.query(datetime.datetime.now() - datetime.timedelta(minutes=10),
datetime.datetime.now(), ['Average'])
if stats:
credit = stats[-1].get('Average')
if credit and credit <= 30:
msg = "Sorry, we're too busy right now. Please try again later!"
exc = webob.exc.HTTPServiceUnavailable(msg)
exc.html_template_obj = Template(HTML_HEADER + msg + HTML_FOOTER)
return exc(environ, start_response)
except:
logging.exception("Couldn't fetch CPU credit balance from CloudWatch!")

write_fn = start_response('200 OK', headers)
def write(line):
write_fn(line.encode())
Expand Down

0 comments on commit 11d8f77

Please sign in to comment.