Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
shared-libs
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
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
Pi-Lab
shared-libs
Commits
b4c0ff7c
Commit
b4c0ff7c
authored
11 months ago
by
Alfred Ashur Oshana
Browse files
Options
Downloads
Patches
Plain Diff
feat: single pi reservation
parent
a15f0e7b
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
pilab/events/reservation.py
+16
-15
16 additions, 15 deletions
pilab/events/reservation.py
setup.py
+1
-1
1 addition, 1 deletion
setup.py
with
17 additions
and
16 deletions
pilab/events/reservation.py
+
16
−
15
View file @
b4c0ff7c
...
...
@@ -22,7 +22,7 @@ logging.getLogger("pika").setLevel(logging.WARNING)
class
Reservation
(
BaseModel
):
owner
:
str
cube
_id
:
int
pi
_id
:
int
starttime
:
datetime
endtime
:
datetime
extraUsers
:
Optional
[
List
[
str
]]
=
None
...
...
@@ -38,7 +38,7 @@ class DateTimeEncoder(JSONEncoder):
def
send_event
(
event
:
Reservation
):
routing_key
=
f
"
res.
cube
.
{
event
.
cube
_id
}
"
routing_key
=
f
"
res.
pi
.
{
event
.
pi
_id
}
"
try
:
connection
=
utils
.
get_blocking_connection
()
channel
=
connection
.
channel
()
...
...
@@ -69,7 +69,7 @@ def send_event(event: Reservation):
class
ReservationConsumer
(
threading
.
Thread
):
def
__init__
(
self
,
cube
s
:
List
[
int
],
callback
:
Callable
,
amqp_url
:
str
=
utils
.
BROKER_URL
,
def
__init__
(
self
,
pi
s
:
List
[
int
],
callback
:
Callable
,
amqp_url
:
str
=
utils
.
BROKER_URL
,
arguments
:
Dict
=
{
"
x-stream-offset
"
:
"
last
"
}):
"""
Create a new instance of the consumer class, passing in the AMQP
URL used to connect to RabbitMQ.
...
...
@@ -92,7 +92,7 @@ class ReservationConsumer(threading.Thread):
self
.
_prefetch_count
=
10
self
.
callback
=
callback
self
.
cubes
=
cube
s
self
.
pis
=
pi
s
self
.
arguments
=
arguments
def
connect
(
self
):
...
...
@@ -153,8 +153,9 @@ class ReservationConsumer(threading.Thread):
self
.
_channel
=
channel
self
.
_channel
.
add_on_close_callback
(
self
.
on_channel_closed
)
self
.
_channel
.
add_on_cancel_callback
(
self
.
on_consumer_cancelled
)
for
cube
in
self
.
cubes
:
queue_name
=
f
'
res.cube.
{
str
(
cube
)
}
'
for
pi
in
self
.
pis
:
#TODO queue_name = f'res.cube.{str(cube)}'
queue_name
=
f
'
res.pi.
{
str
(
pi
)
}
'
self
.
setup_queue
(
queue_name
)
def
on_channel_closed
(
self
,
channel
,
reason
):
...
...
@@ -290,13 +291,13 @@ class ReservationConsumer(threading.Thread):
logger
.
info
(
'
ReservationConsumer stopped
'
)
cube
_reservations
:
Dict
[
int
,
Reservation
]
=
{}
pi
_reservations
:
Dict
[
int
,
Reservation
]
=
{}
res_thread
:
ReservationConsumer
=
None
custom_callback
:
Callable
=
None
def
get_active_users
(
cube
_id
:
int
):
res
:
Reservation
=
cube
_reservations
.
get
(
cube
_id
)
def
get_active_users
(
pi
_id
:
int
):
res
:
Reservation
=
pi
_reservations
.
get
(
pi
_id
)
if
not
res_thread
:
raise
RuntimeError
(
"
Reservation consumer is not initialized
"
)
...
...
@@ -309,8 +310,8 @@ def get_active_users(cube_id: int):
return
[]
def
get_active_reservation
(
cube
_id
:
int
):
res
:
Reservation
=
cube
_reservations
.
get
(
cube
_id
)
def
get_active_reservation
(
pi
_id
:
int
):
res
:
Reservation
=
pi
_reservations
.
get
(
pi
_id
)
if
not
res_thread
:
raise
RuntimeError
(
"
Reservation consumer is not initialized
"
)
...
...
@@ -327,19 +328,19 @@ def restart_reservation_consumer(ids: List[int], callback: Callable = None):
custom_callback
=
callback
def
res_event_callback
(
res
:
Reservation
):
logger
.
info
(
f
'
Received Reservation for
cube
{
res
.
cube
_id
}
;
{
res
}
'
)
cube
_reservations
[
res
.
cube
_id
]
=
res
logger
.
info
(
f
'
Received Reservation for
Pi
{
res
.
pi
_id
}
;
{
res
}
'
)
pi
_reservations
[
res
.
pi
_id
]
=
res
if
custom_callback
:
custom_callback
(
res
)
if
not
res_thread
:
res_thread
=
ReservationConsumer
(
cube
s
=
ids
,
callback
=
res_event_callback
)
res_thread
=
ReservationConsumer
(
pi
s
=
ids
,
callback
=
res_event_callback
)
res_thread
.
start
()
elif
Counter
(
ids
)
!=
Counter
(
res_thread
.
cubes
):
res_thread
.
stop
()
res_thread
.
join
()
res_thread
=
ReservationConsumer
(
cube
s
=
ids
,
callback
=
res_event_callback
)
res_thread
=
ReservationConsumer
(
pi
s
=
ids
,
callback
=
res_event_callback
)
res_thread
.
start
()
return
res_thread
This diff is collapsed.
Click to expand it.
setup.py
+
1
−
1
View file @
b4c0ff7c
...
...
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup
(
name
=
'
pilab
'
,
version
=
'
4.
1
.0
'
,
version
=
'
4.
2
.0
'
,
description
=
'
Shared-Libs for the pi-lab microservices
'
,
url
=
'
https://code.fbi.h-da.de/api/v4/projects/27896/packages/pypi/pilab
'
,
author
=
'
Max Reinheimer
'
,
...
...
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