Skip to content

Commit

Permalink
Eliminate potential infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
grablair committed Jan 3, 2024
1 parent 946628a commit 42931b4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 4 additions & 4 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ def remove_recurring_transaction(self, id):
session.commit()
logger.info("Removed recurring transaction")

def get_past_due_recurring_transactions(self):
def is_next_recurrence_before_now(txn):
def get_past_due_recurring_transactions(self, exclude_ids=set()):
def txn_filter(txn):
rules = rrule.rrulestr(rrule_for_txn(txn))
return rules.after(txn.previous_occurrence) < datetime.now()
return rules.after(txn.previous_occurrence) < datetime.now() and txn.id not in exclude_ids

self.clean_up_expired_recurring_transactions()
return list(filter(is_next_recurrence_before_now, self.get_all_recurring_transactions()))
return list(filter(txn_filter, self.get_all_recurring_transactions()))

def clean_up_expired_recurring_transactions(self):
stmt = select(RecurringTransaction)
Expand Down
6 changes: 5 additions & 1 deletion monarch_money_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def process_recurring_transactions(self):

txns = self.db.get_past_due_recurring_transactions()
logger.info("%s recurring transactions to process in first iteration" % len(txns))
skipped_ids = set()
while txns:
for txn in txns:
logger.info("Creating transaction for \"%s\"" % txn)
Expand All @@ -157,5 +158,8 @@ def process_recurring_transactions(self):
if self.add_transaction(txn.description, txn.amount, txn.category, next_occurrence, "RECUR:%s:%s" % (txn.dedupe_string, next_occurrence.isoformat()), notes=txn.notes):
# only run the completion logic if the transaction now exists
self.db.process_recurring_transaction_completion(txn.id)
txns = self.db.get_past_due_recurring_transactions()
else:
skipped_ids.add(txn.id)

txns = self.db.get_past_due_recurring_transactions(exclude_ids=skipped_ids)
logger.info("%s recurring transactions to process in next iteration" % len(txns))

0 comments on commit 42931b4

Please sign in to comment.