trading_agent.extensions.systemd_user_units
¶
Reads and changes the state of systemd user units, for the apps that packages bring.
Apps such as sridhara already run as systemd user units, which start at login and restart after a crash. trading-agent reports whether each app's unit is running and can start, stop or restart it, but only units named by an installed package's app.toml, and only with systemctl --user, never as root.
Typical usage example:
units = SystemdUserUnits() state = await units.state('sridhara.service') await units.act('sridhara.service', 'restart')
UnitError
¶
Bases: RuntimeError
systemctl failed, or was asked about a unit name it should not be given.
UnitState
dataclass
¶
What systemd says about one unit.
Attributes:
| Name | Type | Description |
|---|---|---|
load_state |
str
|
"loaded" when systemd knows the unit, "not-found" when it does not. |
active_state |
str
|
Such as "active", "inactive" or "failed". |
sub_state |
str
|
Such as "running" or "dead". |
SystemdUserUnits
¶
Talks to the user's systemd through systemctl --user.
state
async
¶
Asks systemd for a unit's state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
str
|
The unit, such as sridhara.service. |
required |
Returns:
| Type | Description |
|---|---|
UnitState
|
The unit's state. |
Raises:
| Type | Description |
|---|---|
UnitError
|
The name is not a service unit, or systemctl failed. |
Source code in src/trading_agent/extensions/systemd_user_units.py
act
async
¶
Starts, stops or restarts a unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
str
|
The unit. |
required |
action
|
str
|
"start", "stop" or "restart". |
required |
Raises:
| Type | Description |
|---|---|
UnitError
|
The action is unknown, the name is not a service unit, or systemctl failed. |
Source code in src/trading_agent/extensions/systemd_user_units.py
enable
async
¶
Links a unit file if one is given, then enables and starts the unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
str
|
The unit. |
required |
unit_file
|
Path | None
|
The unit file to link first, or None when systemd already knows the unit. |
required |
Raises:
| Type | Description |
|---|---|
UnitError
|
The name is not a service unit, the file's name does not match it, or systemctl failed. |