Skip to content

Commit

Permalink
Merge pull request #5 from digitalfortress-dev/feat/allow-publish-laz…
Browse files Browse the repository at this point in the history
…y-message-without-subscribe

feat: allow to publish lazy message without subscribe
  • Loading branch information
haihuynhDF authored Dec 6, 2023
2 parents a2bc339 + adf410a commit 86d9d9e
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 20 deletions.
64 changes: 61 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,84 @@ pip install sqs-client

## Example

#### Subscribe

```python
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
Expand Down
19 changes: 19 additions & 0 deletions examples/lazy_mode.py
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions examples/publish.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 0 additions & 5 deletions examples/example.py → examples/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "sqs-client"
version = "0.0.4"
version = "0.1.0"
authors = [
{name="Digital Fortress", email="[email protected]" },
]
Expand Down
2 changes: 1 addition & 1 deletion sqs_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions sqs_client/publisher.py
Original file line number Diff line number Diff line change
@@ -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,
}
),
)
18 changes: 8 additions & 10 deletions sqs_client/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import threading
import uuid

from sqs_client.publisher import Publisher


class Task:
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)

0 comments on commit 86d9d9e

Please sign in to comment.