-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathscheduled_jobs.py
More file actions
46 lines (36 loc) · 1.25 KB
/
scheduled_jobs.py
File metadata and controls
46 lines (36 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from data.database import save_order, get_all_orders
from products import create_product_download
from apscheduler.schedulers.background import BackgroundScheduler
import requests
def initialise_scheduled_jobs(app):
scheduler = BackgroundScheduler()
scheduler.add_job(
func=process_orders,
args=[app],
trigger="interval",
seconds=app.config["SCHEDULED_JOB_INTERVAL_SECONDS"],
)
scheduler.start()
def process_orders(app):
with app.app_context():
orders = get_queue_of_orders_to_process()
if len(orders) == 0:
return
order = orders[0]
payload = {
"product": order.product,
"customer": order.customer,
"date": order.date_placed.isoformat(),
}
response = requests.post(
app.config["FINANCE_PACKAGE_URL"] + "/ProcessPayment",
json=payload
)
response.raise_for_status()
order.set_as_processed()
save_order(order)
def get_queue_of_orders_to_process():
allOrders = get_all_orders()
queuedOrders = filter(lambda order: order.date_processed == None, allOrders)
sortedQueue = sorted(queuedOrders, key= lambda order: order.date_placed)
return list(sortedQueue)