Skip to content

Commit c1d5875

Browse files
committed
Implement sample worker app
1 parent be77725 commit c1d5875

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.9-slim
2+
3+
WORKDIR /app
4+
5+
COPY app/ /app
6+
RUN pip install -r requirements.txt
7+
8+
CMD ["python", "worker.py"]

app/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
redis==5.0.0

app/worker.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import time
2+
import redis
3+
import os
4+
5+
# Redis configurations
6+
REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
7+
REDIS_PORT = int(os.getenv("REDIS_PORT", 6379))
8+
QUEUE_NAME = "notification_queue"
9+
10+
# Initialize Redis connection
11+
redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True)
12+
13+
def process_job(job):
14+
print(f"Processing job: {job}")
15+
# Simulate the time to send a push notification
16+
time.sleep(1)
17+
18+
def main():
19+
print("Worker started...")
20+
while True:
21+
job = redis_client.lpop(QUEUE_NAME)
22+
if job:
23+
process_job(job)
24+
else:
25+
print("No jobs in queue. Waiting...")
26+
time.sleep(5)
27+
28+
if __name__ == "__main__":
29+
main()

0 commit comments

Comments
 (0)