Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ibd-big-data-visualization-project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Liviu-Ionuț GHEORGHE
ibd-big-data-visualization-project
Commits
1f77e14f
Commit
1f77e14f
authored
3 months ago
by
stefan
Browse files
Options
Downloads
Patches
Plain Diff
modified message delay
parent
4cf96b81
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
producer/main.py
+3
-0
3 additions, 0 deletions
producer/main.py
producer/utils/publisher.py
+8
-9
8 additions, 9 deletions
producer/utils/publisher.py
with
11 additions
and
9 deletions
producer/main.py
+
3
−
0
View file @
1f77e14f
...
...
@@ -7,6 +7,7 @@ from utils.publisher import RabbitMQPublisher
app
=
FastAPI
(
title
=
"
NY BUS Producer API
"
,
description
=
"
IBD Producer API to publish NY BUS data.
"
,)
publisher
=
RabbitMQPublisher
()
@app.get
(
"
/
"
,
summary
=
"
Root Endpoint
"
)
...
...
@@ -27,6 +28,8 @@ async def start_publishing(background_tasks: BackgroundTasks):
status_code
=
400
,
content
=
{
"
message
"
:
"
Publisher is already running
"
}
)
else
:
publisher
.
connect
()
background_tasks
.
add_task
(
publisher
.
start_publishing
)
return
{
"
message
"
:
"
Started publishing messages
"
}
...
...
This diff is collapsed.
Click to expand it.
producer/utils/publisher.py
+
8
−
9
View file @
1f77e14f
...
...
@@ -19,7 +19,6 @@ class RabbitMQPublisher:
self
.
current_position
=
0
self
.
connection
=
None
self
.
channel
=
None
self
.
connect
()
def
connect
(
self
):
credentials
=
pika
.
PlainCredentials
(
self
.
rabbitmq_user
,
self
.
rabbitmq_password
)
...
...
@@ -55,13 +54,12 @@ class RabbitMQPublisher:
return
chunk
def
publish_to_rabbitmq
(
self
,
message
s
:
List
[
Dict
]
):
def
publish_to_rabbitmq
(
self
,
message
:
Dict
):
"""
Publish messages to RabbitMQ queue
"""
try
:
for
message
in
messages
:
self
.
channel
.
basic_publish
(
exchange
=
''
,
routing_key
=
self
.
queue_name
,
body
=
json
.
dumps
(
message
))
self
.
channel
.
basic_publish
(
exchange
=
''
,
routing_key
=
self
.
queue_name
,
body
=
json
.
dumps
(
message
))
except
Exception
as
e
:
LOG
.
error
(
f
"
Error publishing to RabbitMQ:
{
str
(
e
)
}
"
,
exc_info
=
True
)
...
...
@@ -74,11 +72,12 @@ class RabbitMQPublisher:
try
:
messages
=
self
.
read_csv_chunk
()
if
messages
:
self
.
publish_to_rabbitmq
(
messages
)
await
asyncio
.
sleep
(
randint
(
5
,
30
))
for
message
in
messages
:
self
.
publish_to_rabbitmq
(
message
)
await
asyncio
.
sleep
(
randint
(
5
,
30
))
except
Exception
as
e
:
LOG
.
error
(
f
"
Error in publishing loop:
{
str
(
e
)
}
"
,
exc_info
=
True
)
await
asyncio
.
sleep
(
randint
(
5
,
30
))
# Still sleep on error to prevent tight loop
await
asyncio
.
sleep
(
5
)
def
stop_publishing
(
self
):
"""
Stop publishing messages
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment