Skip to content

Commit 05d9739

Browse files
committed
Lazily load nerc-rates to avoid Python 3.9 incompat
1 parent 02e036c commit 05d9739

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/coldfront_plugin_cloud/management/commands/calculate_storage_gb_hours.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,25 @@
1414
from coldfront.core.allocation.models import Allocation, AllocationStatusChoice
1515
import pytz
1616

17-
from nerc_rates import load_from_url
1817

1918
logging.basicConfig(level=logging.INFO)
2019
logger = logging.getLogger(__name__)
2120

21+
_RATES = None
22+
23+
24+
def get_rates():
25+
# nerc-rates doesn't work with Python 3.9, which is what ColdFront is currently
26+
# using in Production. Lazily load the rates only when either of the storage rates
27+
# is not set via CLI arguments, so we can keep providing them via CLI until we upgrade
28+
# Python version.
29+
global _RATES
30+
31+
if _RATES is None:
32+
from nerc_rates import load_from_url
33+
_RATES = load_from_url()
34+
return _RATES
35+
2236

2337
@dataclasses.dataclass
2438
class InvoiceRow:
@@ -200,20 +214,18 @@ def process_invoice_row(allocation, attrs, su_name, rate):
200214
resources__in=openshift_resources
201215
)
202216

203-
rates = load_from_url()
204-
205217
if options['openstack_gb_rate']:
206218
openstack_storage_rate = options['openstack_gb_rate']
207219
else:
208220
openstack_storage_rate = Decimal(
209-
rates.get_value_at('Storage GB Rate', options["invoice_month"])
221+
get_rates().get_value_at('Storage GB Rate', options["invoice_month"])
210222
)
211223

212224
if options['openshift_gb_rate']:
213225
openshift_storage_rate = options['openshift_gb_rate']
214226
else:
215227
openshift_storage_rate = Decimal(
216-
rates.get_value_at('Storage GB Rate', options["invoice_month"])
228+
get_rates().get_value_at('Storage GB Rate', options["invoice_month"])
217229
)
218230

219231
logger.info(f'Using storage rate {openstack_storage_rate} (Openstack) and '

0 commit comments

Comments
 (0)