Sync¶
The problem with fire-and-forget¶
All MQSC START and STOP commands are fire-and-forget — they return
immediately without waiting for the object to reach its target state.
In practice, tooling that provisions infrastructure needs to wait until
a channel is RUNNING or a listener is STOPPED before proceeding to
the next step. Writing polling loops by hand is error-prone and
clutters business logic with retry mechanics.
The sync pattern¶
The *_sync and restart_* methods wrap the fire-and-forget commands
with a polling loop that issues DISPLAY *STATUS until the object
reaches a stable state or the timeout expires.
SyncOperation¶
An enum indicating the operation that was performed:
class SyncOperation(Enum):
STARTED = "started" # Object confirmed running
STOPPED = "stopped" # Object confirmed stopped
RESTARTED = "restarted" # Stop-then-start completed
SyncConfig¶
Configuration controlling the polling behaviour:
@dataclass
class SyncConfig:
timeout: float = 30.0 # Max seconds before raising
poll_interval: float = 1.0 # Seconds between polls
| Attribute | Type | Description |
|---|---|---|
timeout |
float |
Maximum seconds to wait before raising MQRESTTimeoutError |
poll_interval |
float |
Seconds between DISPLAY *STATUS polls |
SyncResult¶
Contains the outcome of a sync operation:
class SyncResult(NamedTuple):
operation: SyncOperation # What happened
polls: int # Number of status polls issued
elapsed: float # Wall-clock seconds from command to confirmation
| Attribute | Type | Description |
|---|---|---|
operation |
SyncOperation |
What happened: STARTED, STOPPED, or RESTARTED |
polls |
int |
Number of status polls issued |
elapsed |
float |
Wall-clock seconds from command to confirmation |
Method signature pattern¶
All 9 sync methods follow the same signature pattern:
The config parameter is keyword-only for API clarity.
Basic usage¶
from pymqrest import SyncConfig, SyncOperation
# Start a channel and wait until it is RUNNING
result = session.start_channel_sync("TO.PARTNER")
assert result.operation is SyncOperation.STARTED
print(f"Channel running after {result.polls} poll(s), {result.elapsed:.1f}s")
# Stop a listener and wait until it is STOPPED
result = session.stop_listener_sync("TCP.LISTENER")
assert result.operation is SyncOperation.STOPPED
Custom timeout and poll interval¶
Pass a SyncConfig to override the defaults:
from pymqrest import SyncConfig
# Aggressive polling for fast local development
fast = SyncConfig(timeout_seconds=10.0, poll_interval_seconds=0.25)
result = session.start_service_sync("MY.SVC", config=fast)
# Patient polling for remote queue managers
patient = SyncConfig(timeout_seconds=120.0, poll_interval_seconds=5.0)
result = session.start_channel_sync("REMOTE.CHL", config=patient)
Restart convenience¶
The restart_* methods perform a synchronous stop followed by a
synchronous start. Each phase gets the full timeout independently —
worst case is 2x the configured timeout.
The returned SyncResult reports total polls and total elapsed
time across both phases:
result = session.restart_channel("TO.PARTNER")
assert result.operation is SyncOperation.RESTARTED
print(f"Restarted in {result.elapsed:.1f}s ({result.polls} total polls)")
Timeout handling¶
When the timeout expires, MQRESTTimeoutError is raised with
diagnostic attributes:
from pymqrest import MQRESTTimeoutError, SyncConfig
try:
session.start_channel_sync(
"BROKEN.CHL",
config=SyncConfig(timeout_seconds=15.0),
)
except MQRESTTimeoutError as err:
print(f"Object: {err.name}") # "BROKEN.CHL"
print(f"Operation: {err.operation}") # "start"
print(f"Elapsed: {err.elapsed:.1f}s") # 15.0
MQRESTTimeoutError inherits from MQRESTError, so existing
except MQRESTError handlers will catch it.
Available methods¶
| Method | Operation | START/STOP qualifier | Status qualifier |
|---|---|---|---|
start_channel_sync() |
Start | CHANNEL |
CHSTATUS |
stop_channel_sync() |
Stop | CHANNEL |
CHSTATUS |
restart_channel() |
Restart | CHANNEL |
CHSTATUS |
start_listener_sync() |
Start | LISTENER |
LSSTATUS |
stop_listener_sync() |
Stop | LISTENER |
LSSTATUS |
restart_listener() |
Restart | LISTENER |
LSSTATUS |
start_service_sync() |
Start | SERVICE |
SVSTATUS |
stop_service_sync() |
Stop | SERVICE |
SVSTATUS |
restart_service() |
Restart | SERVICE |
SVSTATUS |
Status detection¶
The polling loop checks the STATUS attribute in the DISPLAY *STATUS
response. The target values are:
- Start:
RUNNING - Stop:
STOPPED
Channel stop edge case¶
When a channel stops, its CHSTATUS record may disappear entirely
(the DISPLAY CHSTATUS response returns no rows). The channel sync
methods treat an empty status result as successfully stopped. Listener
and service status records are always present, so empty results are not
treated as stopped for those object types.
Attribute mapping¶
The sync methods call _mqsc_command internally, so they participate
in the same mapping pipeline as all other
command methods. The status key is checked using both the mapped
snake_case name and the raw MQSC name, so polling works correctly
regardless of whether mapping is enabled or disabled.
Provisioning example¶
The sync methods pair naturally with the ensure methods for end-to-end provisioning:
from pymqrest import SyncConfig
config = SyncConfig(timeout_seconds=60.0)
# Ensure listeners exist for application and admin traffic
session.ensure_listener("APP.LISTENER", request_parameters={
"transport_type": "TCP",
"port": 1415,
"start_mode": "MQSVC_CONTROL_Q_MGR",
})
session.ensure_listener("ADMIN.LISTENER", request_parameters={
"transport_type": "TCP",
"port": 1416,
"start_mode": "MQSVC_CONTROL_Q_MGR",
})
# Start them synchronously
session.start_listener_sync("APP.LISTENER", config=config)
session.start_listener_sync("ADMIN.LISTENER", config=config)
print("Listeners ready")
Rolling restart example¶
Restart all listeners with error handling — useful when a queue manager serves multiple TCP ports for different client populations:
from pymqrest import MQRESTTimeoutError, SyncConfig
listeners = ["APP.LISTENER", "ADMIN.LISTENER", "PARTNER.LISTENER"]
config = SyncConfig(timeout_seconds=30.0, poll_interval_seconds=2.0)
for name in listeners:
try:
result = session.restart_listener(name, config=config)
print(f"{name}: restarted in {result.elapsed:.1f}s")
except MQRESTTimeoutError as err:
print(f"{name}: timed out after {err.elapsed:.1f}s")
API reference¶
Configuration for synchronous polling operations.
Attributes:
| Name | Type | Description |
|---|---|---|
timeout_seconds |
float
|
Maximum wall-clock seconds to wait for the object to reach the target state. |
poll_interval_seconds |
float
|
Seconds to sleep between status polls. |
Source code in src/pymqrest/sync.py
timeout_seconds = 30.0
class-attribute
instance-attribute
¶
poll_interval_seconds = 1.0
class-attribute
instance-attribute
¶
__init__(timeout_seconds=30.0, poll_interval_seconds=1.0)
¶
__post_init__()
¶
Validate that both fields are positive.
Source code in src/pymqrest/sync.py
Bases: Enum
Operation performed by a synchronous wrapper.
Attributes:
| Name | Type | Description |
|---|---|---|
STARTED |
The object was started and confirmed running. |
|
STOPPED |
The object was stopped and confirmed stopped. |
|
RESTARTED |
The object was stopped then started. |
Source code in src/pymqrest/sync.py
Result of a synchronous start/stop/restart operation.
Attributes:
| Name | Type | Description |
|---|---|---|
operation |
SyncOperation
|
The :class: |
polls |
int
|
Total number of status polls issued. |
elapsed_seconds |
float
|
Total wall-clock seconds from command to target state confirmation. |
Source code in src/pymqrest/sync.py
Mixin providing synchronous start/stop/restart wrappers.
Each *_sync method issues the MQSC command then polls the
corresponding DISPLAY *STATUS until the object reaches a
stable state or the timeout expires. restart_* methods
perform a synchronous stop followed by a synchronous start.
Source code in src/pymqrest/sync.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 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 | |
_mqsc_command(*, command, mqsc_qualifier, name, request_parameters, response_parameters, where=None)
¶
Source code in src/pymqrest/sync.py
start_channel_sync(name, *, config=None)
¶
Start a channel and wait until it is running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Channel name. |
required |
config
|
SyncConfig | None
|
Optional polling configuration. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SyncResult
|
class: |
Raises:
| Type | Description |
|---|---|
MQRESTTimeoutError
|
If the channel does not reach RUNNING within the timeout. |
Source code in src/pymqrest/sync.py
stop_channel_sync(name, *, config=None)
¶
Stop a channel and wait until it is stopped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Channel name. |
required |
config
|
SyncConfig | None
|
Optional polling configuration. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SyncResult
|
class: |
Raises:
| Type | Description |
|---|---|
MQRESTTimeoutError
|
If the channel does not reach STOPPED within the timeout. |
Source code in src/pymqrest/sync.py
restart_channel(name, *, config=None)
¶
Stop then start a channel, waiting for each phase.
Each phase gets the full timeout independently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Channel name. |
required |
config
|
SyncConfig | None
|
Optional polling configuration. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SyncResult
|
class: |
SyncResult
|
across both phases. |
Raises:
| Type | Description |
|---|---|
MQRESTTimeoutError
|
If either phase exceeds the timeout. |
Source code in src/pymqrest/sync.py
start_listener_sync(name, *, config=None)
¶
Start a listener and wait until it is running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Listener name. |
required |
config
|
SyncConfig | None
|
Optional polling configuration. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SyncResult
|
class: |
Raises:
| Type | Description |
|---|---|
MQRESTTimeoutError
|
If the listener does not reach RUNNING within the timeout. |
Source code in src/pymqrest/sync.py
stop_listener_sync(name, *, config=None)
¶
Stop a listener and wait until it is stopped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Listener name. |
required |
config
|
SyncConfig | None
|
Optional polling configuration. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SyncResult
|
class: |
Raises:
| Type | Description |
|---|---|
MQRESTTimeoutError
|
If the listener does not reach STOPPED within the timeout. |
Source code in src/pymqrest/sync.py
restart_listener(name, *, config=None)
¶
Stop then start a listener, waiting for each phase.
Each phase gets the full timeout independently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Listener name. |
required |
config
|
SyncConfig | None
|
Optional polling configuration. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SyncResult
|
class: |
SyncResult
|
across both phases. |
Raises:
| Type | Description |
|---|---|
MQRESTTimeoutError
|
If either phase exceeds the timeout. |
Source code in src/pymqrest/sync.py
start_service_sync(name, *, config=None)
¶
Start a service and wait until it is running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Service name. |
required |
config
|
SyncConfig | None
|
Optional polling configuration. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SyncResult
|
class: |
Raises:
| Type | Description |
|---|---|
MQRESTTimeoutError
|
If the service does not reach RUNNING within the timeout. |
Source code in src/pymqrest/sync.py
stop_service_sync(name, *, config=None)
¶
Stop a service and wait until it is stopped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Service name. |
required |
config
|
SyncConfig | None
|
Optional polling configuration. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SyncResult
|
class: |
Raises:
| Type | Description |
|---|---|
MQRESTTimeoutError
|
If the service does not reach STOPPED within the timeout. |
Source code in src/pymqrest/sync.py
restart_service(name, *, config=None)
¶
Stop then start a service, waiting for each phase.
Each phase gets the full timeout independently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Service name. |
required |
config
|
SyncConfig | None
|
Optional polling configuration. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SyncResult
|
class: |
SyncResult
|
across both phases. |
Raises:
| Type | Description |
|---|---|
MQRESTTimeoutError
|
If either phase exceeds the timeout. |
Source code in src/pymqrest/sync.py
_start_and_poll(name, object_config, config)
¶
Issue START then poll until the object is RUNNING.
Source code in src/pymqrest/sync.py
_stop_and_poll(name, object_config, config)
¶
Issue STOP then poll until the object is STOPPED.
Source code in src/pymqrest/sync.py
_restart(name, object_config, config)
¶
Stop-sync then start-sync, returning combined totals.