Skip to content

Commit 45b5ad1

Browse files
committed
synchronize startup process
1 parent f998e0c commit 45b5ad1

19 files changed

+173
-96
lines changed

apps/startup/apps.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
from django.apps import AppConfig
2-
from ..ticketscraping.scraping import start
32
from datetime import datetime
43
from threading import Thread
4+
from multiprocessing import Process
5+
6+
7+
def run_prepare():
8+
# import module inside the child process to prevent execution in the parent process
9+
print(
10+
f"ticket scraping service started at {datetime.now().strftime('%d/%m/%Y %H:%M:%S')}")
11+
12+
# start sender socket
13+
from apps.ticketscraping.schedulers.async_tasks_scheduler import async_tasks_scheduler
14+
conn_thread = Thread(target=async_tasks_scheduler.connect)
15+
conn_thread.start()
16+
# wait for async tasks handler to connect
17+
conn_thread.join()
18+
19+
# start itself (scraping)
20+
from apps.ticketscraping.scraping import start
21+
start()
22+
23+
24+
def run():
25+
# starter
26+
p = Process(target=run_prepare, daemon=True)
27+
p.start()
28+
# start receiver socket
29+
from apps.ticketscraping.connection.asyn_tasks_receiver import run
30+
conn_process = Process(target=run)
31+
conn_process.start()
32+
conn_process.join()
533

634

735
class MyAppConfig(AppConfig):
836
name = "apps.startup"
937
verbose_name = "start tmtracker"
1038

1139
def ready(self):
12-
print(
13-
f"server started at {datetime.now().strftime('%d/%m/%Y %H:%M:%S')}")
14-
# start scraping
15-
Thread(target=start).start()
40+
run()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# start sockets
2+
from threading import Thread
3+
from multiprocessing import Process
4+
from apps.ticketscraping.connection.receiver_process import ReceiverProcess
5+
from apps.ticketscraping.constants import SERVICE_LOCALHOST, ASYNC_TASKS_RECEIVER_PORT
6+
7+
8+
def run_prepare():
9+
# start receiver socket
10+
from apps.ticketscraping.connection.mail_receiver import run
11+
conn_process = Process(target=run, daemon=True)
12+
conn_process.start()
13+
14+
# start sender socket
15+
from apps.ticketscraping.schedulers.mail_scheduler import mail_scheduler
16+
conn_thread = Thread(target=mail_scheduler.connect)
17+
conn_thread.start()
18+
# wait for mailer to connect
19+
conn_thread.join()
20+
21+
# start itself
22+
from apps.ticketscraping.tasks.asynchronous import run_async_tasks
23+
receiver = ReceiverProcess(run_async_tasks, SERVICE_LOCALHOST, ASYNC_TASKS_RECEIVER_PORT)
24+
receiver.connect()
25+
receiver.serve_forever()
26+
27+
28+
def run():
29+
# starter
30+
p = Process(target=run_prepare)
31+
p.start()
32+
33+
34+
if __name__ == '__main__':
35+
run()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from apps.ticketscraping.connection.receiver_process import ReceiverProcess
2+
# from ..tasks.asynchronous import run_async_tasks
3+
from apps.ticketscraping.constants import SERVICE_LOCALHOST, MAIL_RECEIVER_PORT
4+
5+
def run():
6+
# start itself
7+
receiver = ReceiverProcess(lambda x: print(
8+
x), SERVICE_LOCALHOST, MAIL_RECEIVER_PORT)
9+
receiver.connect()
10+
receiver.serve_forever()
11+
12+
if __name__ == '__main__':
13+
run()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from multiprocessing.connection import Client
2+
from threading import Semaphore
3+
4+
class Receiver:
5+
def __init__(self, hostname: str, port: int):
6+
self.lock = Semaphore(1)
7+
self.hostname = hostname
8+
self.port = port
9+
self.conn = None
10+
11+
def connect(self):
12+
self.conn = Client(address=(self.hostname, self.port,))
13+
14+
def recv(self):
15+
if self.conn is None:
16+
raise Exception('connection is not established')
17+
self.lock.acquire()
18+
res = self.conn.recv()
19+
self.lock.release()
20+
return res
21+
22+
def __del__(self):
23+
if self.conn is not None: self.conn.close()
24+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from .receiver import Receiver
2+
3+
class ReceiverProcess(Receiver):
4+
def __init__(self, action, hostname: str, port: int):
5+
super().__init__(hostname, port)
6+
self.action = action
7+
8+
def serve_forever(self):
9+
while True:
10+
res = self.recv()
11+
self.action(*res)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from multiprocessing.connection import Listener
2+
from threading import Semaphore
3+
4+
class Sender:
5+
def __init__(self, hostname: str, port: int):
6+
self.lock = Semaphore(1)
7+
self.hostname = hostname
8+
self.port = port
9+
self.conn = None
10+
11+
def connect(self):
12+
listener = Listener(address=(self.hostname, self.port))
13+
self.conn = listener.accept()
14+
print("conn accepted ", self.port)
15+
16+
def send(self, *args):
17+
if self.conn is None:
18+
raise Exception('connection is not established')
19+
self.lock.acquire()
20+
self.conn.send(args)
21+
self.lock.release()
22+
return True
23+
24+
def __del__(self):
25+
if self.conn is not None:
26+
self.conn.close()

apps/ticketscraping/constants.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
from functools import reduce
21
from uuid import uuid4
32

3+
# services - async action handlers
4+
ASYNC_TASKS_RECEIVER_PORT = 8100
5+
MAIL_RECEIVER_PORT = 8200
6+
SERVICE_LOCALHOST = 'localhost'
7+
48
ANTIBOT_JS_CODE_URL = "https://epsf.ticketmaster.com/eps-d"
59
TOKEN_INTERROGATION_URL = "https://epsf.ticketmaster.com/eps-d?d=www.ticketmaster.com"
610

apps/ticketscraping/schedulers/async_task_scheduler.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

apps/ticketscraping/schedulers/async_tasks_child_process.py

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from ..connection.sender import Sender
2+
from ..constants import SERVICE_LOCALHOST, ASYNC_TASKS_RECEIVER_PORT
3+
4+
async_tasks_scheduler = Sender(SERVICE_LOCALHOST, ASYNC_TASKS_RECEIVER_PORT)

0 commit comments

Comments
 (0)