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
45f3e7f4
Commit
45f3e7f4
authored
2 years ago
by
istmxrein
Browse files
Options
Downloads
Patches
Plain Diff
fix bugs bump to 1.1.1
parent
77469ec2
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/power.py
+20
-7
20 additions, 7 deletions
pilab/events/power.py
setup.py
+1
-1
1 addition, 1 deletion
setup.py
with
21 additions
and
8 deletions
pilab/events/power.py
+
20
−
7
View file @
45f3e7f4
import
functools
import
functools
import
json
import
json
import
logging
import
logging
import
threading
import
time
import
time
import
traceback
import
traceback
from
datetime
import
datetime
from
datetime
import
datetime
from
enum
import
Enum
from
enum
import
Enum
from
typing
import
List
,
Callable
,
Optional
from
typing
import
List
,
Callable
,
Optional
,
Dict
import
pika
import
pika
import
pika.exceptions
import
pika.exceptions
...
@@ -101,7 +102,7 @@ def send_switch_event(mac: str, state: State):
...
@@ -101,7 +102,7 @@ def send_switch_event(mac: str, state: State):
class
PowerConsumer
(
object
):
class
PowerConsumer
(
object
):
def
__init__
(
self
,
amqp_url
,
queues
:
List
[
str
],
callback
:
Callable
):
def
__init__
(
self
,
amqp_url
,
queues
:
List
[
str
],
callback
:
Callable
,
arguments
:
Dict
):
"""
Create a new instance of the consumer class, passing in the AMQP
"""
Create a new instance of the consumer class, passing in the AMQP
URL used to connect to RabbitMQ.
URL used to connect to RabbitMQ.
...
@@ -123,6 +124,7 @@ class PowerConsumer(object):
...
@@ -123,6 +124,7 @@ class PowerConsumer(object):
self
.
callback
=
callback
self
.
callback
=
callback
self
.
queues
=
queues
self
.
queues
=
queues
self
.
arguments
=
arguments
def
connect
(
self
):
def
connect
(
self
):
"""
This method connects to RabbitMQ, returning the connection handle.
"""
This method connects to RabbitMQ, returning the connection handle.
...
@@ -226,7 +228,10 @@ class PowerConsumer(object):
...
@@ -226,7 +228,10 @@ class PowerConsumer(object):
logger
.
info
(
'
Issuing consumer related RPC commands
'
)
logger
.
info
(
'
Issuing consumer related RPC commands
'
)
self
.
add_on_cancel_callback
()
self
.
add_on_cancel_callback
()
_consumer_tag
=
self
.
_channel
.
basic_consume
(
_consumer_tag
=
self
.
_channel
.
basic_consume
(
queue_name
,
self
.
on_message
,
auto_ack
=
False
)
queue_name
,
self
.
on_message
,
auto_ack
=
False
,
arguments
=
self
.
arguments
)
self
.
_consumer_tags
.
append
(
_consumer_tag
)
self
.
_consumer_tags
.
append
(
_consumer_tag
)
self
.
was_consuming
=
True
self
.
was_consuming
=
True
self
.
_consuming
=
True
self
.
_consuming
=
True
...
@@ -272,6 +277,7 @@ class PowerConsumer(object):
...
@@ -272,6 +277,7 @@ class PowerConsumer(object):
for
tag
in
self
.
_consumer_tags
:
for
tag
in
self
.
_consumer_tags
:
cb
=
functools
.
partial
(
self
.
on_cancelok
,
userdata
=
tag
)
cb
=
functools
.
partial
(
self
.
on_cancelok
,
userdata
=
tag
)
self
.
_channel
.
basic_cancel
(
tag
,
cb
)
self
.
_channel
.
basic_cancel
(
tag
,
cb
)
self
.
_consumer_tags
.
remove
(
tag
)
def
on_cancelok
(
self
,
_unused_frame
,
userdata
):
def
on_cancelok
(
self
,
_unused_frame
,
userdata
):
if
not
self
.
_consumer_tags
:
if
not
self
.
_consumer_tags
:
...
@@ -317,16 +323,22 @@ class PowerConsumer(object):
...
@@ -317,16 +323,22 @@ class PowerConsumer(object):
logger
.
info
(
'
Stopped
'
)
logger
.
info
(
'
Stopped
'
)
class
ReconnectingPowerConsumer
(
object
):
class
ReconnectingPowerConsumer
(
threading
.
Thread
):
"""
This is an example consumer that will reconnect if the nested
"""
This is an example consumer that will reconnect if the nested
ExampleConsumer indicates that a reconnect is necessary.
ExampleConsumer indicates that a reconnect is necessary.
"""
"""
def
__init__
(
self
,
queues
:
List
[
str
],
callback
:
Callable
,
amqp_url
=
utils
.
BROKER_URL
):
def
__init__
(
self
,
queues
:
List
[
str
],
callback
:
Callable
,
amqp_url
:
str
=
utils
.
BROKER_URL
,
arguments
:
Dict
=
{
"
x-stream-offset
"
:
"
last
"
}):
super
(
ReconnectingPowerConsumer
,
self
).
__init__
()
self
.
_reconnect_delay
=
0
self
.
_reconnect_delay
=
0
self
.
_amqp_url
=
amqp_url
self
.
_amqp_url
=
amqp_url
self
.
_consumer
=
PowerConsumer
(
self
.
_amqp_url
,
queues
,
callback
)
self
.
_callback
=
callback
self
.
_queues
=
queues
self
.
_arguments
=
arguments
self
.
_consumer
=
PowerConsumer
(
amqp_url
=
self
.
_amqp_url
,
queues
=
self
.
_queues
,
callback
=
self
.
_callback
,
arguments
=
self
.
_arguments
)
def
run
(
self
):
def
run
(
self
):
while
True
:
while
True
:
...
@@ -343,7 +355,8 @@ class ReconnectingPowerConsumer(object):
...
@@ -343,7 +355,8 @@ class ReconnectingPowerConsumer(object):
reconnect_delay
=
self
.
_get_reconnect_delay
()
reconnect_delay
=
self
.
_get_reconnect_delay
()
logger
.
info
(
'
Reconnecting after %d seconds
'
,
reconnect_delay
)
logger
.
info
(
'
Reconnecting after %d seconds
'
,
reconnect_delay
)
time
.
sleep
(
reconnect_delay
)
time
.
sleep
(
reconnect_delay
)
self
.
_consumer
=
PowerConsumer
(
self
.
_amqp_url
)
self
.
_consumer
=
PowerConsumer
(
amqp_url
=
self
.
_amqp_url
,
queues
=
self
.
_queues
,
callback
=
self
.
_callback
,
arguments
=
self
.
_arguments
)
def
_get_reconnect_delay
(
self
):
def
_get_reconnect_delay
(
self
):
if
self
.
_consumer
.
was_consuming
:
if
self
.
_consumer
.
was_consuming
:
...
...
This diff is collapsed.
Click to expand it.
setup.py
+
1
−
1
View file @
45f3e7f4
...
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
...
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup
(
setup
(
name
=
'
pilab
'
,
name
=
'
pilab
'
,
version
=
'
1.1.
0
'
,
version
=
'
1.1.
1
'
,
description
=
'
Shared-Libs for the pi-lab microservices
'
,
description
=
'
Shared-Libs for the pi-lab microservices
'
,
url
=
'
https://code.fbi.h-da.de/api/v4/projects/27896/packages/pypi/pilab
'
,
url
=
'
https://code.fbi.h-da.de/api/v4/projects/27896/packages/pypi/pilab
'
,
author
=
'
Max Reinheimer
'
,
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