Reference 📚
alsek
Alsek
cli
cli
Command Line Interface
main()
Alsek CLI.
Source code in alsek/cli/cli.py
27 28 29 30 31 |
|
process_pool(package, queues, task_specific_mode, n_processes, prune_interval, slot_wait_interval, consumer_backoff_factor, consumer_backoff_floor, consumer_backoff_ceiling, log_level)
Start a process-based worker pool.
Source code in alsek/cli/cli.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
thread_pool(package, queues, task_specific_mode, n_threads, n_processes, n_process_floor, slot_wait_interval, complete_only_on_thread_exit, consumer_backoff_factor, consumer_backoff_floor, consumer_backoff_ceiling, log_level)
Start a thread-based worker pool.
Source code in alsek/cli/cli.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
helpers
Helpers
package2path(package)
Convert a Python package name into its corresponding filesystem path.
Source code in alsek/cli/helpers.py
11 12 13 14 15 16 17 18 |
|
core
Core
backoff
Backoff Algorithms
Backoff
Bases: ABC
Base backoff class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
floor
|
int
|
minimum backoff in milliseconds |
60 * 1000
|
ceiling
|
int
|
maximum backoff in milliseconds |
60 * 60 * 1000
|
zero_override
|
bool
|
override backoff to zero if the number
of |
True
|
Source code in alsek/core/backoff.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
parameters
abstractmethod
property
Parameters of the current instance which uniquely characterize it.
Returns:
Name | Type | Description |
---|---|---|
params |
dict
|
backoff parameters |
settings
property
Settings the current algorithm.
Returns:
Name | Type | Description |
---|---|---|
serialization |
BackoffSettingsType
|
summary of the current algorithm and parameters with sufficient information to reconstruct it. |
formula(incidents)
abstractmethod
Implementation of the formula for computing the backoff.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
incidents
|
int
|
current number of incidents |
required |
Returns:
Type | Description |
---|---|
int
|
int |
Source code in alsek/core/backoff.py
70 71 72 73 74 75 76 77 78 79 80 81 |
|
get(incidents)
Get the backoff.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
incidents
|
int
|
current number of incidents |
required |
Returns:
Name | Type | Description |
---|---|---|
backoff |
int
|
backoff in milliseconds |
Source code in alsek/core/backoff.py
91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
ConstantBackoff
Bases: Backoff
Constant backoff.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
constant
|
int
|
amount of time (in milliseconds) to backoff. |
60 * 1000
|
**kwargs
|
Keyword Args
|
keyword arguments to pass to
|
{}
|
Source code in alsek/core/backoff.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
parameters
property
Parameters of the current ConstantBackoff
instance which uniquely characterize it.
Returns:
Name | Type | Description |
---|---|---|
params |
dict
|
backoff parameters |
formula(incidents)
Constant backoff formula.
Implements:
where \(c\) is constant
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
incidents
|
int
|
current number of incidents |
required |
Returns:
Name | Type | Description |
---|---|---|
backoff |
int
|
backoff in milliseconds |
Source code in alsek/core/backoff.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
ExponentialBackoff
Bases: Backoff
Exponential backoff.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base
|
int
|
the base of the exponential (milliseconds) |
4
|
factor
|
int
|
factor to multiply the result by |
10000
|
**kwargs
|
Keyword Args
|
keyword arguments to pass to
|
{}
|
Source code in alsek/core/backoff.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
parameters
property
Parameters of the current ExponentialBackoff
instance which uniquely characterize it.
Returns:
Name | Type | Description |
---|---|---|
params |
dict
|
backoff parameters |
formula(incidents)
Exponential backoff formula.
Implements:
where \(f\) is factor
, \(b\) is base
and \(i\) is the number of incidents
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
incidents
|
int
|
current number of incidents |
required |
Returns:
Name | Type | Description |
---|---|---|
backoff |
int
|
backoff in milliseconds |
Source code in alsek/core/backoff.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
LinearBackoff
Bases: Backoff
Linear backoff.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
factor
|
int
|
amount of time (in milliseconds) to add to backoff after each retry. |
30 * 1000
|
**kwargs
|
Keyword Args
|
keyword arguments to pass to
|
{}
|
Source code in alsek/core/backoff.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
parameters
property
Parameters of the current LinearBackoff
instance which uniquely characterize it.
Returns:
Name | Type | Description |
---|---|---|
params |
dict
|
backoff parameters |
formula(incidents)
Linear backoff formula.
Implements:
where \(f\) is factor
and \(i\) is the number of incidents
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
incidents
|
int
|
current number of incidents |
required |
Returns:
Name | Type | Description |
---|---|---|
backoff |
int
|
backoff in milliseconds |
Source code in alsek/core/backoff.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
settings2backoff(settings)
Convert backoff settings to a Backoff
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings
|
BackoffSettingsType
|
backoff settings of
the form |
required |
Returns:
Name | Type | Description |
---|---|---|
backoff |
Backoff
|
a backoff instance |
Source code in alsek/core/backoff.py
266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
broker
Broker
Broker
Alsek Broker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
backend
|
Backend
|
backend for data storage |
required |
dlq_ttl
|
int
|
time to live (in milliseconds) for
Dead Letter Queue (DLQ). If |
DEFAULT_TTL
|
Source code in alsek/core/broker.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
ack(message)
Acknowledge a message by removing it from the data backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
a message to acknowledge |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Warning
- Messages will not be redelivered once acked.
Source code in alsek/core/broker.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
exists(message)
Determine if the message exists in the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
exists |
bool
|
whether the message exists. |
Source code in alsek/core/broker.py
52 53 54 55 56 57 58 59 60 61 62 63 |
|
fail(message)
Acknowledge and fail a message by removing it from the backend.
If dlq_ttl
is not null, the messages will be persisted to
the dead letter queue for the prescribed amount of time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/broker.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
in_dlq(message)
Determine if a message is in the dead letter queue.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
whether the message is in the DLQ. |
Source code in alsek/core/broker.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
remove(message)
Remove a message from the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/broker.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
retry(message)
Retry a message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Warning
- This method will mutate
message
by incrementing it.
Source code in alsek/core/broker.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
submit(message, ttl=DEFAULT_TTL)
Submit a message for processing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
ttl
|
int
|
time to live for the submitted message in milliseconds |
DEFAULT_TTL
|
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
MessageAlreadyExistsError
|
if the message already exists |
Source code in alsek/core/broker.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
sync_from_backend(message)
Synchronize a message's internal data with that in the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
updated_message |
Message
|
the updated message data |
Source code in alsek/core/broker.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
sync_to_backend(message)
Synchronize the data persisted in the backend with the current state of
message
held in memory.
This method is the logical inverse of sync_from_backend
; any changes
made to the message
instance are written back to the backend so that
future reads reflect the most up-to-date information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message whose current state should be persisted. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Warning
- This method will mutate
message
by updating it regardless of whether a lock is linked to it. You are responsible for ensuring that any mutation of the message's underlying data is only performed by the lock owner.
Source code in alsek/core/broker.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
concurrency
Concurrency
Lock
Distributed mutual exclusion (MUTEX) lock for controlling concurrency accross machines.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
name of the lock |
required |
backend
|
Backend
|
backend for data storage |
required |
ttl
|
int
|
time to live in milliseconds.
If |
60 * 60 * 1000
|
auto_release
|
bool
|
if |
True
|
owner_id
|
str
|
unique identifier for the lock. Do not change this value unless you know what you are doing. |
CURRENT_HOST_OWNER_ID
|
Warning
- Locks are global and do not consider queues, unless
included in
name
. - When used as a context manager, the lock is not automatically
acquired. Lock acquisition requires calling
acquire()
.
Examples:
>>> from alsek import Lock
>>> from alsek.storage.backends.redis.standard import RedisBackend
...
>>> backend = RedisBackend()
...
>>> with Lock("mutex", backend=backend) as lock:
>>> if lock.acquire():
>>> print("Acquired lock.")
>>> else:
>>> print("Did not acquire lock.")
Source code in alsek/core/concurrency.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
full_name
property
The full name of the lock including its namespace prefix.
held
property
If the lock is held by the current owner.
holder
property
Name of the owner that currently holds the lock, if any.
__enter__()
Enter the context and try to acquire the lock.
Returns:
Name | Type | Description |
---|---|---|
lock |
Lock
|
underlying lock object. |
Source code in alsek/core/concurrency.py
181 182 183 184 185 186 187 188 |
|
__exit__(exc_type, exc_val, exc_tb)
Exit the context. If auto_release
is enabled,
the lock will be released.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exc_val
|
BaseException
|
an exception from within the context |
required |
exc_val
|
BaseException
|
value of any exception from within the context |
required |
exc_tb
|
TracebackType
|
the traceback from the context |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/concurrency.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
acquire(wait=None, if_already_acquired='raise_error')
Try to acquire the lock.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wait
|
int
|
the amount of time wait to acquire
the lock (in seconds). If |
None
|
if_already_acquired
|
str
|
if |
'raise_error'
|
Returns:
Name | Type | Description |
---|---|---|
acquired |
bool
|
|
Source code in alsek/core/concurrency.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
release(raise_if_not_acquired=False)
Release the lock.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raise_if_not_acquired
|
bool
|
raise if the lock was not acquired for release. |
False
|
Returns:
Name | Type | Description |
---|---|---|
released |
bool
|
whether the lock was found and released. |
Source code in alsek/core/concurrency.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
ProcessLock
Bases: Lock
Distributed mutual exclusion (MUTEX) lock for controlling concurrency accross processes on the same host.
Source code in alsek/core/concurrency.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
ThreadLock
Bases: Lock
Distributed mutual exclusion (MUTEX) lock for controlling concurrency accross processes and threads on the same host.
Source code in alsek/core/concurrency.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
consumer
Consumer
Consumer
Tool for consuming messages generated by the broker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
broker
|
Broker
|
an Alsek broker |
required |
subset
|
(list[str], dict[str, list[str]])
|
subset of messages to consume Must be one of the following
|
None
|
backoff
|
Backoff
|
backoff to use in response to passes over the backend which did not yield any actionable messages. |
LinearBackoff(1 * 1000, floor=1000, ceiling=30000, zero_override=False)
|
Notes
- If
subset
is alist
ordict
, queue priority is derived from the order of the items. Items which appear earlier are given higher priority. - If
subset
is adict
, task priority is derived from the order of task names in the value associated with each key (queue).
Warning
- If
subset
is of typedict
, task names not included in any of the values will be ignored.
Source code in alsek/core/consumer.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
stream()
Generate a stream of messages to process from the data backend.
Returns:
Name | Type | Description |
---|---|---|
stream |
Iterable[Message]
|
an iterable of messages to process |
Source code in alsek/core/consumer.py
148 149 150 151 152 153 154 155 156 157 158 159 |
|
futures
Futures
ProcessTaskFuture
Bases: TaskFuture
Future for task execution in a separate process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task
|
Task
|
a task to perform |
required |
message
|
Message
|
a message to run |
required |
patience
|
int
|
time to wait (in milliseconds) after issuing a SIGTERM signal to the process at shutdown. If the process is still active after this time, a SIGKILL will be issued. |
1 * 1000
|
Source code in alsek/core/futures.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
|
complete
property
Whether the task has finished.
stop(exception)
Stop the future.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/futures.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
|
TaskFuture
Bases: ABC
Future for background task execution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task
|
Task
|
a task to perform |
required |
message
|
Message
|
a message to run |
required |
Source code in alsek/core/futures.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
complete
abstractmethod
property
Whether the task has finished.
time_limit_exceeded
property
Whether task has been running longer than the allowed time window.
stop(exception)
abstractmethod
Stop the future.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exception
|
Type[BaseException]
|
exception type to raise. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/futures.py
182 183 184 185 186 187 188 189 190 191 192 193 |
|
ThreadTaskFuture
Bases: TaskFuture
Future for task execution in a separate thread.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task
|
Task
|
a task to perform |
required |
message
|
Message
|
a message to run |
required |
complete_only_on_thread_exit
|
bool
|
if |
False
|
Source code in alsek/core/futures.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
|
complete
property
Whether the task has finished.
stop(exception)
Stop the future.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exception
|
Type[BaseException]
|
exception type to raise. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/futures.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
|
message
Message
Message
Alsek Message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task_name
|
str
|
the name of the task for which the message is intended |
required |
queue
|
str
|
the queue for which the message was intended.
If |
None
|
args
|
(list, tuple)
|
positional arguments to pass to
the task's function during the execution of |
None
|
kwargs
|
dict
|
keyword arguments to pass to
the task's function during the execution of |
None
|
priority
|
int
|
priority of the message within the task. Messages with lower values will be executed before messages with higher values. |
0
|
metadata
|
dict
|
a dictionary of user-defined message metadata. This can store any data types supported by the backend's serializer. |
None
|
exception_details
|
dict
|
information about any exception raised
while executing this message. See |
None
|
result_ttl
|
int
|
time to live (in milliseconds) for the
result in the result store. If a result store is provided and
this parameter is |
None
|
uuid
|
str
|
universal unique identifier for the message.
If |
None
|
progenitor_uuid
|
str
|
universal unique identifier for the message from which this message descended. (This field is only set in for tasks with triggers and/or callbacks.) |
None
|
retries
|
int
|
number of retries |
0
|
timeout
|
int
|
the maximum amount of time (in milliseconds) a task is permitted to run against this message. |
DEFAULT_TASK_TIMEOUT
|
created_at
|
int
|
UTC timestamp (in milliseconds) for when the message was created |
None
|
updated_at
|
int
|
UTC timestamp (in milliseconds) for when the message was last updated |
None
|
delay
|
int
|
delay before the message becomes ready (in milliseconds). |
None
|
previous_result
|
any
|
the output of any previously executed task. (This will only be non-null in cases where callbacks are used.) |
None
|
previous_message_uuid
|
str
|
universal unique identifier for the message for the preceding message (This will only be non-null in cases where callbacks are used.) |
None
|
callback_message_data
|
dict
|
data to construct a new message as part of a callback operation |
None
|
backoff_settings
|
dict
|
parameters to control
backoff. Expected to be of the form
|
None
|
mechanism
|
SupportedMechanismType
|
mechanism for executing the task. Must be either "process" or "thread". |
DEFAULT_MECHANISM
|
Notes
- While not recommended,
timeout
can be disabled, in effect, by setting it to a very large integer.
Source code in alsek/core/message.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
|
data
property
Underlying message data.
descendant_uuids
property
A list of uuids which have or will decent from this message.
exception_details
property
writable
information about any exception raised.
ready
property
If the messages is currently ready for processing.
ready_at
property
Timestamp denoting when the message will be ready for processing.
summary
property
High-level summary of the message object.
ttr
property
Time to ready in milliseconds.
add_to_metadata(**data)
Adds metadata to the current instance by merging provided data into the existing metadata. The function performs a non-inplace merge operation, ensuring the original metadata is not directly altered unless returned and reassigned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**data
|
Any
|
Key-value pairs to merge into the existing metadata. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Message |
Message
|
The updated instance with the merged metadata. |
Source code in alsek/core/message.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
|
clone()
Create an exact copy of the current message.
Returns:
Name | Type | Description |
---|---|---|
clone |
Message
|
the cloned message |
Source code in alsek/core/message.py
326 327 328 329 330 331 332 333 |
|
duplicate(uuid=None)
Create a duplicate of the current message, changing only uuid
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uuid
|
str
|
universal unique identifier for the new message.
If |
None
|
Returns:
Name | Type | Description |
---|---|---|
duplicate_message |
Message
|
the duplicate message |
Warning
- Linked locks are not conserved
Source code in alsek/core/message.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
get_backoff_duration()
Get the amount of time to backoff (wait) before the message is eligible for processing again, should it fail.
Returns:
Name | Type | Description |
---|---|---|
duration |
int
|
duration of the backoff in milliseconds |
Source code in alsek/core/message.py
222 223 224 225 226 227 228 229 230 231 |
|
increment_retries()
Update a message by increasing the number of retries.
Returns:
Name | Type | Description |
---|---|---|
message |
Message
|
the updated message |
Notes
updated_at
will be updated to the current time.
Warning
- Changes are not automatically persisted to the backend.
Source code in alsek/core/message.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
|
link_lock(lock, override=False)
Link a lock to the current message.
Links are formed against the long_name
of lock
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lock
|
Lock
|
a concurrency lock |
required |
override
|
bool
|
if |
False
|
Returns:
Name | Type | Description |
---|---|---|
message |
Message
|
the updated message |
Warning
- Locks links are formed in memory and are never persisted to the data backend.
Source code in alsek/core/message.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
|
release_lock(not_linked_ok, target_backend)
Release the lock linked to the message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
not_linked_ok
|
bool
|
if |
required |
target_backend
|
Backend
|
a backend to release the lock from. |
required |
Returns:
Name | Type | Description |
---|---|---|
success |
bool
|
if the lock was released successfully. |
Raises:
Type | Description |
---|---|
AttributeError
|
if no lock is associated with the message
and |
Source code in alsek/core/message.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
|
update(**data)
Update the data
in the current message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**data
|
Keyword Args
|
key value pairs of data to update |
{}
|
Returns:
Name | Type | Description |
---|---|---|
updated_message |
Message
|
the updated message |
Warning
- This method operates 'in place'. To avoid changing the current
message, first call
.clone()
, e.g.,message.clone().update(...)
. - Changes are not automatically persisted to the backend.
Source code in alsek/core/message.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
|
status
Status Tracking
StatusTracker
Alsek Status Tracker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
backend
|
Backend
|
backend to persists results to. (In almost all cases, this should be the same backend used by the Broker). |
required |
ttl
|
int
|
time to live (in milliseconds) for the status |
DEFAULT_TTL
|
enable_pubsub
|
bool
|
if |
None
|
Source code in alsek/core/status.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
delete(message, check=True)
Delete the status of message
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
check
|
bool
|
check that it is safe to delete the status.
This is done by ensuring that the current status of |
True
|
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
ValidationError
|
if |
Source code in alsek/core/status.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
exists(message)
Check if a status for message
exists in the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Type | Description |
---|---|
bool
|
bool |
Source code in alsek/core/status.py
131 132 133 134 135 136 137 138 139 140 141 |
|
get(message)
Get the status of message
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
status |
StatusUpdate
|
the status of |
Source code in alsek/core/status.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
get_pubsub_name(message)
staticmethod
Get the channel for status updates about the message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
name |
string
|
the channel for the status information |
Source code in alsek/core/status.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
get_storage_name(message)
staticmethod
Get the key for the status information about the message
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
name |
string
|
the key for the status information |
Source code in alsek/core/status.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
listen_to_updates(message, auto_exit=True)
Listen to PUBSUB updates for message
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
auto_exit
|
bool
|
if |
True
|
Returns:
Name | Type | Description |
---|---|---|
stream |
Iterable[StatusUpdate]
|
A stream of updates from the pubsub channel |
Source code in alsek/core/status.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
publish_update(message, update)
Publish a PUBSUB update for a message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
update
|
StatusUpdate
|
a status to publish |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/status.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
set(message, status, details=None)
Set a status
for message
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
status
|
TaskStatus
|
a status to set |
required |
details
|
Any
|
additional information about the status (e.g., progress percentage) |
None
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/status.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
wait_for(message, status, timeout=5.0, poll_interval=0.05)
Wait for a message to reach a desired status.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
the message to monitor |
required |
status
|
TaskStatus, tuple[TaskStatus...], list[TaskStatus]
|
the target status |
required |
timeout
|
float
|
max time to wait (in seconds). None means wait forever. |
5.0
|
poll_interval
|
float
|
how often to check (in seconds) |
0.05
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if desired status reached, False if timed out |
Source code in alsek/core/status.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
|
StatusTrackerIntegryScanner
Tool to ensure the integrity of statuses scanning a StatusTracker()
with non-terminal statuses (i.e., TaskStatus.FAILED
or TaskStatus.SUCCEEDED
)
that no longer exist in the broker. Entries which meet this criteria will have
their status set to TaskStatus.UNKNOWN
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
status_tracker
|
StatusTracker
|
status tracker to scan for messages with non-terminal status |
required |
trigger
|
(CronTrigger, DateTrigger, IntervalTrigger)
|
trigger which determines how often to perform the scan. |
IntervalTrigger(hours=1)
|
Source code in alsek/core/status.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
scan()
Run the integrity scan.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/status.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
StatusUpdate
Bases: NamedTuple
Status information.
Source code in alsek/core/status.py
40 41 42 43 44 45 46 47 48 49 50 |
|
TaskStatus
Bases: Enum
Alsek task statuses.
Source code in alsek/core/status.py
26 27 28 29 30 31 32 33 34 |
|
task
Task
Task
Alsek Task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
callable
|
function to use for the main operation |
required |
broker
|
Broker
|
an Alsek broker |
required |
name
|
str
|
the name of the task. If |
None
|
queue
|
str
|
the name of the queue to generate the task on.
If |
None
|
timeout
|
int
|
the maximum amount of time (in milliseconds) this task is permitted to run. |
DEFAULT_TASK_TIMEOUT
|
max_retries
|
int
|
maximum number of allowed retries |
DEFAULT_MAX_RETRIES
|
backoff
|
Backoff
|
backoff algorithm and parameters to use when computing delay between retries |
ExponentialBackoff()
|
result_store
|
ResultStore
|
store for persisting task results |
None
|
status_tracker
|
StatusTracker
|
store for persisting task statuses |
None
|
mechanism
|
SupportedMechanismType
|
mechanism for executing the task. Must be either "process" or "thread". |
DEFAULT_MECHANISM
|
no_positional_args
|
bool
|
if |
False
|
Notes
do_retry()
can be overridden in cases wheremax_retries
is not sufficiently complex to determine if a retry should occur.
Warning
- Timeouts are not supported for
mechanism='thread'
on Python implementations other than CPython.
Source code in alsek/core/task.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
|
deferred
property
Whether deferred mode is currently enabled.
name
property
Name of the task.
cancel_defer()
Cancel "deferred" mode.
Returns:
Name | Type | Description |
---|---|---|
task |
Task
|
the current task |
Source code in alsek/core/task.py
277 278 279 280 281 282 283 284 285 |
|
defer()
Enter "deferred" mode.
Do not submit the next message created by generate()
to the broker.
Returns:
Name | Type | Description |
---|---|---|
task |
Task
|
the current task |
Warning
- Deferred mode is automatically cancelled by
generate()
prior to it returning.
Source code in alsek/core/task.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
|
do_callback(message, result)
Whether or to submit the callback provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
message with the callback |
required |
result
|
Any
|
output of |
required |
Returns:
Type | Description |
---|---|
bool
|
bool |
Warning
- If the task message does not have a callback this method will not be invoked.
Source code in alsek/core/task.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
|
do_retry(message, exception)
Whether a failed task should be retried.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
message which failed |
required |
exception
|
BaseException
|
the exception which was raised |
required |
Returns:
Type | Description |
---|---|
bool
|
bool |
Source code in alsek/core/task.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
|
execute(message)
Execute the task against a message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
message to process |
required |
Returns:
Name | Type | Description |
---|---|---|
result |
Any
|
output of |
Source code in alsek/core/task.py
501 502 503 504 505 506 507 508 509 510 511 |
|
generate(args=None, kwargs=None, priority=0, metadata=None, result_ttl=None, uuid=None, timeout_override=None, delay=None, previous_result=None, callback=None, queue=None, submit=True, **options)
Generate an instance of the task for processing.
This method generates a new message for the task and submit it to the broker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
(list, tuple)
|
positional arguments to pass to |
None
|
kwargs
|
dict
|
keyword arguments to pass to |
None
|
priority
|
int
|
priority of the message within the task. Messages with lower values will be executed before messages with higher values. |
0
|
metadata
|
dict
|
a dictionary of user-defined message metadata. This can store any data types supported by the backend's serializer. |
None
|
result_ttl
|
int
|
time to live (in milliseconds) for the result in the result store. If a result store is provided and |
None
|
uuid
|
str
|
universal unique identifier for the message.
If |
None
|
timeout_override
|
int
|
override the default maximum runtime (in milliseconds) for instances of this task. |
None
|
delay
|
int
|
delay before message is ready (in milliseconds) |
None
|
previous_result
|
Any
|
result of a previous task. |
None
|
callback
|
(Message, tuple[Message, ...])
|
one or more messages to be submitted to the broker after the proceeding message has been successfully processed by a worker. |
None
|
queue
|
str
|
queue to use for the task. If none, the default queue for this task will be used. |
None
|
submit
|
bool
|
if |
True
|
options
|
Keyword Args
|
options to use when submitting
the message via the broker. See |
{}
|
Returns:
Name | Type | Description |
---|---|---|
message |
Message
|
message generated for the task |
Warning
submit
is overridden toFalse
if deferred mode is activeuuid
is refreshed after the first event when using a trigger.- If manually overriding
queue
such that it differs from the default for this task, Worker Pools built usingtask_specific_mode=True
will fail acknowledge its existence.
Source code in alsek/core/task.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
is_revoked(message)
Check if a message is revoked.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Type | Description |
---|---|
bool
|
None |
Source code in alsek/core/task.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
|
on_failure(message, exception)
Handles the actions to be performed when an operation fails.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
The message object containing the details of the failed operation. |
required |
exception
|
BaseException
|
The exception object associated with the failure, providing additional context or details about what caused the failure. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
|
on_retry(message, exception)
Handles the retry logic when a processing failure occurs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
The message object that failed during processing and is subject to a retry attempt. |
required |
exception
|
BaseException
|
The exception instance that was raised during the failure of processing the message. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
458 459 460 461 462 463 464 465 466 467 468 469 470 |
|
on_revocation(message, exception, result)
Handles the event when a message is revoked and logs the associated exception.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
The message instance that was revoked. |
required |
exception
|
Optional[BaseException]
|
The exception instance that represents the reason for the revocation. |
required |
result
|
Any
|
The result of the revoked operation. This is only provided if the task succeeds |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
|
on_submit(message)
Handles the action to be performed when a message is submitted. This method processes the provided message and executes the required behavior upon submission.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
The message object submitted for processing. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
287 288 289 290 291 292 293 294 295 296 297 298 |
|
on_success(message, result)
Handles successful outcomes of an operation by processing the given message and corresponding result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
An instance of the Message class that contains relevant information about the operation. |
required |
result
|
Any
|
The result of the completed operation, which can be of any type. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
487 488 489 490 491 492 493 494 495 496 497 498 499 |
|
op(message)
Pass message
data to function
for processing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
message to perform the operation against |
required |
Returns:
Name | Type | Description |
---|---|---|
result |
Any
|
output of the function |
Notes
- If, and only if, the signature of
function
contains amessage
parameter,message
itself will be passed along with anyargs
andkwargs
contained in the message.
Warning
message
will not be passed in cases where a "message" exists inmessage.kwargs
.
Source code in alsek/core/task.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
|
post_op(message, result)
Operation to perform after running op
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
message |
required |
result
|
Any
|
output of |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
429 430 431 432 433 434 435 436 437 438 439 |
|
pre_op(message)
Operation to perform before running op
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
message |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
394 395 396 397 398 399 400 401 402 403 |
|
revoke(message, skip_if_running=False)
Revoke the task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
message to revoke |
required |
skip_if_running
|
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
|
update_status(message, status)
Update the status of a message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
message to update the status of. |
required |
status
|
TaskStatus
|
status to update the message to. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
|
TriggerTask
Bases: Task
Triggered Task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
callable
|
function to use for the main operation |
required |
trigger
|
(CronTrigger, DateTrigger, IntervalTrigger)
|
trigger for task execution. |
required |
broker
|
Broker
|
an Alsek broker |
required |
name
|
str
|
the name of the task. If |
None
|
queue
|
str
|
the name of the queue to generate the task on.
If |
None
|
timeout
|
int
|
the maximum amount of time (in milliseconds) this task is permitted to run. |
DEFAULT_TASK_TIMEOUT
|
max_retries
|
int
|
maximum number of allowed retries |
DEFAULT_MAX_RETRIES
|
backoff
|
Backoff
|
backoff algorithm and parameters to use when computing delay between retries |
ExponentialBackoff()
|
result_store
|
ResultStore
|
store for persisting task results |
None
|
status_tracker
|
StatusTracker
|
store for persisting task statuses |
None
|
mechanism
|
SupportedMechanismType
|
mechanism for executing the task. Must be either "process" or "thread". |
DEFAULT_MECHANISM
|
no_positional_args
|
bool
|
if |
False
|
Raises:
Type | Description |
---|---|
* ``SchedulingError``
|
if the signature of |
Source code in alsek/core/task.py
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
|
generated
property
If the task has been generated.
clear()
Clear the currently scheduled task.
Returns:
Type | Description |
---|---|
None
|
None |
Raises * AttributeError: if a task has not yet been generated
Source code in alsek/core/task.py
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 |
|
pause()
Pause the underlying scheduler.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
725 726 727 728 729 730 731 732 |
|
resume()
Resume the underlying scheduler.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
734 735 736 737 738 739 740 741 |
|
shutdown()
Shutdown the underlying scheduler.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/task.py
743 744 745 746 747 748 749 750 |
|
task(broker, name=None, queue=None, timeout=DEFAULT_TASK_TIMEOUT, max_retries=DEFAULT_MAX_RETRIES, backoff=ExponentialBackoff(), trigger=None, result_store=None, status_tracker=None, mechanism=DEFAULT_MECHANISM, no_positional_args=False, base_task=None)
Wrapper for task construction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
broker
|
Broker
|
an Alsek broker |
required |
name
|
str
|
the name of the task. If |
None
|
queue
|
str
|
the name of the queue to generate the task on.
If |
None
|
timeout
|
int
|
the maximum amount of time (in milliseconds) this task is permitted to run. |
DEFAULT_TASK_TIMEOUT
|
max_retries
|
int
|
maximum number of allowed retries |
DEFAULT_MAX_RETRIES
|
backoff
|
Backoff
|
backoff algorithm and parameters to use when computing delay between retries |
ExponentialBackoff()
|
trigger
|
(CronTrigger, DateTrigger, IntervalTrigger)
|
trigger for task execution. |
None
|
result_store
|
ResultStore
|
store for persisting task results |
None
|
status_tracker
|
StatusTracker
|
store for persisting task statuses |
None
|
mechanism
|
SupportedMechanismType
|
mechanism for executing the task. Must be either "process" or "thread". |
DEFAULT_MECHANISM
|
no_positional_args
|
bool
|
if |
False
|
base_task
|
Type[Task]
|
base to use for task constuction.
If |
None
|
Returns:
Name | Type | Description |
---|---|---|
wrapper |
callable
|
task-wrapped function |
Raises:
Type | Description |
---|---|
* ValueError
|
if a |
Examples:
>>> from alsek import Broker, task
>>> from alsek.storage.backends.redis.standard import RedisBackend
>>> backend = RedisBackend()
>>> broker = Broker(backend)
>>> @task(broker)
... def add(a: int, b: int) -> int:
... return a + b
Source code in alsek/core/task.py
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 |
|
worker
process
Process Worker Pool
ProcessWorkerPool
Bases: BaseWorkerPool
Fixed-size pool that runs each task in its own forked process
(via ProcessTaskFuture
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_processes
|
int
|
Maximum number of live |
None
|
prune_interval
|
int
|
Number of milliseconds between background runs of a scan to prune spent futures. |
100
|
Source code in alsek/core/worker/process.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
on_shutdown()
Terminate everything that is still alive.
Source code in alsek/core/worker/process.py
88 89 90 91 92 93 94 95 96 |
|
prune()
Prune spent futures.
Source code in alsek/core/worker/process.py
77 78 79 80 81 82 83 84 85 86 |
|
submit_message(message)
Submit a single message
Source code in alsek/core/worker/process.py
98 99 100 101 102 103 104 105 106 107 108 109 |
|
thread
Thread Worker Pool
ProcessGroup
Source code in alsek/core/worker/thread.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
stop(timeout=2)
Stop the group of threads in this process group.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
(int, float)
|
the time to wait in seconds |
2
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/core/worker/thread.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
ThreadWorkerPool
Bases: BaseWorkerPool
Elastic thread-based pool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_threads
|
int
|
the number of threads to use per group. |
8
|
n_processes
|
int
|
the number of process groups to use |
None
|
n_process_floor
|
int
|
the minimum number of processes to have active at any given time, regardless of load. |
1
|
complete_only_on_thread_exit
|
bool
|
if |
False
|
**kwargs
|
Keyword Args
|
Keyword arguments to pass to |
{}
|
Notes
- Spawns a new process (ThreadProcessGroup) only when all existing
groups are saturated and the hard ceiling
n_processes
hasn’t been hit. - Each group runs up to
n_threads
true ThreadTaskFutures concurrently. - Total worker capacity is
n_threads * n_processes
.
Source code in alsek/core/worker/thread.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
on_shutdown()
Stop all futures in the pool.
Source code in alsek/core/worker/thread.py
272 273 274 275 |
|
prune()
Prune exited process groups, but only enforce floor if NOT shutting down.
Source code in alsek/core/worker/thread.py
277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
submit_message(message)
Submit a single message
Source code in alsek/core/worker/thread.py
300 301 302 303 304 305 306 307 308 |
|
ThreadsInProcessGroup
• Runs inside a forked process
• Accepts work items via Queue
• Spawns at most n_threads
ThreadTaskFutures concurrently
Source code in alsek/core/worker/thread.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
defaults
Defaults
exceptions
Exceptions
AlsekError
Bases: Exception
Base Alsek error.
Source code in alsek/exceptions.py
8 9 |
|
MessageAlreadyExistsError
Bases: AlsekError
Message already exists in backend.
Source code in alsek/exceptions.py
12 13 |
|
MessageDoesNotExistsError
Bases: AlsekError
Message does not exist in backend.
Source code in alsek/exceptions.py
16 17 |
|
MultipleBrokersError
Bases: AlsekError
Multiple brokers in use.
Source code in alsek/exceptions.py
20 21 |
|
NoTasksFoundError
Bases: AlsekError
No tasks found.
Source code in alsek/exceptions.py
24 25 |
|
RevokedError
Bases: AlsekError
Alsek task revoked error.
Source code in alsek/exceptions.py
44 45 |
|
SchedulingError
Bases: AlsekError
Error scheduling work.
Source code in alsek/exceptions.py
32 33 |
|
TaskNameCollisionError
Bases: AlsekError
Duplicate task detected.
Source code in alsek/exceptions.py
36 37 |
|
TerminationError
Bases: AlsekError
Alsek termination error.
Source code in alsek/exceptions.py
40 41 |
|
ValidationError
Bases: AlsekError
Data validation failed.
Source code in alsek/exceptions.py
28 29 |
|
storage
Storage
backends
Backend
BaseBackend
Bases: ABC
Backend base class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
namespace
|
str
|
prefix to use when inserting names in the backend |
DEFAULT_NAMESPACE
|
serializer
|
Serializer
|
tool for encoding and decoding values written into the backend. |
JsonSerializer()
|
Source code in alsek/storage/backends/__init__.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
clear_namespace(raise_on_error=True)
abstractmethod
Clear all items in backend under the current namespace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raise_on_error
|
bool
|
raise if a delete operation fails |
True
|
Returns:
Name | Type | Description |
---|---|---|
count |
int
|
number of items cleared |
Raises:
Type | Description |
---|---|
KeyError
|
if |
Source code in alsek/storage/backends/__init__.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
count(pattern=None)
abstractmethod
Count the number of items in the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str
|
pattern to limit count to |
None
|
Returns:
Name | Type | Description |
---|---|---|
count |
int
|
number of matching names |
Source code in alsek/storage/backends/__init__.py
300 301 302 303 304 305 306 307 308 309 310 311 |
|
delete(name, missing_ok=False)
abstractmethod
Delete a name
from the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
name of the item |
required |
missing_ok
|
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
KeyError
|
if |
Source code in alsek/storage/backends/__init__.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
exists(name)
abstractmethod
Check if name
exists in the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
name of the item |
required |
Returns:
Type | Description |
---|---|
bool
|
bool |
Source code in alsek/storage/backends/__init__.py
138 139 140 141 142 143 144 145 146 147 148 149 |
|
full_name(name)
Get an item's complete name, including the namespace in which it exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
the name of an item |
required |
Returns:
Name | Type | Description |
---|---|---|
full_name |
str
|
a name of the form |
Notes
- If
name
is already the full name, this method will collapse to a no-op.
Source code in alsek/storage/backends/__init__.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
get(name, default=None)
abstractmethod
Get name
from the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
name of the item |
required |
default
|
(Any, Type[Empty])
|
default value for |
None
|
Returns:
Type | Description |
---|---|
Any
|
Any |
Source code in alsek/storage/backends/__init__.py
176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
in_namespace(name)
Determine if name
belong to the current namespace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
a name (key) |
required |
Returns:
Type | Description |
---|---|
bool
|
bool |
Warning
name
should be a complete (i.e., full) name.
Source code in alsek/storage/backends/__init__.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
priority_add(key, unique_id, priority)
abstractmethod
Add an item to a priority-sorted set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
unique_id
|
str
|
The item's (Message's) unique identifier |
required |
priority
|
float
|
The numeric priority score (decide if lower or higher means higher priority). |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/storage/backends/__init__.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
priority_get(key)
abstractmethod
Get (peek) the highest-priority item without removing it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
Returns:
Name | Type | Description |
---|---|---|
item |
(str, optional)
|
The member with the highest priority, or None if empty. |
Source code in alsek/storage/backends/__init__.py
222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
priority_iter(key)
Iterate over the items in a priority-sorted set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
Returns:
Name | Type | Description |
---|---|---|
priority |
Iterable[str]
|
An iterable of members in the sorted set, sorted by priority. |
Source code in alsek/storage/backends/__init__.py
236 237 238 239 240 241 242 243 244 245 246 |
|
priority_remove(key, unique_id)
abstractmethod
Remove an item from a priority-sorted set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
unique_id
|
str
|
The item's (Message's) unique identifier |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/storage/backends/__init__.py
248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
pub(channel, value)
Publish to a channel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channel
|
str
|
channel name |
required |
value
|
Any
|
value to publish |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/storage/backends/__init__.py
262 263 264 265 266 267 268 269 270 271 272 273 |
|
scan(pattern=None)
abstractmethod
Scan the backend for matching names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str
|
pattern to limit search to |
None
|
Returns:
Name | Type | Description |
---|---|---|
matches_stream |
Iterable[str]
|
a stream of matching name |
Source code in alsek/storage/backends/__init__.py
287 288 289 290 291 292 293 294 295 296 297 298 |
|
set(name, value, nx=False, ttl=None)
abstractmethod
Set name
to value
in the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
name of the item |
required |
value
|
Any
|
value to set for |
required |
nx
|
bool
|
if |
False
|
ttl
|
int
|
time to live for the entry in milliseconds |
None
|
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
KeyError
|
if |
Source code in alsek/storage/backends/__init__.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
short_name(name)
Get an item's short name, without the namespace in which it exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
the full name of an item |
required |
Returns:
Name | Type | Description |
---|---|---|
short_name |
str
|
|
Notes
- If
name
is already the short name, this method will collapse to a no-op.
Source code in alsek/storage/backends/__init__.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
sub(channel)
Subscribe to a channel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channel
|
str
|
channel name |
required |
Returns:
Type | Description |
---|---|
Iterable[str | dict[str, Any]]
|
Iterable[str | dict[str, Any]] |
Source code in alsek/storage/backends/__init__.py
275 276 277 278 279 280 281 282 283 284 285 |
|
LazyClient
Lazy client.
Wrapper for lazy client initialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_func
|
callable
|
a callable which returns a backend client. |
required |
Source code in alsek/storage/backends/__init__.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
get()
Execute client_func
.
Returns:
Name | Type | Description |
---|---|---|
client |
Any
|
a backend client |
Source code in alsek/storage/backends/__init__.py
39 40 41 42 43 44 45 46 |
|
redis
Redis
asyncio
Asynchronous Redis Backend
RedisAsyncBackend
Bases: AsyncBackend
Asynchronous Redis Backend.
This backend is powered by Redis and provides asynchronous support for Redis operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conn
|
Optional[Union[str, AsyncRedis, LazyClient]]
|
A connection URL,
an |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to the base class initializer. |
{}
|
Source code in alsek/storage/backends/redis/asyncio.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
conn
property
Asynchronous Redis connection.
Returns:
Name | Type | Description |
---|---|---|
AsyncRedis |
Redis
|
The asynchronous Redis client instance. |
delete(name, missing_ok=False)
async
Delete a key from the Redis backend asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the key to delete. |
required |
missing_ok
|
bool
|
If |
False
|
Raises:
Type | Description |
---|---|
KeyError
|
If the key does not exist and |
Source code in alsek/storage/backends/redis/asyncio.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
exists(name)
async
Check if a key exists in the Redis backend asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the key to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
Source code in alsek/storage/backends/redis/asyncio.py
119 120 121 122 123 124 125 126 127 128 129 |
|
get(name, default=Empty)
async
Get the value of a key from the Redis backend asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the key. |
required |
default
|
Optional[Union[Any, Type[Empty]]]
|
Default value if the key does not exist. |
Empty
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The value of the key. |
Raises:
Type | Description |
---|---|
KeyError
|
If the key does not exist and no default is provided. |
Source code in alsek/storage/backends/redis/asyncio.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
priority_add(key, unique_id, priority)
async
Add an item to a priority-sorted set asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
unique_id
|
str
|
The item's unique identifier. |
required |
priority
|
int | float
|
The numeric priority score. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/storage/backends/redis/asyncio.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
priority_get(key)
async
Peek the highest-priority item in a sorted set asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
Returns:
Name | Type | Description |
---|---|---|
item |
(str, optional)
|
The ID of the highest-priority item, or None if empty. |
Source code in alsek/storage/backends/redis/asyncio.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
priority_iter(key)
async
Iterate over all items in a priority-sorted set asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
Yields:
Name | Type | Description |
---|---|---|
item |
str
|
Member of the sorted set, in priority order. |
Source code in alsek/storage/backends/redis/asyncio.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
|
priority_remove(key, unique_id)
async
Remove an item from a priority-sorted set asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
unique_id
|
str
|
The ID of the item to remove. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/storage/backends/redis/asyncio.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
pub(channel, value)
async
Publish a message to a Redis channel asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channel
|
str
|
The name of the channel. |
required |
value
|
Any
|
The message to publish. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/storage/backends/redis/asyncio.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
scan(pattern=None)
async
Asynchronously scan the Redis backend for keys matching a pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
Optional[str]
|
The pattern to match keys against. Defaults to '*'. |
None
|
Yields:
Name | Type | Description |
---|---|---|
str |
AsyncIterable[str]
|
The names of matching keys without the namespace prefix. |
Source code in alsek/storage/backends/redis/asyncio.py
234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
set(name, value, nx=False, ttl=None)
async
Set a value for a key in the Redis backend asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the key. |
required |
value
|
Any
|
The value to set. |
required |
nx
|
bool
|
If |
False
|
ttl
|
Optional[int]
|
Time to live for the key in milliseconds. |
None
|
Raises:
Type | Description |
---|---|
KeyError
|
If |
Source code in alsek/storage/backends/redis/asyncio.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
sub(channel)
async
Subscribe to a Redis channel and asynchronously yield messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channel
|
str
|
The name of the channel to subscribe to. |
required |
Yields:
Type | Description |
---|---|
AsyncIterable[dict[str, Any]]
|
dict[str, Any]: A dictionary representing the message data. |
Source code in alsek/storage/backends/redis/asyncio.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
|
standard
Redis Backend
RedisBackend
Bases: Backend
Redis Backend.
Backend powered by Redis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conn
|
(str, Redis, LazyClient)
|
a connection url, |
None
|
namespace
|
str
|
prefix to use when inserting names in the backend |
DEFAULT_NAMESPACE
|
serializer
|
Serializer
|
tool for encoding and decoding values written into the backend. |
JsonSerializer()
|
Warning
- If
conn
is aRedis()
object,decode_responses
is expected to be set toTrue
.
Source code in alsek/storage/backends/redis/standard.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
|
conn
property
Connection to the backend.
delete(name, missing_ok=False)
Delete a name
from the Redis backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
name of the item |
required |
missing_ok
|
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
KeyError
|
if |
Source code in alsek/storage/backends/redis/standard.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
exists(name)
Check if name
exists in the Redis backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
name of the item |
required |
Returns:
Type | Description |
---|---|
bool
|
bool |
Source code in alsek/storage/backends/redis/standard.py
112 113 114 115 116 117 118 119 120 121 122 |
|
get(name, default=None)
Get name
from the Redis backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
name of the item |
required |
default
|
(Any, Type[Empty])
|
default value for |
None
|
Returns:
Type | Description |
---|---|
Any
|
Any |
Source code in alsek/storage/backends/redis/standard.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
priority_add(key, unique_id, priority)
Add an item to a priority-sorted set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
unique_id
|
str
|
The item's (Message's) unique identifier |
required |
priority
|
float
|
The numeric priority score (decide if lower or higher means higher priority). |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/storage/backends/redis/standard.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
priority_get(key)
Get (peek) the highest-priority item without removing it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
Returns:
Name | Type | Description |
---|---|---|
item |
(str, optional)
|
The member with the highest priority, or None if empty. |
Source code in alsek/storage/backends/redis/standard.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
priority_iter(key)
Iterate over the items in a priority-sorted set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
Returns:
Name | Type | Description |
---|---|---|
priority |
Iterable[str]
|
An iterable of members in the sorted set, sorted by priority. |
Source code in alsek/storage/backends/redis/standard.py
224 225 226 227 228 229 230 231 232 233 234 |
|
priority_remove(key, unique_id)
Remove an item from a priority-sorted set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The name of the sorted set. |
required |
unique_id
|
str
|
The item's (Message's) unique identifier |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/storage/backends/redis/standard.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
scan(pattern=None)
Scan the backend for matching names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str
|
pattern to match against |
None
|
Returns:
Name | Type | Description |
---|---|---|
names_stream |
Iterable[str]
|
a stream of matching name |
Source code in alsek/storage/backends/redis/standard.py
269 270 271 272 273 274 275 276 277 278 279 280 |
|
set(name, value, nx=False, ttl=None)
Set name
to value
in the Redis backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
name of the item |
required |
value
|
Any
|
value to set for |
required |
nx
|
bool
|
if |
False
|
ttl
|
int
|
time to live for the entry in milliseconds |
None
|
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
KeyError
|
if |
Source code in alsek/storage/backends/redis/standard.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
result
Result Storage
ResultStore
Alsek Result Store.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
backend
|
Backend
|
backend for data storage |
required |
Warning
- In order for a result to be stored, it must be
serializable by the
serializer
used bybackend
.
Source code in alsek/storage/result.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
delete(message, descendants=True, missing_ok=False)
Delete any data for message
from the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message. |
required |
descendants
|
bool
|
if |
True
|
missing_ok
|
bool
|
if |
False
|
Returns:
Name | Type | Description |
---|---|---|
count |
int
|
number of results deleted |
Source code in alsek/storage/result.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
exists(message, descendants=False)
Whether data for message
exists in the store.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
descendants
|
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
bool
|
bool |
Source code in alsek/storage/result.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
get(message, timeout=0, keep=False, with_metadata=False, descendants=False)
Get the result for message
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message. |
required |
timeout
|
int
|
amount of time (in milliseconds) to wait for the result to become available |
0
|
keep
|
bool
|
whether to keep the result after fetching it.
Defaults to |
False
|
with_metadata
|
bool
|
if |
False
|
descendants
|
bool
|
if |
False
|
Returns:
Name | Type | Description |
---|---|---|
result |
(Any, list[Any])
|
the stored result. If |
Raises:
Type | Description |
---|---|
KeyError
|
if results are not available for |
TimeoutError
|
if results are not available for |
Notes
- The order of results when
descendants=True
is determined by the time at which the data was written to the backend.
Warning
- If a message has a projenitor, the
projenitor_uuid
field in themessage
must be set.
Examples:
>>> from alsek import Message
>>> from alsek.storage.backends.redis.standard import RedisBackend
>>> from alsek.storage.result import ResultStore
>>> backend = RedisBackend()
>>> result_store = ResultStore(backend)
>>> result_store.get(Message(uuid="..."))
Source code in alsek/storage/result.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
get_storage_name(message)
staticmethod
Get the name for message
in the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
name |
str
|
message-specific name |
Source code in alsek/storage/result.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
set(message, result, nx=True)
Store a result
for message
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message. |
required |
result
|
Any
|
the result to persist |
required |
nx
|
bool
|
if |
True
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/storage/result.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
serialization
Serialization
BinarySerializer
Bases: Serializer
Binary serialization.
Source code in alsek/storage/serialization.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
forward(obj)
staticmethod
Encode an object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
an object to encode |
required |
Returns:
Name | Type | Description |
---|---|---|
encoded |
Any
|
base64 encoded |
Source code in alsek/storage/serialization.py
87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
reverse(obj)
staticmethod
Decode an object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
an object to decode |
required |
Returns:
Name | Type | Description |
---|---|---|
decoded |
Any
|
|
Source code in alsek/storage/serialization.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
JsonSerializer
Bases: Serializer
JSON serialization.
Source code in alsek/storage/serialization.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
forward(obj)
staticmethod
Encode an object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
an object to encode |
required |
Returns:
Name | Type | Description |
---|---|---|
encoded |
Any
|
JSON encoded object |
Source code in alsek/storage/serialization.py
55 56 57 58 59 60 61 62 63 64 65 66 |
|
reverse(obj)
staticmethod
Decode an object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
an object to decode |
required |
Returns:
Name | Type | Description |
---|---|---|
decoded |
Any
|
JSON decoded object |
Source code in alsek/storage/serialization.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
Serializer
Bases: ABC
Base Serializer Class.
Source code in alsek/storage/serialization.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
forward(obj)
abstractmethod
staticmethod
Encode an object for backend serialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
an object to encode |
required |
Returns:
Name | Type | Description |
---|---|---|
encoded |
Any
|
encoded object |
Source code in alsek/storage/serialization.py
23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
reverse(obj)
abstractmethod
staticmethod
Decode an object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
an object to decode |
required |
Returns:
Name | Type | Description |
---|---|---|
decoded |
Any
|
decoded object |
Source code in alsek/storage/serialization.py
37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
tools
Tools
iteration
Result Iteration
ResultPool
Tooling for iterating over task results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_store
|
ResultStore
|
store where task results are persisted |
required |
Examples:
>>> from alsek.storage.result import ResultStore
>>> from alsek.tools.iteration import ResultPool
...
>>> result_store = ResultStore(...)
>>> pool = ResultPool(result_store)
...
>>> messages = [...]
>>> for uuid, result in pool.istream(*messages):
... pass
Source code in alsek/tools/iteration.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
istream(*messages, wait=5 * 1000, descendants=False, **kwargs)
Stream the results of one or more messages. Results are yielded in the order in which they become available. (This may differ from the order in which messages are provided.)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*messages
|
Message
|
one or more messages to iterate over |
()
|
wait
|
int
|
time to wait (in milliseconds) between checks for available results |
5 * 1000
|
descendants
|
bool
|
if |
False
|
**kwargs
|
Keyword Args
|
keyword arguments to pass to
|
{}
|
results (iterable): an iterable of results of the form
(Message, result)
.
Warning
- By default,
result_store
does not keep messages once they have been collected. As a result, providing messages for which the corresponding results have already been collected (and deleted) will cause this method to loop indefinitely. In order to loop over messages multiple times setkeep=True
.
Source code in alsek/tools/iteration.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
stream(*messages, wait=5 * 1000, descendants=False, **kwargs)
Stream the results of one or more messages. The order of the
results are guaranteed to match the order of messages
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*messages
|
Message
|
one or more messages to iterate over |
()
|
wait
|
int
|
time to wait (in milliseconds) between checks for available results |
5 * 1000
|
descendants
|
bool
|
if |
False
|
**kwargs
|
Keyword Args
|
keyword arguments to pass to
|
{}
|
Returns:
Name | Type | Description |
---|---|---|
results |
iterable
|
an iterable of results of the form
|
Warning
- By default,
result_store
does not keep messages once they have been collected. As a result, providing messages for which the corresponding results have already been collected (and deleted) will cause this method to loop indefinitely. In order to loop over messages multiple times setkeep=True
.
Source code in alsek/tools/iteration.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
types
Types
Empty
Empty sentinel.
Source code in alsek/types.py
13 14 |
|
utils
aggregation
Aggregation Utils
gather_init_params(obj, ignore=None)
Extract the parameters passed to an object's __init__()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
object
|
|
required |
ignore
|
tuple
|
parameters in |
None
|
Returns:
Name | Type | Description |
---|---|---|
params |
dict
|
parameters from |
Source code in alsek/utils/aggregation.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
checks
Check Utils
has_duplicates(itera)
Determine if itera
contains duplicates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
itera
|
Collection
|
a sized iterable |
required |
Returns:
Type | Description |
---|---|
bool
|
bool |
Source code in alsek/utils/checks.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
decorators
Decorators
helpers
Helpers
logging
Logging
get_logger()
Get the Alsek logger.
Returns:
Name | Type | Description |
---|---|---|
logger |
Logger
|
Alsek logger |
Source code in alsek/utils/logging.py
19 20 21 22 23 24 25 26 |
|
magic_logger(before=lambda: None, after=lambda: None)
Logging decorator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
before
|
callable
|
function to log a message before function execution. This callable will be passed only those parameters which it shares with the deocrated function. |
lambda: None
|
after
|
callable
|
function to log a message after function
execution. This callable will be passed:
* |
lambda: None
|
Returns:
Name | Type | Description |
---|---|---|
wrapper |
callable
|
wrapped |
Examples:
>>> import logging
>>> from alsek.utils.logging import magic_logger
>>> log = logging.getLogger(__name__)
>>> @magic_logger(
>>> before=lambda a: log.debug(a),
>>> after=lambda input_, output: log.info(output)
>>> )
>>> def add99(a: int) -> int:
>>> return a + 99
Source code in alsek/utils/logging.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
setup_logging(level)
Setup Alsek-style logging.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level
|
int
|
logging level to use |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/utils/logging.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
namespacing
Namespacing
get_dlq_message_name(message)
Get the name for message
in the backend's dead letter queue (DLQ).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
dlq_name |
str
|
message-specific name in the DLQ |
Source code in alsek/utils/namespacing.py
115 116 117 118 119 120 121 122 123 124 125 |
|
get_message_name(message)
Get the name for message
in the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
name |
str
|
message-specific name |
Source code in alsek/utils/namespacing.py
61 62 63 64 65 66 67 68 69 70 71 72 |
|
get_message_signature(message)
Get the signature for message
in the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
signature |
str
|
message-specific signature. |
Source code in alsek/utils/namespacing.py
75 76 77 78 79 80 81 82 83 84 85 |
|
get_messages_namespace(message)
Get the namespace for a message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
namespace |
str
|
the namespace for the message |
Source code in alsek/utils/namespacing.py
47 48 49 50 51 52 53 54 55 56 57 58 |
|
get_priority_namespace(subnamespace)
Get the namespace for a message's priority information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subnamespace
|
str
|
the namespace for the message |
required |
Returns:
Name | Type | Description |
---|---|---|
priority_namespace |
str
|
the namespace for priority information |
Source code in alsek/utils/namespacing.py
88 89 90 91 92 93 94 95 96 97 98 |
|
get_priority_namespace_from_message(message)
Get the namespace for message's priority information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
an Alsek message |
required |
Returns:
Name | Type | Description |
---|---|---|
namespace |
str
|
the fully-qualified priority queue name |
Source code in alsek/utils/namespacing.py
101 102 103 104 105 106 107 108 109 110 111 112 |
|
get_subnamespace(queue=None, task_name=None)
Get the subnamespace for a given queue
and (optionally) task_name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
queue
|
str
|
the name of the queue |
None
|
task_name
|
str
|
name of the task |
None
|
Returns:
Name | Type | Description |
---|---|---|
subnamespace |
str
|
queue-specific namespace |
Raises:
Type | Description |
---|---|
ValueError
|
if |
Source code in alsek/utils/namespacing.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
parsing
Parsing
ExceptionDetails
Bases: NamedTuple
Source code in alsek/utils/parsing.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
as_dict()
Convert the NamedTuple to a dictionary
Returns:
Type | Description |
---|---|
dict[str, str]
|
dict |
Source code in alsek/utils/parsing.py
44 45 46 47 48 49 50 51 |
|
as_exception(strict=True)
Return parsed exception information as a Python exception.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict
|
bool
|
if |
True
|
Returns:
Type | Description |
---|---|
BaseException
|
BaseException |
Source code in alsek/utils/parsing.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
get_exception_name(exception)
Get the name of an exception as a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exception
|
(BaseException, Type[BaseException])
|
Exception class |
required |
Returns:
Name | Type | Description |
---|---|---|
name |
str
|
the exception name |
Source code in alsek/utils/parsing.py
24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
parse_exception(error)
Extracts the exception type, exception message, and exception traceback from an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
error
|
BaseException
|
The exception to extract details from. |
required |
Returns:
Name | Type | Description |
---|---|---|
details |
ExceptionDetails
|
A named tuple containing the exception information |
Source code in alsek/utils/parsing.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
printing
Printing Utils
auto_repr(obj, new_line_threshold=5, **params)
Autogenerate a class repr string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
object
|
an object to generate a repr for |
required |
new_line_threshold
|
int
|
number of |
5
|
**params
|
Keyword Args
|
parameters to include in the repr string |
{}
|
Returns:
Name | Type | Description |
---|---|---|
repr |
str
|
repr string |
Source code in alsek/utils/printing.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
scanning
Helpers
collect_tasks(module)
Recursively collect all tasks in name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module
|
(str, ModuleType)
|
name of a module |
required |
Returns:
Name | Type | Description |
---|---|---|
module |
tuple[Task, ...]
|
collected tasks |
Raises:
Type | Description |
---|---|
NoTasksFoundError
|
if no tasks can be found |
Source code in alsek/utils/scanning.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
sorting
Sorting
dict_sort(dictionary, key=None)
Sort a dictionary by key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary
|
dict[Any, Any]
|
|
required |
key
|
callable
|
a callable which consumes a key and returns an object which supports the less than comparison operator. |
None
|
Returns:
Name | Type | Description |
---|---|---|
sorted_dictionary |
dict
|
|
Source code in alsek/utils/sorting.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
string
String Utils
smart_join(items, limit=None, delimiter=', ')
Joins a list of strings with a delimiter, limiting the number of items to display and optionally appending a continuation indicator or providing a grammatically correct conjunction for the last two items.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
items
|
list[str]
|
A list of strings to be joined. |
required |
limit
|
Optional[int]
|
The maximum number of items to include in the joined string. If None, join all items without limiting. |
None
|
delimiter
|
str
|
The string used to separate the items in the joined output. |
', '
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string containing the joined items, formatted according to the |
str
|
specified delimiter and limits. |
Raises:
Type | Description |
---|---|
ValueError
|
If the |
Source code in alsek/utils/string.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
system
System Utils
StopSignalListener
Tool for listing for stop signals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stop_signals
|
tuple[int, ...]
|
one or more stop signals to listen for. |
DEFAULT_STOP_SIGNALS
|
exit_override
|
bool
|
trigger an immediate and non-graceful shutdown of the current process if two or more SIGTERM or SIGINT signals are received. |
True
|
Source code in alsek/utils/system.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
received
property
Whether a stop signal has been received.
wait(timeout)
Wait for a stop signal to be received.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
int
|
amount of time (in milliseconds) to wait |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in alsek/utils/system.py
86 87 88 89 90 91 92 93 94 95 96 97 |
|
smart_cpu_count()
Count the number of CPUs, with one reserved for the main process.
Returns:
Name | Type | Description |
---|---|---|
count |
int
|
number of cpus |
Source code in alsek/utils/system.py
105 106 107 108 109 110 111 112 113 |
|
thread_raise(ident, exception)
Raise an exception in a thread asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ident
|
int
|
ident of the thread |
required |
exception
|
Type[BaseException]
|
type of exception to raise in the thread |
required |
References
- https://docs.python.org/3/c-api/init.html#c.PyThreadState_SetAsyncExc
Warning
- Intended for use with CPython only
Source code in alsek/utils/system.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
temporal
Temporal Utils
from_timestamp_ms(timestamp)
Construct datetime object from UTC timestamp in milliseconds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timestamp
|
int
|
UTC time in milliseconds |
required |
Returns:
Type | Description |
---|---|
datetime
|
datetime |
Source code in alsek/utils/temporal.py
21 22 23 24 25 26 27 28 29 30 31 |
|
time_ms()
Get the current time since the Epoch in milliseconds.
Returns:
Name | Type | Description |
---|---|---|
time |
int
|
current time in milliseconds |
Source code in alsek/utils/temporal.py
34 35 36 37 38 39 40 41 |
|
utcnow_timestamp_ms()
UTC timestamp in milliseconds.
Returns:
Name | Type | Description |
---|---|---|
timestamp |
int
|
UTC time in milliseconds |
Source code in alsek/utils/temporal.py
11 12 13 14 15 16 17 18 |
|
waiting
Waiting
waiter(condition, sleep_interval=1 * 1000, timeout=None, timeout_msg=None)
Wait for condition
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
condition
|
callable
|
condition to wait for |
required |
sleep_interval
|
int
|
time (in milliseconds) to sleep
between checks of |
1 * 1000
|
timeout
|
int
|
maximum amount of time (in milliseconds)
this function can wait for |
None
|
timeout_msg
|
str
|
message to display in the event of a timeout |
None
|
Returns:
Type | Description |
---|---|
bool
|
bool |
Source code in alsek/utils/waiting.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|