-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenew_worker.py
33 lines (24 loc) · 979 Bytes
/
renew_worker.py
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
import asyncio
from celery import Celery
from config import Settings as settings
from infrastructure.postgres.connection import create_session_factory
from infrastructure.postgres.mapping import start_mapping
from workers.renew import RenewSubscriptionWorker
app = Celery('tasks', broker='redis://14.225.36.41:6379/0')
@app.task(name="create_invoice")
def create_invoice(subscription_id):
...
async def job(session_factory):
worker = RenewSubscriptionWorker(session_factory, create_invoice)
await worker.handle()
async def schedule_job():
session_factory = create_session_factory(settings.async_postgresql_uri)
start_mapping()
while True:
# schedule the job to run in 3 seconds
await asyncio.sleep(settings.overdue_invoice_schedule_interval)
await asyncio.ensure_future(job(session_factory))
if __name__ == '__main__':
evtlp = asyncio.new_event_loop()
asyncio.set_event_loop(evtlp)
asyncio.run(schedule_job())