-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable resiliency by providing FileSystemOrderManager and adding a ne…
…w volume
- Loading branch information
mhewedy
committed
May 23, 2024
1 parent
ace34db
commit 4462154
Showing
3 changed files
with
83 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import ast | ||
import json | ||
import logging | ||
from abc import ABC | ||
from pathlib import Path | ||
|
||
|
||
class OrderManager(ABC): | ||
def __init__(self): | ||
self.orders = {} | ||
|
||
def add_order(self, message_id, user, order): | ||
self.orders[(message_id, user)] = order | ||
|
||
def delete_order(self, user): | ||
deleted_orders = [(key, self.orders[key]) for key in list(self.orders) if key[1] == user] | ||
for key, _ in deleted_orders: | ||
del self.orders[key] | ||
return deleted_orders | ||
|
||
def list_orders(self): | ||
return self.orders | ||
|
||
def clear_orders(self): | ||
self.orders = {} | ||
|
||
|
||
class InMemoryOrderManager(OrderManager): | ||
pass | ||
|
||
|
||
class FileSystemOrderManager(OrderManager): | ||
def __init__(self, file_path): | ||
super().__init__() | ||
self.file_path = Path(file_path) | ||
logging.info('loading orders from: {}'.format(self.file_path)) | ||
if self.file_path.exists(): | ||
with open(self.file_path, 'r') as file: | ||
self.orders = {ast.literal_eval(k): v for k, v in json.load(file).items()} | ||
logging.info('loaded orders {}'.format(self.orders)) | ||
else: | ||
logging.error('orders file does not exist, creating one...') | ||
self._save_to_file() | ||
|
||
def add_order(self, message_id, user, order): | ||
super().add_order(message_id, user, order) | ||
self._save_to_file() | ||
|
||
def delete_order(self, user): | ||
deleted_orders = super().delete_order(user) | ||
self._save_to_file() | ||
return deleted_orders | ||
|
||
def clear_orders(self): | ||
super().clear_orders() | ||
self._save_to_file() | ||
|
||
def _save_to_file(self): | ||
with open(self.file_path, 'w') as file: | ||
json.dump({str(k): v for k, v in self.orders.items()}, file) |