diff --git a/csp_billing_adapter/adapter.py b/csp_billing_adapter/adapter.py index 042ed4b..b4c7acd 100644 --- a/csp_billing_adapter/adapter.py +++ b/csp_billing_adapter/adapter.py @@ -252,6 +252,21 @@ def event_loop_handler( add_usage_record(usage, cache, config.billing_interval) csp_config['base_product'] = usage.get('base_product', '') + try: + archive = retry_on_exception( + functools.partial( + hook.get_metering_archive, + config=config, + ), + logger=log, + func_name='hook.get_metering_archive' + ) + except Exception: + archive = None + + if archive is None: + archive = [] + log.debug( "Now: %s, Next Reporting Time: %s, Next Bill Time: %s", date_to_string(now), @@ -262,7 +277,15 @@ def event_loop_handler( trial_remaining = cache.get('trial_remaining', 0) if now >= string_to_date(cache['next_bill_time']): - if trial_remaining > 0: + if trial_remaining > 0 and len(archive) > 0: + msg = ( + 'Free trial is active but archive contains metering history. ' + 'Usage will be billed for the previous cycle.' + ) + log.warning(msg) + csp_config['errors'].append(msg) + + if trial_remaining > 0 and len(archive) == 0: log.info('Attempt a free trial billing cycle update') process_metering( hook,