@colabo-flow/s-go 0.1.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cjs/index.js +8 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/jest.config.d.ts +3 -0
- package/{index.ts → dist/cjs/src/index.d.ts} +3 -4
- package/dist/cjs/src/lib/config.d.ts +13 -0
- package/dist/cjs/src/lib/go.amqp.client.d.ts +4 -0
- package/dist/cjs/src/lib/go.amqp.host.d.ts +6 -0
- package/dist/cjs/src/lib/go.amqp.host_status.d.ts +1 -0
- package/dist/cjs/src/lib/go.amqp.service.d.ts +1 -0
- package/dist/cjs/src/lib/operators.d.ts +2 -0
- package/dist/cjs/src/lib/params.d.ts +1 -0
- package/dist/cjs/src/lib/vos.d.ts +173 -0
- package/dist/cjs/tests/units/chalk/index.d.ts +21 -0
- package/dist/cjs/tests/units/tests-vos/tasks-and-flows.vos.d.ts +20 -0
- package/dist/colabo/src/services/puzzles/flow/go/index.js +7 -0
- package/dist/colabo/src/services/puzzles/flow/go/index.js.map +1 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/config.js +21 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/config.js.map +1 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/go.amqp.client.js +80 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/go.amqp.client.js.map +1 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/go.amqp.host.js +237 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/go.amqp.host.js.map +1 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/go.amqp.host_status.js +67 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/go.amqp.host_status.js.map +1 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/go.amqp.service.js +244 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/go.amqp.service.js.map +1 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/operators.js +15 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/operators.js.map +1 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/params.js +2 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/params.js.map +1 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/vos.js +23 -0
- package/dist/colabo/src/services/puzzles/flow/go/lib/vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/index.js +22 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/index.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/execution/execution-semaphore.js +203 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/execution/execution-semaphore.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/execution/execution-semaphore.vos.js +2 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/execution/execution-semaphore.vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/execution/execution-vos.js +45 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/execution/execution-vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/execution/execution.operators.js +42 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/execution/execution.operators.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/execution/flow-execution.service.js +334 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/execution/flow-execution.service.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/executors/executors-store.js +2 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/executors/executors-store.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/executors/executors-vos.js +30 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/executors/executors-vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-instance-engine/flow-instance-engine-updates-vos.js +10 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-instance-engine/flow-instance-engine-updates-vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-instance-engine/flow-instance-engine-vos.js +106 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-instance-engine/flow-instance-engine-vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-instance-engine/flow-instance-engine.js +1513 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-instance-engine/flow-instance-engine.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-instance-engine/index.js +5 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-instance-engine/index.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-type-utils.js +189 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-type-utils.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-condensed.operators.js +335 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-condensed.operators.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-condensed.vos.js +8 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-condensed.vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-form-condensed.operators.js +38 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-form-condensed.operators.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-form-condensed.vos.js +10 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-form-condensed.vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-form.vos.js +2 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-form.vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-translators.vos.js +5 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-translators.vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-visual.vos.js +16 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-visual.vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-vos.js +103 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types-vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types.operators.js +121 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/lib/flow-types/flow-types.operators.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/vos.js +25 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/flow/core/vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/utils/verify/index.js +3 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/utils/verify/index.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/utils/verify/lib/assertion.js +20 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/utils/verify/lib/assertion.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/utils/verify/lib/params.js +2 -0
- package/dist/colabo-vue3/src/isomorphic/dev_puzzles/utils/verify/lib/params.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/datatalks/cache/index.js +7 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/datatalks/cache/index.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/datatalks/cache/lib/datatalks-cache.service.js +33 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/datatalks/cache/lib/datatalks-cache.service.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/datatalks/cache/lib/datatalks-cache.vos.js +2 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/datatalks/cache/lib/datatalks-cache.vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/datatalks/cache/lib/params.js +2 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/datatalks/cache/lib/params.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/datatalks/cache/vos.js +2 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/datatalks/cache/vos.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/headless/core-backend/chalk/index.js +3 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/headless/core-backend/chalk/index.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/transform/json/index.js +6 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/transform/json/index.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/transform/json/lib/transform.js +582 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/transform/json/lib/transform.js.map +1 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/transform/json/lib/vos/transform.vos.js +33 -0
- package/dist/colabo-vue3/src/isomorphic/puzzles/transform/json/lib/vos/transform.vos.js.map +1 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/jest.config.d.ts +3 -0
- package/dist/esm/src/index.d.ts +3 -0
- package/dist/esm/src/lib/config.d.ts +13 -0
- package/dist/esm/src/lib/go.amqp.client.d.ts +4 -0
- package/dist/esm/src/lib/go.amqp.host.d.ts +6 -0
- package/dist/esm/src/lib/go.amqp.host_status.d.ts +1 -0
- package/dist/esm/src/lib/go.amqp.service.d.ts +1 -0
- package/dist/esm/src/lib/operators.d.ts +2 -0
- package/dist/esm/src/lib/params.d.ts +1 -0
- package/dist/esm/src/lib/vos.d.ts +173 -0
- package/dist/esm/tests/units/chalk/index.d.ts +21 -0
- package/dist/esm/tests/units/tests-vos/tasks-and-flows.vos.d.ts +20 -0
- package/dist/index.d.ts +184 -0
- package/package.json +38 -6
- package/DEVELOPMENT.md +0 -38
- package/__init__.py +0 -0
- package/__pycache__/__init__.cpython-39.pyc +0 -0
- package/create_client.py +0 -84
- package/create_host.py +0 -91
- package/create_host_terra.py +0 -103
- package/demo/__init__.py +0 -0
- package/demo/create-client.ts +0 -51
- package/demo/create-host.ts +0 -136
- package/demo/send-simple-recommended.py +0 -26
- package/demo/send-simple.py +0 -50
- package/demo/send.py +0 -352
- package/demo/send.ts +0 -40
- package/demo/threading/README.md +0 -14
- package/demo/threading/basic_consumer_connection_threaded.png +0 -0
- package/demo/threading/basic_consumer_connection_threaded.py +0 -135
- package/demo/threading/basic_consumer_threaded.py +0 -100
- package/demo/threading/long-running-publisher.py +0 -68
- package/docs/ColaboFlow-infrastructure-diagram.png +0 -0
- package/docs/workflow.md +0 -35
- package/jest.config.ts +0 -172
- package/lib/__init__.py +0 -0
- package/lib/__pycache__/__init__.cpython-39.pyc +0 -0
- package/lib/__pycache__/config.cpython-39.pyc +0 -0
- package/lib/__pycache__/executor_operators.cpython-39.pyc +0 -0
- package/lib/__pycache__/executor_vos.cpython-39.pyc +0 -0
- package/lib/__pycache__/go_amqp_client.cpython-39.pyc +0 -0
- package/lib/__pycache__/go_amqp_client_threaded.cpython-39.pyc +0 -0
- package/lib/__pycache__/go_amqp_host.cpython-39.pyc +0 -0
- package/lib/__pycache__/operators.cpython-39.pyc +0 -0
- package/lib/__pycache__/vos.cpython-39.pyc +0 -0
- package/lib/config.py +0 -21
- package/lib/config.ts +0 -22
- package/lib/executor_operators.py +0 -23
- package/lib/executor_vos.py +0 -318
- package/lib/go.amqp.client-old-basic.py +0 -50
- package/lib/go.amqp.client.ts +0 -100
- package/lib/go.amqp.host-old-basic.py +0 -48
- package/lib/go.amqp.host.ts +0 -269
- package/lib/go.amqp.host_status.ts +0 -81
- package/lib/go.amqp.service.ts +0 -272
- package/lib/go_amqp_client.py +0 -263
- package/lib/go_amqp_client_non_threaded.py +0 -152
- package/lib/go_amqp_host.py +0 -348
- package/lib/operators.py +0 -16
- package/lib/operators.ts +0 -17
- package/lib/params.ts +0 -1
- package/lib/vos.py +0 -232
- package/lib/vos.ts +0 -212
- package/requirements.txt +0 -4
- package/tests/units/README.md +0 -12
- package/tests/units/chalk/index.ts +0 -41
- package/tests/units/go.amqp.host.test.ts +0 -116
- package/tests/units/tests-vos/tasks-and-flows.vos.ts +0 -128
- package/tsconfig.json +0 -45
- package/v1 - GRPC/README.md +0 -18
- package/v1 - GRPC/node/README.md +0 -13
- package/v1 - GRPC/node/colaboflow_go_actionhost_client.ts +0 -91
- package/v1 - GRPC/node/colaboflow_go_actionhost_server.ts +0 -84
- package/v1 - GRPC/node/colaboflow_go_client.ts +0 -82
- package/v1 - GRPC/node/colaboflow_go_server.ts +0 -84
- package/v1 - GRPC/node/demos/demo-colaboflow_go_actionhost_client.ts +0 -21
- package/v1 - GRPC/node/index.ts +0 -3
- package/v1 - GRPC/node/package-lock.json +0 -770
- package/v1 - GRPC/node/package.json +0 -30
- package/v1 - GRPC/node/tsconfig.json +0 -32
- package/v1 - GRPC/node/yarn.lock +0 -642
- package/v1 - GRPC/protos/colabo/flow/go/go.proto +0 -135
- package/v1 - GRPC/python/DEVELOPMENT.md +0 -86
- package/v1 - GRPC/python/DISCUSSION.md +0 -8
- package/v1 - GRPC/python/LICENSE.txt +0 -21
- package/v1 - GRPC/python/README.md +0 -45
- package/v1 - GRPC/python/__init__.py +0 -1
- package/v1 - GRPC/python/colabo/__init__.py +0 -3
- package/v1 - GRPC/python/colabo/flow/__init__.py +0 -3
- package/v1 - GRPC/python/colabo/flow/go/__init__.py +0 -12
- package/v1 - GRPC/python/colabo/flow/go/basic/PuzzleImporter.py +0 -77
- package/v1 - GRPC/python/colabo/flow/go/basic/__init__.py +0 -5
- package/v1 - GRPC/python/colabo/flow/go/basic/colaboflow_go_basic.py +0 -66
- package/v1 - GRPC/python/colabo/flow/go/basic/parseFlowJSON.py +0 -32
- package/v1 - GRPC/python/colabo/flow/go/colaboflow_go_actionhost.py +0 -44
- package/v1 - GRPC/python/colabo/flow/go/colaboflow_go_client.py +0 -48
- package/v1 - GRPC/python/colabo/flow/go/colaboflow_go_demo.py +0 -168
- package/v1 - GRPC/python/colabo/flow/go/go_pb2.py +0 -976
- package/v1 - GRPC/python/colabo/flow/go/go_pb2_grpc.py +0 -139
- package/v1 - GRPC/python/colabo.flow.go.egg-info/PKG-INFO +0 -58
- package/v1 - GRPC/python/colabo.flow.go.egg-info/SOURCES.txt +0 -16
- package/v1 - GRPC/python/colabo.flow.go.egg-info/dependency_links.txt +0 -1
- package/v1 - GRPC/python/colabo.flow.go.egg-info/requires.txt +0 -3
- package/v1 - GRPC/python/colabo.flow.go.egg-info/top_level.txt +0 -2
- package/v1 - GRPC/python/demo-0.py +0 -69
- package/v1 - GRPC/python/demo-1.py +0 -146
- package/v1 - GRPC/python/demo-actionhost.py +0 -47
- package/v1 - GRPC/python/demo-basic.md +0 -13
- package/v1 - GRPC/python/demo-basic.py +0 -38
- package/v1 - GRPC/python/demo-executeActionSync.py +0 -32
- package/v1 - GRPC/python/demo-flows/flow-demo.json +0 -36
- package/v1 - GRPC/python/demo-puzzles/puzzles/puzzle-a.py +0 -19
- package/v1 - GRPC/python/demo-puzzles/puzzles/puzzle-b.py +0 -21
- package/v1 - GRPC/python/demo-puzzles/puzzles/puzzle-c.py +0 -20
- package/v1 - GRPC/python/requirements.txt +0 -7
- package/v1 - GRPC/python/setup.py +0 -39
- package/v1 - GRPC/python/use-cases/__init__.py +0 -0
package/demo/send.py
DELETED
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# pylint: disable=C0111,C0103,R0205
|
|
3
|
-
|
|
4
|
-
# https://pypi.org/project/pika/
|
|
5
|
-
# https://github.com/pika/pika
|
|
6
|
-
# https://github.com/pika/pika/blob/main/examples/asynchronous_publisher_example.py
|
|
7
|
-
|
|
8
|
-
import functools
|
|
9
|
-
import logging
|
|
10
|
-
import json
|
|
11
|
-
import pika
|
|
12
|
-
from pika.exchange_type import ExchangeType
|
|
13
|
-
|
|
14
|
-
LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
|
|
15
|
-
'-35s %(lineno) -5d: %(message)s')
|
|
16
|
-
LOGGER = logging.getLogger(__name__)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class ExamplePublisher(object):
|
|
20
|
-
"""This is an example publisher that will handle unexpected interactions
|
|
21
|
-
with RabbitMQ such as channel and connection closures.
|
|
22
|
-
If RabbitMQ closes the connection, it will reopen it. You should
|
|
23
|
-
look at the output, as there are limited reasons why the connection may
|
|
24
|
-
be closed, which usually are tied to permission related issues or
|
|
25
|
-
socket timeouts.
|
|
26
|
-
It uses delivery confirmations and illustrates one way to keep track of
|
|
27
|
-
messages that have been sent and if they've been confirmed by RabbitMQ.
|
|
28
|
-
"""
|
|
29
|
-
EXCHANGE = 'message'
|
|
30
|
-
EXCHANGE_TYPE = ExchangeType.topic
|
|
31
|
-
PUBLISH_INTERVAL = 1
|
|
32
|
-
ORCHESTRATOR_QUEUE = 'colabo.flow.orchestrator'
|
|
33
|
-
ROUTING_KEY = 'example.text'
|
|
34
|
-
|
|
35
|
-
def __init__(self, amqp_url):
|
|
36
|
-
"""Setup the example publisher object, passing in the URL we will use
|
|
37
|
-
to connect to RabbitMQ.
|
|
38
|
-
:param str amqp_url: The URL for connecting to RabbitMQ
|
|
39
|
-
"""
|
|
40
|
-
self._connection = None
|
|
41
|
-
self._channel = None
|
|
42
|
-
|
|
43
|
-
self._deliveries = None
|
|
44
|
-
self._acked = None
|
|
45
|
-
self._nacked = None
|
|
46
|
-
self._message_number = None
|
|
47
|
-
|
|
48
|
-
self._stopping = False
|
|
49
|
-
self._url = amqp_url
|
|
50
|
-
|
|
51
|
-
def connect(self):
|
|
52
|
-
"""This method connects to RabbitMQ, returning the connection handle.
|
|
53
|
-
When the connection is established, the on_connection_open method
|
|
54
|
-
will be invoked by pika.
|
|
55
|
-
:rtype: pika.SelectConnection
|
|
56
|
-
"""
|
|
57
|
-
LOGGER.info('Connecting to %s', self._url)
|
|
58
|
-
return pika.SelectConnection(
|
|
59
|
-
pika.URLParameters(self._url),
|
|
60
|
-
on_open_callback=self.on_connection_open,
|
|
61
|
-
on_open_error_callback=self.on_connection_open_error,
|
|
62
|
-
on_close_callback=self.on_connection_closed)
|
|
63
|
-
|
|
64
|
-
def on_connection_open(self, _unused_connection):
|
|
65
|
-
"""This method is called by pika once the connection to RabbitMQ has
|
|
66
|
-
been established. It passes the handle to the connection object in
|
|
67
|
-
case we need it, but in this case, we'll just mark it unused.
|
|
68
|
-
:param pika.SelectConnection _unused_connection: The connection
|
|
69
|
-
"""
|
|
70
|
-
LOGGER.info('Connection opened')
|
|
71
|
-
self.open_channel()
|
|
72
|
-
|
|
73
|
-
def on_connection_open_error(self, _unused_connection, err):
|
|
74
|
-
"""This method is called by pika if the connection to RabbitMQ
|
|
75
|
-
can't be established.
|
|
76
|
-
:param pika.SelectConnection _unused_connection: The connection
|
|
77
|
-
:param Exception err: The error
|
|
78
|
-
"""
|
|
79
|
-
LOGGER.error('Connection open failed, reopening in 5 seconds: %s', err)
|
|
80
|
-
self._connection.ioloop.call_later(5, self._connection.ioloop.stop)
|
|
81
|
-
|
|
82
|
-
def on_connection_closed(self, _unused_connection, reason):
|
|
83
|
-
"""This method is invoked by pika when the connection to RabbitMQ is
|
|
84
|
-
closed unexpectedly. Since it is unexpected, we will reconnect to
|
|
85
|
-
RabbitMQ if it disconnects.
|
|
86
|
-
:param pika.connection.Connection connection: The closed connection obj
|
|
87
|
-
:param Exception reason: exception representing reason for loss of
|
|
88
|
-
connection.
|
|
89
|
-
"""
|
|
90
|
-
self._channel = None
|
|
91
|
-
if self._stopping:
|
|
92
|
-
self._connection.ioloop.stop()
|
|
93
|
-
else:
|
|
94
|
-
LOGGER.warning('Connection closed, reopening in 5 seconds: %s',
|
|
95
|
-
reason)
|
|
96
|
-
self._connection.ioloop.call_later(5, self._connection.ioloop.stop)
|
|
97
|
-
|
|
98
|
-
def open_channel(self):
|
|
99
|
-
"""This method will open a new channel with RabbitMQ by issuing the
|
|
100
|
-
Channel.Open RPC command. When RabbitMQ confirms the channel is open
|
|
101
|
-
by sending the Channel.OpenOK RPC reply, the on_channel_open method
|
|
102
|
-
will be invoked.
|
|
103
|
-
"""
|
|
104
|
-
LOGGER.info('Creating a new channel')
|
|
105
|
-
self._connection.channel(on_open_callback=self.on_channel_open)
|
|
106
|
-
|
|
107
|
-
def on_channel_open(self, channel):
|
|
108
|
-
"""This method is invoked by pika when the channel has been opened.
|
|
109
|
-
The channel object is passed in so we can make use of it.
|
|
110
|
-
Since the channel is now open, we'll declare the exchange to use.
|
|
111
|
-
:param pika.channel.Channel channel: The channel object
|
|
112
|
-
"""
|
|
113
|
-
LOGGER.info('Channel opened')
|
|
114
|
-
self._channel = channel
|
|
115
|
-
self.add_on_channel_close_callback()
|
|
116
|
-
self.setup_exchange(self.EXCHANGE)
|
|
117
|
-
|
|
118
|
-
def add_on_channel_close_callback(self):
|
|
119
|
-
"""This method tells pika to call the on_channel_closed method if
|
|
120
|
-
RabbitMQ unexpectedly closes the channel.
|
|
121
|
-
"""
|
|
122
|
-
LOGGER.info('Adding channel close callback')
|
|
123
|
-
self._channel.add_on_close_callback(self.on_channel_closed)
|
|
124
|
-
|
|
125
|
-
def on_channel_closed(self, channel, reason):
|
|
126
|
-
"""Invoked by pika when RabbitMQ unexpectedly closes the channel.
|
|
127
|
-
Channels are usually closed if you attempt to do something that
|
|
128
|
-
violates the protocol, such as re-declare an exchange or queue with
|
|
129
|
-
different parameters. In this case, we'll close the connection
|
|
130
|
-
to shutdown the object.
|
|
131
|
-
:param pika.channel.Channel channel: The closed channel
|
|
132
|
-
:param Exception reason: why the channel was closed
|
|
133
|
-
"""
|
|
134
|
-
LOGGER.warning('Channel %i was closed: %s', channel, reason)
|
|
135
|
-
self._channel = None
|
|
136
|
-
if not self._stopping:
|
|
137
|
-
self._connection.close()
|
|
138
|
-
|
|
139
|
-
def setup_exchange(self, exchange_name):
|
|
140
|
-
"""Setup the exchange on RabbitMQ by invoking the Exchange.Declare RPC
|
|
141
|
-
command. When it is complete, the on_exchange_declareok method will
|
|
142
|
-
be invoked by pika.
|
|
143
|
-
:param str|unicode exchange_name: The name of the exchange to declare
|
|
144
|
-
"""
|
|
145
|
-
LOGGER.info('Declaring exchange %s', exchange_name)
|
|
146
|
-
# Note: using functools.partial is not required, it is demonstrating
|
|
147
|
-
# how arbitrary data can be passed to the callback when it is called
|
|
148
|
-
cb = functools.partial(self.on_exchange_declareok,
|
|
149
|
-
userdata=exchange_name)
|
|
150
|
-
self._channel.exchange_declare(exchange=exchange_name,
|
|
151
|
-
exchange_type=self.EXCHANGE_TYPE,
|
|
152
|
-
callback=cb)
|
|
153
|
-
|
|
154
|
-
def on_exchange_declareok(self, _unused_frame, userdata):
|
|
155
|
-
"""Invoked by pika when RabbitMQ has finished the Exchange.Declare RPC
|
|
156
|
-
command.
|
|
157
|
-
:param pika.Frame.Method unused_frame: Exchange.DeclareOk response frame
|
|
158
|
-
:param str|unicode userdata: Extra user data (exchange name)
|
|
159
|
-
"""
|
|
160
|
-
LOGGER.info('Exchange declared: %s', userdata)
|
|
161
|
-
self.setup_queue(self.ORCHESTRATOR_QUEUE)
|
|
162
|
-
|
|
163
|
-
def setup_queue(self, queue_name):
|
|
164
|
-
"""Setup the queue on RabbitMQ by invoking the Queue.Declare RPC
|
|
165
|
-
command. When it is complete, the on_queue_declareok method will
|
|
166
|
-
be invoked by pika.
|
|
167
|
-
:param str|unicode queue_name: The name of the queue to declare.
|
|
168
|
-
"""
|
|
169
|
-
LOGGER.info('Declaring queue %s', queue_name)
|
|
170
|
-
self._channel.queue_declare(queue=queue_name,
|
|
171
|
-
callback=self.on_queue_declareok)
|
|
172
|
-
|
|
173
|
-
def on_queue_declareok(self, _unused_frame):
|
|
174
|
-
"""Method invoked by pika when the Queue.Declare RPC call made in
|
|
175
|
-
setup_queue has completed. In this method we will bind the queue
|
|
176
|
-
and exchange together with the routing key by issuing the Queue.Bind
|
|
177
|
-
RPC command. When this command is complete, the on_bindok method will
|
|
178
|
-
be invoked by pika.
|
|
179
|
-
:param pika.frame.Method method_frame: The Queue.DeclareOk frame
|
|
180
|
-
"""
|
|
181
|
-
LOGGER.info('Binding %s to %s with %s', self.EXCHANGE, self.ORCHESTRATOR_QUEUE,
|
|
182
|
-
self.ROUTING_KEY)
|
|
183
|
-
self._channel.queue_bind(self.ORCHESTRATOR_QUEUE,
|
|
184
|
-
self.EXCHANGE,
|
|
185
|
-
routing_key=self.ROUTING_KEY,
|
|
186
|
-
callback=self.on_bindok)
|
|
187
|
-
|
|
188
|
-
def on_bindok(self, _unused_frame):
|
|
189
|
-
"""This method is invoked by pika when it receives the Queue.BindOk
|
|
190
|
-
response from RabbitMQ. Since we know we're now setup and bound, it's
|
|
191
|
-
time to start publishing."""
|
|
192
|
-
LOGGER.info('Queue bound')
|
|
193
|
-
self.start_publishing()
|
|
194
|
-
|
|
195
|
-
def start_publishing(self):
|
|
196
|
-
"""This method will enable delivery confirmations and schedule the
|
|
197
|
-
first message to be sent to RabbitMQ
|
|
198
|
-
"""
|
|
199
|
-
LOGGER.info('Issuing consumer related RPC commands')
|
|
200
|
-
self.enable_delivery_confirmations()
|
|
201
|
-
self.schedule_next_message()
|
|
202
|
-
|
|
203
|
-
def enable_delivery_confirmations(self):
|
|
204
|
-
"""Send the Confirm.Select RPC method to RabbitMQ to enable delivery
|
|
205
|
-
confirmations on the channel. The only way to turn this off is to close
|
|
206
|
-
the channel and create a new one.
|
|
207
|
-
When the message is confirmed from RabbitMQ, the
|
|
208
|
-
on_delivery_confirmation method will be invoked passing in a Basic.Ack
|
|
209
|
-
or Basic.Nack method from RabbitMQ that will indicate which messages it
|
|
210
|
-
is confirming or rejecting.
|
|
211
|
-
"""
|
|
212
|
-
LOGGER.info('Issuing Confirm.Select RPC command')
|
|
213
|
-
self._channel.confirm_delivery(self.on_delivery_confirmation)
|
|
214
|
-
|
|
215
|
-
def on_delivery_confirmation(self, method_frame):
|
|
216
|
-
"""Invoked by pika when RabbitMQ responds to a Basic.Publish RPC
|
|
217
|
-
command, passing in either a Basic.Ack or Basic.Nack frame with
|
|
218
|
-
the delivery tag of the message that was published. The delivery tag
|
|
219
|
-
is an integer counter indicating the message number that was sent
|
|
220
|
-
on the channel via Basic.Publish. Here we're just doing house keeping
|
|
221
|
-
to keep track of stats and remove message numbers that we expect
|
|
222
|
-
a delivery confirmation of from the list used to keep track of messages
|
|
223
|
-
that are pending confirmation.
|
|
224
|
-
:param pika.frame.Method method_frame: Basic.Ack or Basic.Nack frame
|
|
225
|
-
"""
|
|
226
|
-
confirmation_type = method_frame.method.NAME.split('.')[1].lower()
|
|
227
|
-
ack_multiple = method_frame.method.multiple
|
|
228
|
-
delivery_tag = method_frame.method.delivery_tag
|
|
229
|
-
|
|
230
|
-
LOGGER.info('Received %s for delivery tag: %i (multiple: %s)',
|
|
231
|
-
confirmation_type, delivery_tag, ack_multiple)
|
|
232
|
-
|
|
233
|
-
if confirmation_type == 'ack':
|
|
234
|
-
self._acked += 1
|
|
235
|
-
elif confirmation_type == 'nack':
|
|
236
|
-
self._nacked += 1
|
|
237
|
-
|
|
238
|
-
del self._deliveries[delivery_tag]
|
|
239
|
-
|
|
240
|
-
if ack_multiple:
|
|
241
|
-
for tmp_tag in list(self._deliveries.keys()):
|
|
242
|
-
if tmp_tag <= delivery_tag:
|
|
243
|
-
self._acked += 1
|
|
244
|
-
del self._deliveries[tmp_tag]
|
|
245
|
-
"""
|
|
246
|
-
NOTE: at some point you would check self._deliveries for stale
|
|
247
|
-
entries and decide to attempt re-delivery
|
|
248
|
-
"""
|
|
249
|
-
|
|
250
|
-
LOGGER.info(
|
|
251
|
-
'Published %i messages, %i have yet to be confirmed, '
|
|
252
|
-
'%i were acked and %i were nacked', self._message_number,
|
|
253
|
-
len(self._deliveries), self._acked, self._nacked)
|
|
254
|
-
|
|
255
|
-
def schedule_next_message(self):
|
|
256
|
-
"""If we are not closing our connection to RabbitMQ, schedule another
|
|
257
|
-
message to be delivered in PUBLISH_INTERVAL seconds.
|
|
258
|
-
"""
|
|
259
|
-
LOGGER.info('Scheduling next message for %0.1f seconds',
|
|
260
|
-
self.PUBLISH_INTERVAL)
|
|
261
|
-
self._connection.ioloop.call_later(self.PUBLISH_INTERVAL,
|
|
262
|
-
self.publish_message)
|
|
263
|
-
|
|
264
|
-
def publish_message(self):
|
|
265
|
-
"""If the class is not stopping, publish a message to RabbitMQ,
|
|
266
|
-
appending a list of deliveries with the message number that was sent.
|
|
267
|
-
This list will be used to check for delivery confirmations in the
|
|
268
|
-
on_delivery_confirmations method.
|
|
269
|
-
Once the message has been sent, schedule another message to be sent.
|
|
270
|
-
The main reason I put scheduling in was just so you can get a good idea
|
|
271
|
-
of how the process is flowing by slowing down and speeding up the
|
|
272
|
-
delivery intervals by changing the PUBLISH_INTERVAL constant in the
|
|
273
|
-
class.
|
|
274
|
-
"""
|
|
275
|
-
if self._channel is None or not self._channel.is_open:
|
|
276
|
-
return
|
|
277
|
-
|
|
278
|
-
hdrs = {u'مفتاح': u' قيمة', u'键': u'值', u'キー': u'値'}
|
|
279
|
-
properties = pika.BasicProperties(app_id='example-publisher',
|
|
280
|
-
content_type='application/json',
|
|
281
|
-
headers=hdrs)
|
|
282
|
-
|
|
283
|
-
message = u'مفتاح قيمة 键 值 キー 値'
|
|
284
|
-
self._channel.basic_publish(self.EXCHANGE, self.ROUTING_KEY,
|
|
285
|
-
json.dumps(message, ensure_ascii=False),
|
|
286
|
-
properties)
|
|
287
|
-
self._message_number += 1
|
|
288
|
-
self._deliveries[self._message_number] = True
|
|
289
|
-
LOGGER.info('Published message # %i', self._message_number)
|
|
290
|
-
self.schedule_next_message()
|
|
291
|
-
|
|
292
|
-
def run(self):
|
|
293
|
-
"""Run the example code by connecting and then starting the IOLoop.
|
|
294
|
-
"""
|
|
295
|
-
while not self._stopping:
|
|
296
|
-
self._connection = None
|
|
297
|
-
self._deliveries = {}
|
|
298
|
-
self._acked = 0
|
|
299
|
-
self._nacked = 0
|
|
300
|
-
self._message_number = 0
|
|
301
|
-
|
|
302
|
-
try:
|
|
303
|
-
self._connection = self.connect()
|
|
304
|
-
self._connection.ioloop.start()
|
|
305
|
-
except KeyboardInterrupt:
|
|
306
|
-
self.stop()
|
|
307
|
-
if (self._connection is not None and
|
|
308
|
-
not self._connection.is_closed):
|
|
309
|
-
self._connection.ioloop.start()
|
|
310
|
-
|
|
311
|
-
LOGGER.info('Stopped')
|
|
312
|
-
|
|
313
|
-
def stop(self):
|
|
314
|
-
"""Stop the example by closing the channel and connection. We
|
|
315
|
-
set a flag here so that we stop scheduling new messages to be
|
|
316
|
-
published. The IOLoop is started because this method is
|
|
317
|
-
invoked by the Try/Catch below when KeyboardInterrupt is caught.
|
|
318
|
-
Starting the IOLoop again will allow the publisher to cleanly
|
|
319
|
-
disconnect from RabbitMQ.
|
|
320
|
-
"""
|
|
321
|
-
LOGGER.info('Stopping')
|
|
322
|
-
self._stopping = True
|
|
323
|
-
self.close_channel()
|
|
324
|
-
self.close_connection()
|
|
325
|
-
|
|
326
|
-
def close_channel(self):
|
|
327
|
-
"""Invoke this command to close the channel with RabbitMQ by sending
|
|
328
|
-
the Channel.Close RPC command.
|
|
329
|
-
"""
|
|
330
|
-
if self._channel is not None:
|
|
331
|
-
LOGGER.info('Closing the channel')
|
|
332
|
-
self._channel.close()
|
|
333
|
-
|
|
334
|
-
def close_connection(self):
|
|
335
|
-
"""This method closes the connection to RabbitMQ."""
|
|
336
|
-
if self._connection is not None:
|
|
337
|
-
LOGGER.info('Closing connection')
|
|
338
|
-
self._connection.close()
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
def main():
|
|
342
|
-
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
|
|
343
|
-
|
|
344
|
-
# Connect to localhost:5672 as guest with the password guest and virtual host "/" (%2F)
|
|
345
|
-
example = ExamplePublisher(
|
|
346
|
-
'amqp://guest:guest@localhost:5672/%2F?connection_attempts=3&heartbeat=3600'
|
|
347
|
-
)
|
|
348
|
-
example.run()
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
if __name__ == '__main__':
|
|
352
|
-
main()
|
package/demo/send.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// https://www.npmjs.com/package/amqplib
|
|
2
|
-
import amqplib from "amqplib";
|
|
3
|
-
import chalk from "chalk";
|
|
4
|
-
|
|
5
|
-
import { connectConfig } from "../lib/config";
|
|
6
|
-
|
|
7
|
-
process.title = "ColaboFlow.Go.Orchestrator - sending example";
|
|
8
|
-
const appNameText = `[${chalk.red.bold("Colabo")}${chalk.blue.bold("Flow")}${chalk.green.bold(".Go")}${chalk.green.yellow(".Orchestrator - Sending")}]`;
|
|
9
|
-
|
|
10
|
-
(async () => {
|
|
11
|
-
const queue = connectConfig.ORCHESTRATOR_QUEUE;
|
|
12
|
-
console.log(`${appNameText} Connecting to the server: ${chalk.greenBright(connectConfig.AMQP_URL)}`);
|
|
13
|
-
let conn: any;
|
|
14
|
-
try {
|
|
15
|
-
conn = await amqplib.connect(connectConfig.AMQP_URL);
|
|
16
|
-
} catch (err) {
|
|
17
|
-
console.error(`${appNameText} Error connecting to the "${chalk.greenBright(connectConfig.AMQP_URL)}" server: ${err}`);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
let ch2: any;
|
|
21
|
-
try {
|
|
22
|
-
ch2 = await conn.createChannel();
|
|
23
|
-
} catch (err) {
|
|
24
|
-
console.error(`${appNameText} Error creating the "${chalk.greenBright(connectConfig.ORCHESTRATOR_QUEUE)}" queue: ${err}`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
setInterval(() => {
|
|
28
|
-
// const msg1: string = "[send.ts] something to do";
|
|
29
|
-
const msg2: any = {
|
|
30
|
-
client: "send.ts",
|
|
31
|
-
type: "string",
|
|
32
|
-
msg: "sudo: make me a tea",
|
|
33
|
-
};
|
|
34
|
-
// console.log(`${appNameText} Sending [queue: "${chalk.blueBright(queue)}"]: ${JSON.stringify(msg1)}`);
|
|
35
|
-
console.log(`${appNameText} Sending [queue: "${chalk.blueBright(queue)}"]: ${JSON.stringify(msg2)}`);
|
|
36
|
-
// https://www.w3schools.com/nodejs/met_buffer_from.asp
|
|
37
|
-
ch2.sendToQueue(queue, Buffer.from(JSON.stringify(msg2)));
|
|
38
|
-
// ch2.sendToQueue(queue, Buffer.from(msg1));
|
|
39
|
-
}, 1000);
|
|
40
|
-
})();
|
package/demo/threading/README.md
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
basic_consumer_threaded
|
|
2
|
-
|
|
3
|
-
basic_consumer_connection_threaded
|
|
4
|
-
+ same as basic_consumer_threaded but the main thread is not blocked with connection so it can work
|
|
5
|
-
+ the connection is moved to a separate thread
|
|
6
|
-
|
|
7
|
-
Example of its execution:
|
|
8
|
-
|
|
9
|
-

|
|
10
|
-
|
|
11
|
-
```sh
|
|
12
|
-
# send a message to `basic_consumer_threaded`
|
|
13
|
-
rabbitmqadmin publish exchange=amq.default routing_key=standard payload='{"id": "ColaboFlow-msg:371f4180-5c92-11ed-bc66-96ba02116486", "msh": "hello"}'
|
|
14
|
-
```
|
|
Binary file
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import functools
|
|
2
|
-
import logging
|
|
3
|
-
import pika
|
|
4
|
-
import threading
|
|
5
|
-
import time
|
|
6
|
-
from yachalk import chalk
|
|
7
|
-
|
|
8
|
-
# LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
|
|
9
|
-
# '-35s %(lineno) -5d: %(message)s')
|
|
10
|
-
# LOGGER = logging.getLogger(__name__)
|
|
11
|
-
|
|
12
|
-
# logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
|
|
13
|
-
|
|
14
|
-
publisherName = "demo"
|
|
15
|
-
def appNameTextWithContext(context: str) -> str:
|
|
16
|
-
threadId = threading.get_native_id()
|
|
17
|
-
thread_id = threading.get_ident()
|
|
18
|
-
# :{chalk.blue.bold(thread_id)}
|
|
19
|
-
return f'[(thread:{chalk.blue.bold(threadId)}) {chalk.red.bold("Colabo")}{chalk.blue.bold("Flow")}{chalk.green.bold(".Go")}.{chalk.white.bold(publisherName)}:{chalk.yellow.bold(context)}]';
|
|
20
|
-
|
|
21
|
-
def ack_message(channel, delivery_tag):
|
|
22
|
-
print(f"{appNameTextWithContext('ack_message')}")
|
|
23
|
-
"""Note that `channel` must be the same pika channel instance via which
|
|
24
|
-
the message being ACKed was retrieved (AMQP protocol constraint).
|
|
25
|
-
"""
|
|
26
|
-
if channel.is_open:
|
|
27
|
-
channel.basic_ack(delivery_tag)
|
|
28
|
-
else:
|
|
29
|
-
# Channel is already closed, so we can't ACK this message;
|
|
30
|
-
# log and/or do something that makes sense for your app in this case.
|
|
31
|
-
pass
|
|
32
|
-
|
|
33
|
-
def do_work(connection, channel, delivery_tag, body):
|
|
34
|
-
print(f"{appNameTextWithContext('do_work')} I got some serious work to do. starting ... ")
|
|
35
|
-
thread_id = threading.get_ident()
|
|
36
|
-
fmt1 = 'Thread id: {} Delivery tag: {} Message body: {}'
|
|
37
|
-
# LOGGER.info(fmt1.format(thread_id, delivery_tag, body))
|
|
38
|
-
# Sleeping to simulate 10 seconds of work
|
|
39
|
-
time.sleep(10)
|
|
40
|
-
print(f"{appNameTextWithContext('do_work')} done, acknowledging ... ")
|
|
41
|
-
cb = functools.partial(ack_message, channel, delivery_tag)
|
|
42
|
-
connection.add_callback_threadsafe(cb)
|
|
43
|
-
|
|
44
|
-
def on_message(channel, method_frame, header_frame, body, args):
|
|
45
|
-
print(f"{appNameTextWithContext('on_message')}")
|
|
46
|
-
(connection, threads) = args
|
|
47
|
-
delivery_tag = method_frame.delivery_tag
|
|
48
|
-
|
|
49
|
-
print(f"{appNameTextWithContext('do_work')} I got some serious work to do. I will span a new thread to avoid blocking this ({chalk.blue('the message handling')}) thread ... ")
|
|
50
|
-
|
|
51
|
-
t = threading.Thread(target=do_work, args=(connection, channel, delivery_tag, body))
|
|
52
|
-
t.start()
|
|
53
|
-
native_id = t.native_id
|
|
54
|
-
print(f"{appNameTextWithContext('do_work')} OK, the worker thread (with the native native_id: {chalk.blue.bold(native_id)} ) is spawned")
|
|
55
|
-
threads.append(t)
|
|
56
|
-
|
|
57
|
-
# https://docs.python.org/3/library/threading.html#threading.Thread
|
|
58
|
-
threads: list[threading.Thread] = []
|
|
59
|
-
|
|
60
|
-
def connect():
|
|
61
|
-
global connection, channel
|
|
62
|
-
|
|
63
|
-
print(f"{appNameTextWithContext('connect')} connecting ...")
|
|
64
|
-
|
|
65
|
-
credentials = pika.PlainCredentials('guest', 'guest')
|
|
66
|
-
# Note: sending a short heartbeat to prove that heartbeats are still
|
|
67
|
-
# sent even though the worker simulates long-running work
|
|
68
|
-
parameters = pika.ConnectionParameters('localhost', credentials=credentials, heartbeat=5)
|
|
69
|
-
connection = pika.BlockingConnection(parameters)
|
|
70
|
-
|
|
71
|
-
channel = connection.channel()
|
|
72
|
-
channel.exchange_declare(exchange="test_exchange", exchange_type="direct", passive=False, durable=True, auto_delete=False)
|
|
73
|
-
channel.queue_declare(queue="standard", auto_delete=True)
|
|
74
|
-
channel.queue_bind(queue="standard", exchange="test_exchange", routing_key="standard_key")
|
|
75
|
-
|
|
76
|
-
# Note: prefetch is set to 1 here as an example only and to keep the number of threads created
|
|
77
|
-
# to a reasonable amount. In production you will want to test with different prefetch values
|
|
78
|
-
# to find which one provides the best performance and usability for your solution
|
|
79
|
-
# channel.basic_qos(prefetch_count=1)
|
|
80
|
-
channel.basic_qos(prefetch_count=2)
|
|
81
|
-
print(f"{appNameTextWithContext('connect')} connected")
|
|
82
|
-
|
|
83
|
-
on_message_callback = functools.partial(on_message, args=(connection, threads))
|
|
84
|
-
channel.basic_consume(on_message_callback=on_message_callback, queue='standard')
|
|
85
|
-
|
|
86
|
-
def _start_consuming(channel):
|
|
87
|
-
print(f"{appNameTextWithContext('_start_consuming')} started")
|
|
88
|
-
channel.start_consuming()
|
|
89
|
-
print(f"{appNameTextWithContext('_start_consuming')} finished")
|
|
90
|
-
|
|
91
|
-
consuming_thread: threading.Thread
|
|
92
|
-
def start_consuming():
|
|
93
|
-
global consuming_thread
|
|
94
|
-
|
|
95
|
-
print(f"{appNameTextWithContext('start_consuming')} starting '_start_consuming' as a separate thread to avoid blocking this ({chalk.blue('the message handling')}) thread")
|
|
96
|
-
consuming_thread = threading.Thread(target=_start_consuming, args=(channel, ))
|
|
97
|
-
consuming_thread.start()
|
|
98
|
-
native_id = consuming_thread.native_id
|
|
99
|
-
print(f"{appNameTextWithContext('start_consuming')} OK, the '_start_consuming' thread (with the native native_id: {chalk.blue.bold(native_id)} ) is spawned")
|
|
100
|
-
|
|
101
|
-
def _stop_consuming(channel):
|
|
102
|
-
print(f"{appNameTextWithContext('_stop_consuming')} stopping consuming")
|
|
103
|
-
channel.stop_consuming()
|
|
104
|
-
|
|
105
|
-
def do_some_main_work():
|
|
106
|
-
print(f"{appNameTextWithContext('do_some_main_work')} I will do some work and you press the '{chalk.blue.bold('CTRL+C')}' keystroke when you want to stop me ...")
|
|
107
|
-
try:
|
|
108
|
-
while True:
|
|
109
|
-
print(f"{appNameTextWithContext('do_some_main_work')} hanging around")
|
|
110
|
-
time.sleep(5)
|
|
111
|
-
except KeyboardInterrupt:
|
|
112
|
-
# extra line to compensate the CTRL+C keystroke
|
|
113
|
-
print(f"")
|
|
114
|
-
print(f"{appNameTextWithContext('do_some_main_work')} keyboard interrupt, _stop_consuming")
|
|
115
|
-
# wrong (non-connection) thread, will produce the `pika.exceptions.StreamLostError: Stream connection lost: IndexError('pop from an empty deque')` error
|
|
116
|
-
# _stop_consuming(channel)
|
|
117
|
-
connection.add_callback_threadsafe(lambda: _stop_consuming(channel))
|
|
118
|
-
|
|
119
|
-
connect()
|
|
120
|
-
start_consuming()
|
|
121
|
-
do_some_main_work()
|
|
122
|
-
|
|
123
|
-
print(f"{appNameTextWithContext('main')} waiting for threads to finish processing")
|
|
124
|
-
|
|
125
|
-
# Wait for all to complete
|
|
126
|
-
for thread in threads:
|
|
127
|
-
print(f"{appNameTextWithContext('main')} waiting for thread '{thread.name} ('{thread.native_id}') to finish processing")
|
|
128
|
-
thread.join()
|
|
129
|
-
|
|
130
|
-
print(f"{appNameTextWithContext('main')} waiting for the consuming_thread thread '{consuming_thread.name} ('{consuming_thread.native_id}') to finish processing")
|
|
131
|
-
consuming_thread.join()
|
|
132
|
-
|
|
133
|
-
print(f"{appNameTextWithContext('main')} all threads finished processing")
|
|
134
|
-
|
|
135
|
-
connection.close()
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import functools
|
|
2
|
-
import logging
|
|
3
|
-
import pika
|
|
4
|
-
import threading
|
|
5
|
-
import time
|
|
6
|
-
from yachalk import chalk
|
|
7
|
-
|
|
8
|
-
# LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
|
|
9
|
-
# '-35s %(lineno) -5d: %(message)s')
|
|
10
|
-
# LOGGER = logging.getLogger(__name__)
|
|
11
|
-
|
|
12
|
-
# logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
|
|
13
|
-
|
|
14
|
-
publisherName = "demo"
|
|
15
|
-
def appNameTextWithContext(context: str) -> str:
|
|
16
|
-
threadId = threading.get_native_id()
|
|
17
|
-
thread_id = threading.get_ident()
|
|
18
|
-
return f'[[{chalk.blue.bold(threadId)}:{chalk.blue.bold(thread_id)}]{chalk.red.bold("Colabo")}{chalk.blue.bold("Flow")}{chalk.green.bold(".Go")}.{chalk.white.bold(publisherName)}:{chalk.yellow.bold(context)}]';
|
|
19
|
-
|
|
20
|
-
def ack_message(channel, delivery_tag):
|
|
21
|
-
print(f"{appNameTextWithContext('ack_message')}")
|
|
22
|
-
"""Note that `channel` must be the same pika channel instance via which
|
|
23
|
-
the message being ACKed was retrieved (AMQP protocol constraint).
|
|
24
|
-
"""
|
|
25
|
-
if channel.is_open:
|
|
26
|
-
channel.basic_ack(delivery_tag)
|
|
27
|
-
else:
|
|
28
|
-
# Channel is already closed, so we can't ACK this message;
|
|
29
|
-
# log and/or do something that makes sense for your app in this case.
|
|
30
|
-
pass
|
|
31
|
-
|
|
32
|
-
def do_work(connection, channel, delivery_tag, body):
|
|
33
|
-
print(f"{appNameTextWithContext('do_work')} starting ... ")
|
|
34
|
-
thread_id = threading.get_ident()
|
|
35
|
-
fmt1 = 'Thread id: {} Delivery tag: {} Message body: {}'
|
|
36
|
-
# LOGGER.info(fmt1.format(thread_id, delivery_tag, body))
|
|
37
|
-
# Sleeping to simulate 10 seconds of work
|
|
38
|
-
time.sleep(10)
|
|
39
|
-
print(f"{appNameTextWithContext('do_work')} done, acknowledging ... ")
|
|
40
|
-
cb = functools.partial(ack_message, channel, delivery_tag)
|
|
41
|
-
connection.add_callback_threadsafe(cb)
|
|
42
|
-
|
|
43
|
-
def on_message(channel, method_frame, header_frame, body, args):
|
|
44
|
-
print(f"{appNameTextWithContext('on_message')}")
|
|
45
|
-
(connection, threads) = args
|
|
46
|
-
delivery_tag = method_frame.delivery_tag
|
|
47
|
-
t = threading.Thread(target=do_work, args=(connection, channel, delivery_tag, body))
|
|
48
|
-
t.start()
|
|
49
|
-
threads.append(t)
|
|
50
|
-
|
|
51
|
-
# https://docs.python.org/3/library/threading.html#threading.Thread
|
|
52
|
-
threads: list[threading.Thread] = []
|
|
53
|
-
|
|
54
|
-
def connect():
|
|
55
|
-
global connection, channel
|
|
56
|
-
|
|
57
|
-
print(f"{appNameTextWithContext('connect')} connecting ...")
|
|
58
|
-
|
|
59
|
-
credentials = pika.PlainCredentials('guest', 'guest')
|
|
60
|
-
# Note: sending a short heartbeat to prove that heartbeats are still
|
|
61
|
-
# sent even though the worker simulates long-running work
|
|
62
|
-
parameters = pika.ConnectionParameters('localhost', credentials=credentials, heartbeat=5)
|
|
63
|
-
connection = pika.BlockingConnection(parameters)
|
|
64
|
-
|
|
65
|
-
channel = connection.channel()
|
|
66
|
-
channel.exchange_declare(exchange="test_exchange", exchange_type="direct", passive=False, durable=True, auto_delete=False)
|
|
67
|
-
channel.queue_declare(queue="standard", auto_delete=True)
|
|
68
|
-
channel.queue_bind(queue="standard", exchange="test_exchange", routing_key="standard_key")
|
|
69
|
-
|
|
70
|
-
# Note: prefetch is set to 1 here as an example only and to keep the number of threads created
|
|
71
|
-
# to a reasonable amount. In production you will want to test with different prefetch values
|
|
72
|
-
# to find which one provides the best performance and usability for your solution
|
|
73
|
-
# channel.basic_qos(prefetch_count=1)
|
|
74
|
-
channel.basic_qos(prefetch_count=2)
|
|
75
|
-
print(f"{appNameTextWithContext('connect')} connected")
|
|
76
|
-
|
|
77
|
-
on_message_callback = functools.partial(on_message, args=(connection, threads))
|
|
78
|
-
channel.basic_consume(on_message_callback=on_message_callback, queue='standard')
|
|
79
|
-
|
|
80
|
-
def start_consuming():
|
|
81
|
-
try:
|
|
82
|
-
print(f"{appNameTextWithContext('start_consuming')} start_consuming")
|
|
83
|
-
channel.start_consuming()
|
|
84
|
-
except KeyboardInterrupt:
|
|
85
|
-
print(f"{appNameTextWithContext('start_consuming')} keyboard interrupt, stop_consuming")
|
|
86
|
-
channel.stop_consuming()
|
|
87
|
-
print(f"{appNameTextWithContext('start_consuming')} finished")
|
|
88
|
-
|
|
89
|
-
connect()
|
|
90
|
-
start_consuming()
|
|
91
|
-
|
|
92
|
-
print(f"{appNameTextWithContext('main')} waiting for threads to finish processing")
|
|
93
|
-
|
|
94
|
-
# Wait for all to complete
|
|
95
|
-
for thread in threads:
|
|
96
|
-
print(f"{appNameTextWithContext('main')} waiting for thread '{thread.name} ('{thread.native_id}') to finish processing")
|
|
97
|
-
thread.join()
|
|
98
|
-
print(f"{appNameTextWithContext('main')} all threads finished processing")
|
|
99
|
-
|
|
100
|
-
connection.close()
|