diff --git a/README.md b/README.md index 50827a3..bf8e9a1 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ pip install sqs-client ## Example +#### Subscribe ```python from sqs_client.client import SQSClient @@ -27,19 +28,76 @@ from sqs_client.client import SQSClient sqs_client = SQSClient() -# Subscribe to an SQS +# Subscribe to a SQS @sqs_client.task( queue_name="sqs-queue-name", - lazy=True, wait_time_seconds=0, visibility_timeout=300, ) def test_task(message): print("test_task received:", message) +``` + +#### Publish +```python +from sqs_client.client import SQSClient +from sqs_client.publisher import Publisher + +sqs_client = SQSClient() + +sqs_client.publish( + queue_name="sqs-queue-name", + message="test message", +) + +# or + +publisher = Publisher( + sqs_client=sqs_client, + queue_name="sqs-queue-name", +) +``` + +### Lazy mode + +Faster to subscribe and publish a message to SQS + +```python +from sqs_client.client import SQSClient + +sqs_client = SQSClient() + + +# Subscribe to a SQS +@sqs_client.task( + queue_name="sqs-queue-name", + lazy=True, + wait_time_seconds=0, + visibility_timeout=300, +) +def test_task(message, abc): + print("test_task received message:", message) + print("test_task received abc:", abc) # Publish a message -test_task.trigger("Test message") +test_task.trigger("Test message", abc=1) +``` + +Publish a lazy mode message without subscribe + +```python +from sqs_client.client import SQSClient +from sqs_client.publisher import Publisher + +sqs_client = SQSClient() + +publisher = Publisher( + sqs_client=sqs_client, + queue_name="sqs-queue-name", +) + +publisher.publish_lazy("Test lazy message", abc=1) ``` ## License diff --git a/examples/lazy_mode.py b/examples/lazy_mode.py new file mode 100644 index 0000000..edc3f8f --- /dev/null +++ b/examples/lazy_mode.py @@ -0,0 +1,19 @@ +from sqs_client.client import SQSClient + +sqs_client = SQSClient() + + +# Subscribe to a SQS +@sqs_client.task( + queue_name="sqs-queue-name", + lazy=True, + wait_time_seconds=0, + visibility_timeout=300, +) +def test_task(message, abc): + print("test_task received message:", message) + print("test_task received abc:", abc) + + +# Publish a message +test_task.trigger("Test message", abc=1) diff --git a/examples/publish.py b/examples/publish.py new file mode 100644 index 0000000..90a6968 --- /dev/null +++ b/examples/publish.py @@ -0,0 +1,21 @@ +from sqs_client.client import SQSClient +from sqs_client.publisher import Publisher + +sqs_client = SQSClient() + +sqs_client.publish( + queue_name="sqs-queue-name", + message="test message", +) + +# or + +publisher = Publisher( + sqs_client=sqs_client, + queue_name="sqs-queue-name", +) + +publisher.publish("test message") + +# publish lazy mode message +publisher.publish_lazy("test lazy message", abc=1) diff --git a/examples/example.py b/examples/subscribe.py similarity index 78% rename from examples/example.py rename to examples/subscribe.py index 849a42b..22f6ff2 100644 --- a/examples/example.py +++ b/examples/subscribe.py @@ -6,13 +6,8 @@ # Subscribe to a SQS @sqs_client.task( queue_name="sqs-queue-name", - lazy=True, wait_time_seconds=0, visibility_timeout=300, ) def test_task(message): print("test_task received:", message) - - -# Publish a message -test_task.trigger("Test message") diff --git a/pyproject.toml b/pyproject.toml index 80a51fa..1b444e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "sqs-client" -version = "0.0.4" +version = "0.1.0" authors = [ {name="Digital Fortress", email="hai.huynh@digitalfortress.dev" }, ] diff --git a/sqs_client/client.py b/sqs_client/client.py index d57ec9a..d867a16 100644 --- a/sqs_client/client.py +++ b/sqs_client/client.py @@ -188,7 +188,7 @@ def publish( This function allows you to publish a message to an SQS queue. Args: - queue_name: (string) The name of the SQS queue you want to receives messages. + queue_name: (string) The name of the SQS queue you want to receive messages. message: (string) The message content to be sent. delay_seconds: (integer) The length of time, in seconds, for which to delay a specific message. Valid values: 0 to 900. Default: 0 diff --git a/sqs_client/publisher.py b/sqs_client/publisher.py new file mode 100644 index 0000000..6cf2f9a --- /dev/null +++ b/sqs_client/publisher.py @@ -0,0 +1,54 @@ +import json + + +class Publisher: + """ + This class represents a publisher to send messages to an SQS queue + """ + + def __init__( + self, + sqs_client, + queue_name, + delay_seconds=0, + ): + """ + Initializes the Publisher class. + + Args: + sqs_client: (SQSClient) The SQSClient of task. + queue_name: (string) The name of the SQS queue you want to send and receive messages. + delay_seconds: (integer) The length of time, in seconds, for which to delay a specific message. + Valid values: 0 to 900. Default: 0 + """ + self._sqs_client = sqs_client + self._queue_name = queue_name + self._delay_seconds = delay_seconds + + def publish(self, message): + """ + This function allows you to publish a message to an SQS queue. + + Args: + message: (string) The message content to be sent. + """ + self._sqs_client.publish( + queue_name=self._queue_name, + delay_seconds=self._delay_seconds, + message=message, + ) + + def publish_lazy(self, *args, **kwargs): + """ + This function allows you to publish a message in lazy mode. + """ + self._sqs_client.publish( + queue_name=self._queue_name, + delay_seconds=self._delay_seconds, + message=json.dumps( + { + "args": args, + "kwargs": kwargs, + } + ), + ) diff --git a/sqs_client/task.py b/sqs_client/task.py index ce69f48..10991b0 100644 --- a/sqs_client/task.py +++ b/sqs_client/task.py @@ -2,6 +2,8 @@ import threading import uuid +from sqs_client.publisher import Publisher + class Task: """ @@ -49,6 +51,11 @@ def __init__( self._delay_seconds = delay_seconds self._lazy = lazy self._thread = self._create_subscribe_thread() + self._publisher = Publisher( + sqs_client=self._sqs_client, + queue_name=self._queue_name, + delay_seconds=self._delay_seconds, + ) def __call__(self, *args, **kwargs): """ @@ -119,13 +126,4 @@ def trigger(self, *args, **kwargs): if not self._lazy: raise Exception("Trigger function only work on lazy mode") - self._sqs_client.publish( - queue_name=self._queue_name, - delay_seconds=self._delay_seconds, - message=json.dumps( - { - "args": args, - "kwargs": kwargs, - } - ), - ) + self._publisher.publish_lazy(*args, **kwargs)