|
1 | | -## Loopster |
2 | | -Сервисная библиотека, которая обеспечивает выполнение сервисных процессов, следит за их зависаниями и при необходимости осуществляет перезапуск. |
| 1 | +# Loopster |
| 2 | + |
| 3 | +Loopster is a service library that provides mechanisms for executing and managing services, including monitoring their status and automatically restarting them when necessary. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install Loopster, you can use pip: |
| 8 | + |
| 9 | +``` |
| 10 | +pip install loopster |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Basic Service Example |
| 16 | + |
| 17 | +Here's an example of how to create and run a basic service using Loopster: |
| 18 | + |
| 19 | +```python |
| 20 | +from loopster.hubs import base |
| 21 | +from loopster.hubs.controllers import panic |
| 22 | +from loopster.hubs.drivers import process |
| 23 | +from loopster.services import softirq |
| 24 | + |
| 25 | +class BasicService(softirq.SoftIrqService): |
| 26 | + def _step(self): |
| 27 | + print("Hello, world!") |
| 28 | + |
| 29 | +driver = process.ProcessDriver() |
| 30 | +controller = panic.PanicController() |
| 31 | +hub = base.BaseHub(driver=driver, controller=controller) |
| 32 | + |
| 33 | +hub.add_service(svc_class=BasicService, svc_kwargs={}) |
| 34 | +hub.serve() |
| 35 | +``` |
| 36 | + |
| 37 | +### Nested Service Example with MySQLWrapper |
| 38 | + |
| 39 | +Here's an example of how to use a nested service with the `mysqlwrapper` module: |
| 40 | + |
| 41 | +```python |
| 42 | +from restalchemy.storage.sql import engines |
| 43 | +from rooster.hubs.controllers import force_state |
| 44 | +from loopster.hubs.drivers import process |
| 45 | +from loopster.services import mysql |
| 46 | +from loopster.services import softirq |
| 47 | + |
| 48 | +class BasicService(softirq.SoftIrqService): |
| 49 | + def _step(self): |
| 50 | + print("Hello, world!") |
| 51 | + |
| 52 | +hub = process.ProcessHub( |
| 53 | + controller=force_state.AlwaysForceTargetStateController()) |
| 54 | + |
| 55 | +hub.add_service( |
| 56 | + svc_class=mysql.MySQLStorageWrapper, |
| 57 | + svc_kwargs=dict( |
| 58 | + nested_class=BasicService, |
| 59 | + nested_kwargs=dict( |
| 60 | + step_period=STEP_PERIOD, |
| 61 | + ), |
| 62 | + mysql_connection_url=engines.DBConnectionUrl(CONNECTION_URL) |
| 63 | + ) |
| 64 | +) |
| 65 | +``` |
| 66 | + |
| 67 | +## Public Interface |
| 68 | + |
| 69 | +### Services |
| 70 | + |
| 71 | +Loopster provides several built-in services, including: |
| 72 | + |
| 73 | +- `SoftIrqService`: A service with a watchdog that runs in an infinite loop. |
| 74 | +- `BjoernService`: A special server for Bjoern, which implements multiprocessing by itself. |
| 75 | + |
| 76 | +### Hubs |
| 77 | +The `ProcessHub` class is used to manage multiple services using one strategy provided by the driver. It inherits from `SoftIrqService`, so it's a service too! |
| 78 | + |
| 79 | +Hub provides a simple way to start and stop multiple services at once, as well as monitor their status. |
| 80 | + |
| 81 | +#### Features |
| 82 | + |
| 83 | +- **Signal Handling**: The `ProcessHub` can handle signals such as SIGHUP and SIGUSR1, allowing for graceful shutdowns and other terminal actions. |
| 84 | +- **Multiprocessing Support**: By using the `multiprocessing` module, `ProcessHub` ensures that services run in separate processes, providing better isolation and resource management. |
| 85 | +- **State Management**: The hub manages the state of each service, ensuring they are running as expected and automatically restarting them when necessary. |
| 86 | + |
| 87 | +### Controllers |
| 88 | + |
| 89 | +Loopster includes different controllers that manage the state of services: |
| 90 | + |
| 91 | +- `AlwaysForceTargetStateController`: A simple controller that always sets states in a loop. |
| 92 | +- `PanicController`: A controller that stops managing on any problem. |
| 93 | + |
| 94 | +### Drivers |
| 95 | + |
| 96 | +The `ProcessDriver` class is used to serve services as child processes. It provides methods for adding, removing, and setting the state of services. |
0 commit comments