onestep 0.4.1__tar.gz → 0.4.3__tar.gz
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.
Potentially problematic release.
This version of onestep might be problematic. Click here for more details.
- {onestep-0.4.1 → onestep-0.4.3}/PKG-INFO +4 -2
- {onestep-0.4.1 → onestep-0.4.3}/pyproject.toml +2 -1
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/__init__.py +4 -1
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/broker/base.py +3 -3
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/broker/cron.py +6 -4
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/broker/memory.py +1 -1
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/broker/rabbitmq.py +45 -9
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/broker/redis/pubsub.py +2 -2
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/broker/redis/stream.py +2 -2
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/broker/webhook.py +6 -4
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/cli.py +8 -2
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/middleware/unique.py +1 -1
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/onestep.py +1 -1
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/worker.py +4 -1
- {onestep-0.4.1 → onestep-0.4.3}/README.md +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/_utils.py +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/broker/__init__.py +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/broker/redis/__init__.py +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/exception.py +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/message.py +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/middleware/__init__.py +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/middleware/base.py +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/middleware/config.py +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/retry.py +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/signal.py +0 -0
- {onestep-0.4.1 → onestep-0.4.3}/src/onestep/state.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: onestep
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.3
|
|
4
4
|
Summary: 仅需一步,轻松实现分布式异步任务。
|
|
5
5
|
Author: miclon
|
|
6
6
|
Author-email: jcnd@163.com
|
|
@@ -11,6 +11,8 @@ Classifier: Programming Language :: Python :: 3.9
|
|
|
11
11
|
Classifier: Programming Language :: Python :: 3.10
|
|
12
12
|
Classifier: Programming Language :: Python :: 3.11
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
16
|
Provides-Extra: redis
|
|
15
17
|
Requires-Dist: asgiref (>=3.6.0,<4.0.0)
|
|
16
18
|
Requires-Dist: blinker (>=1.5,<2.0)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "onestep"
|
|
3
|
-
version = "0.4.
|
|
3
|
+
version = "0.4.3"
|
|
4
4
|
description = "仅需一步,轻松实现分布式异步任务。"
|
|
5
5
|
authors = ["miclon <jcnd@163.com>"]
|
|
6
6
|
readme = "README.md"
|
|
@@ -28,6 +28,7 @@ nacos-sdk-python = "^0.1.12"
|
|
|
28
28
|
redis = "^4.5.1"
|
|
29
29
|
autopep8 = "^2.0.2"
|
|
30
30
|
loguru = "^0.6.0"
|
|
31
|
+
use-redis = "^0.1.92"
|
|
31
32
|
|
|
32
33
|
|
|
33
34
|
[tool.poetry.group.test.dependencies]
|
|
@@ -11,6 +11,7 @@ from .middleware import (
|
|
|
11
11
|
BaseMiddleware, BaseConfigMiddleware,
|
|
12
12
|
NacosPublishConfigMiddleware, NacosConsumeConfigMiddleware,
|
|
13
13
|
RedisPublishConfigMiddleware, RedisConsumeConfigMiddleware,
|
|
14
|
+
UniqueMiddleware, MemoryUniqueMiddleware,
|
|
14
15
|
)
|
|
15
16
|
from .exception import (
|
|
16
17
|
StopMiddleware, DropMessage,
|
|
@@ -48,6 +49,8 @@ __all__ = [
|
|
|
48
49
|
'NacosConsumeConfigMiddleware',
|
|
49
50
|
'RedisPublishConfigMiddleware',
|
|
50
51
|
'RedisConsumeConfigMiddleware',
|
|
52
|
+
'UniqueMiddleware',
|
|
53
|
+
'MemoryUniqueMiddleware',
|
|
51
54
|
|
|
52
55
|
# exception
|
|
53
56
|
'StopMiddleware',
|
|
@@ -59,4 +62,4 @@ __all__ = [
|
|
|
59
62
|
'__version__'
|
|
60
63
|
]
|
|
61
64
|
|
|
62
|
-
__version__ = '0.4.
|
|
65
|
+
__version__ = '0.4.3'
|
|
@@ -43,15 +43,15 @@ class BaseBroker:
|
|
|
43
43
|
def add_middleware(self, middleware: BaseMiddleware):
|
|
44
44
|
self.middlewares.append(middleware)
|
|
45
45
|
|
|
46
|
-
def send(self, message):
|
|
46
|
+
def send(self, message, *args, **kwargs):
|
|
47
47
|
"""对消息进行预处理,然后再发送"""
|
|
48
48
|
if not isinstance(message, Message):
|
|
49
49
|
message = self.message_cls(body=message)
|
|
50
50
|
# TODO: 对消息发送进行N次重试,确保消息发送成功。
|
|
51
|
-
return self.publish(message.to_json())
|
|
51
|
+
return self.publish(message.to_json(), *args, **kwargs)
|
|
52
52
|
|
|
53
53
|
@abc.abstractmethod
|
|
54
|
-
def publish(self, message: Any):
|
|
54
|
+
def publish(self, message: Any, *args, **kwargs):
|
|
55
55
|
"""
|
|
56
56
|
将消息原样发布到 broker 中。如果当前Broker是Job的to_broker, 则必须实现此方法
|
|
57
57
|
"""
|
|
@@ -14,14 +14,15 @@ logger = logging.getLogger(__name__)
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
class CronBroker(MemoryBroker):
|
|
17
|
-
_thread = None
|
|
18
17
|
|
|
19
|
-
def __init__(self, cron, name=None, middlewares=None, body: Any = None, *args, **kwargs):
|
|
18
|
+
def __init__(self, cron, name=None, middlewares=None, body: Any = None, start_time=None, *args, **kwargs):
|
|
20
19
|
super().__init__(name=name, middlewares=middlewares, *args, **kwargs)
|
|
21
20
|
self.cron = cron
|
|
22
|
-
self.
|
|
21
|
+
self.start_time = start_time or datetime.now()
|
|
22
|
+
self.itr = croniter(cron, self.start_time)
|
|
23
23
|
self.next_fire_time = self.itr.get_next(datetime)
|
|
24
24
|
self.body = body
|
|
25
|
+
self._thread = None
|
|
25
26
|
|
|
26
27
|
def _scheduler(self):
|
|
27
28
|
if self.next_fire_time <= datetime.now():
|
|
@@ -36,7 +37,8 @@ class CronBroker(MemoryBroker):
|
|
|
36
37
|
return CronConsumer(self, *args, **kwargs)
|
|
37
38
|
|
|
38
39
|
def shutdown(self):
|
|
39
|
-
self._thread
|
|
40
|
+
if self._thread:
|
|
41
|
+
self._thread.cancel()
|
|
40
42
|
|
|
41
43
|
|
|
42
44
|
class CronConsumer(MemoryConsumer):
|
|
@@ -3,17 +3,21 @@ import threading
|
|
|
3
3
|
from queue import Queue
|
|
4
4
|
from typing import Optional, Dict, Any
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
try:
|
|
7
|
+
import amqpstorm
|
|
8
|
+
from use_rabbitmq import useRabbitMQ as RabbitMQStore
|
|
9
|
+
except ImportError:
|
|
10
|
+
amqpstorm = None
|
|
11
|
+
RabbitMQStore = None
|
|
7
12
|
|
|
8
13
|
from .base import BaseBroker, BaseConsumer
|
|
9
|
-
from use_rabbitmq import useRabbitMQ as RabbitMQStore
|
|
10
14
|
from ..message import Message
|
|
11
15
|
|
|
12
16
|
|
|
13
17
|
class _RabbitMQMessage(Message):
|
|
14
18
|
|
|
15
19
|
@classmethod
|
|
16
|
-
def from_broker(cls, broker_message
|
|
20
|
+
def from_broker(cls, broker_message):
|
|
17
21
|
try:
|
|
18
22
|
message = json.loads(broker_message.body)
|
|
19
23
|
except json.JSONDecodeError:
|
|
@@ -30,20 +34,51 @@ class _RabbitMQMessage(Message):
|
|
|
30
34
|
class RabbitMQBroker(BaseBroker):
|
|
31
35
|
message_cls = _RabbitMQMessage
|
|
32
36
|
|
|
33
|
-
def __init__(
|
|
34
|
-
|
|
37
|
+
def __init__(
|
|
38
|
+
self,
|
|
39
|
+
queue_name,
|
|
40
|
+
params: Optional[Dict] = None,
|
|
41
|
+
prefetch: Optional[int] = 1,
|
|
42
|
+
auto_create: Optional[bool]=True,
|
|
43
|
+
queue_params: Optional[Dict]=None,
|
|
44
|
+
*args,
|
|
45
|
+
**kwargs
|
|
46
|
+
):
|
|
47
|
+
"""
|
|
48
|
+
Initializes the RabbitMQ broker.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
queue_name (str): The name of the queue.
|
|
52
|
+
params (Optional[Dict], optional): Parameters for RabbitMQStore. Defaults to None.
|
|
53
|
+
prefetch (Optional[int], optional): Number of messages to prefetch. Defaults to 1.
|
|
54
|
+
auto_create (Optional[bool], optional): Whether to automatically create the queue. Defaults to True.
|
|
55
|
+
queue_params (Optional[Dict], optional): Parameters for queue declaration. Defaults to None.
|
|
56
|
+
*args: Variable length argument list.
|
|
57
|
+
**kwargs: Arbitrary keyword arguments.
|
|
58
|
+
|
|
59
|
+
Attributes:
|
|
60
|
+
queue_name (str): The name of the queue.
|
|
61
|
+
queue (Queue): The queue instance.
|
|
62
|
+
client (RabbitMQStore): The RabbitMQ client instance.
|
|
63
|
+
prefetch (int): Number of messages to prefetch.
|
|
64
|
+
threads (list): List of threads.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
if RabbitMQStore is None:
|
|
68
|
+
raise ImportError("RabbitMQ dependencies not installed. Please install 'use-rabbitmq' package.")
|
|
69
|
+
|
|
35
70
|
super().__init__(*args, **kwargs)
|
|
36
71
|
self.queue_name = queue_name
|
|
37
72
|
self.queue = Queue()
|
|
38
73
|
params = params or {}
|
|
39
74
|
self.client = RabbitMQStore(**params)
|
|
40
75
|
if auto_create:
|
|
41
|
-
self.client.declare_queue(self.queue_name)
|
|
76
|
+
self.client.declare_queue(self.queue_name, **(queue_params or {}))
|
|
42
77
|
self.prefetch = prefetch
|
|
43
78
|
self.threads = []
|
|
44
79
|
|
|
45
80
|
def _consume(self, *args, **kwargs):
|
|
46
|
-
def callback(message
|
|
81
|
+
def callback(message):
|
|
47
82
|
self.queue.put(message)
|
|
48
83
|
|
|
49
84
|
prefetch = kwargs.pop("prefetch", self.prefetch)
|
|
@@ -57,8 +92,9 @@ class RabbitMQBroker(BaseBroker):
|
|
|
57
92
|
self.threads.append(thread)
|
|
58
93
|
return RabbitMQConsumer(self)
|
|
59
94
|
|
|
60
|
-
def publish(self, message: Any):
|
|
61
|
-
|
|
95
|
+
def publish(self, message: Any, properties: Optional[dict] = None, **kwargs):
|
|
96
|
+
"""发布消息"""
|
|
97
|
+
self.client.send(self.queue_name, message, properties=properties, **kwargs)
|
|
62
98
|
|
|
63
99
|
def confirm(self, message: Message):
|
|
64
100
|
"""确认消息"""
|
|
@@ -59,12 +59,12 @@ class RedisPubSubBroker(BaseBroker):
|
|
|
59
59
|
self.threads.append(thread)
|
|
60
60
|
return RedisPubSubConsumer(self)
|
|
61
61
|
|
|
62
|
-
def send(self, message: Any):
|
|
62
|
+
def send(self, message: Any, *args, **kwargs):
|
|
63
63
|
"""Publish message to the Redis channel"""
|
|
64
64
|
if not isinstance(message, Message):
|
|
65
65
|
message = self.message_cls(body=message)
|
|
66
66
|
|
|
67
|
-
self.client.publish(self.channel, message.to_json())
|
|
67
|
+
self.client.publish(self.channel, message.to_json(), *args, **kwargs)
|
|
68
68
|
|
|
69
69
|
publish = send
|
|
70
70
|
|
|
@@ -75,12 +75,12 @@ class RedisStreamBroker(BaseBroker):
|
|
|
75
75
|
self.threads.append(thread)
|
|
76
76
|
return RedisStreamConsumer(self)
|
|
77
77
|
|
|
78
|
-
def send(self, message: Any):
|
|
78
|
+
def send(self, message: Any, *args, **kwargs):
|
|
79
79
|
"""对消息进行预处理,然后再发送"""
|
|
80
80
|
if not isinstance(message, Message):
|
|
81
81
|
message = self.message_cls(body=message)
|
|
82
82
|
|
|
83
|
-
self.client.send({"_message": message.to_json()})
|
|
83
|
+
self.client.send({"_message": message.to_json()}, *args, **kwargs)
|
|
84
84
|
|
|
85
85
|
publish = send
|
|
86
86
|
|
|
@@ -57,13 +57,15 @@ class WebHookBroker(MemoryBroker):
|
|
|
57
57
|
WebHookServer
|
|
58
58
|
)
|
|
59
59
|
self._servers[(self.host, self.port)] = hs
|
|
60
|
+
# 只有在创建新服务器时才启动线程
|
|
61
|
+
thread = threading.Thread(target=hs.serve_forever)
|
|
62
|
+
thread.daemon = True
|
|
63
|
+
thread.start()
|
|
64
|
+
self.threads.append(thread)
|
|
60
65
|
else:
|
|
61
66
|
hs = self._servers[(self.host, self.port)]
|
|
62
67
|
|
|
63
68
|
WebHookServer.servers[(self.host, self.port)].append(Server(self.path, self.queue))
|
|
64
|
-
thread = threading.Thread(target=hs.serve_forever)
|
|
65
|
-
thread.start()
|
|
66
|
-
self.threads.append(thread)
|
|
67
69
|
|
|
68
70
|
def consume(self, *args, **kwargs):
|
|
69
71
|
self._create_server()
|
|
@@ -71,7 +73,7 @@ class WebHookBroker(MemoryBroker):
|
|
|
71
73
|
return WebHookConsumer(self, *args, **kwargs)
|
|
72
74
|
|
|
73
75
|
def shutdown(self):
|
|
74
|
-
hs = self._servers
|
|
76
|
+
hs = self._servers.get((self.host, self.port))
|
|
75
77
|
if hs:
|
|
76
78
|
hs.shutdown()
|
|
77
79
|
for thread in self.threads:
|
|
@@ -8,11 +8,17 @@ LOGFORMAT = "[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s"
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
def setup_logging():
|
|
11
|
-
|
|
11
|
+
# 设置全局日志级别为INFO,避免第三方库的DEBUG日志输出
|
|
12
|
+
logging.basicConfig(level=logging.INFO, format=LOGFORMAT, stream=sys.stdout)
|
|
12
13
|
|
|
13
14
|
# exclude amqpstorm logs
|
|
14
15
|
logging.getLogger("amqpstorm").setLevel(logging.CRITICAL)
|
|
15
|
-
|
|
16
|
+
|
|
17
|
+
# 获取onestep的logger并设置为DEBUG级别以便调试
|
|
18
|
+
onestep_logger = logging.getLogger("onestep")
|
|
19
|
+
onestep_logger.setLevel(logging.DEBUG)
|
|
20
|
+
|
|
21
|
+
return onestep_logger
|
|
16
22
|
|
|
17
23
|
|
|
18
24
|
logger = setup_logging()
|
|
@@ -42,7 +42,7 @@ class BaseOneStep:
|
|
|
42
42
|
self.workers = workers or DEFAULT_WORKERS
|
|
43
43
|
self.worker_class = worker_class or DEFAULT_WORKER_CLASS
|
|
44
44
|
if self.workers > MAX_WORKERS:
|
|
45
|
-
logger.warning(f"workers[{self.workers}]
|
|
45
|
+
logger.warning(f"workers[{self.workers}] greater than {MAX_WORKERS}")
|
|
46
46
|
self.workers = MAX_WORKERS
|
|
47
47
|
self.middlewares = middlewares or []
|
|
48
48
|
|
|
@@ -169,6 +169,8 @@ class ThreadWorker(BaseWorker):
|
|
|
169
169
|
|
|
170
170
|
|
|
171
171
|
class ThreadPoolWorker(BaseWorker):
|
|
172
|
+
broker_exit: Dict[BaseBroker, bool] = {}
|
|
173
|
+
broker_exit_lock = threading.Lock()
|
|
172
174
|
|
|
173
175
|
def __init__(self, onestep, broker: BaseBroker, workers=None, *args, **kwargs):
|
|
174
176
|
super().__init__(onestep, broker, *args, **kwargs)
|
|
@@ -191,7 +193,8 @@ class ThreadPoolWorker(BaseWorker):
|
|
|
191
193
|
self.shutdown()
|
|
192
194
|
break
|
|
193
195
|
for message in self.receive_messages():
|
|
194
|
-
|
|
196
|
+
# 将消息处理提交到线程池中并发执行
|
|
197
|
+
self.executor.submit(self.handle_message, message)
|
|
195
198
|
|
|
196
199
|
def shutdown(self):
|
|
197
200
|
"""关闭线程池 Worker"""
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|