-
Notifications
You must be signed in to change notification settings - Fork 0
Service Registry
https://github.com/xellu/nautica-api/tree/main/nautica/services
Service is a building block in Nautica. It's a component that plugs into Nautica's runtime, that is able expose functionality to other services, start background tasks, define a config, and clean up after.
from nautica import Service, Logger
class MyService(Service):
def onStart(self, registry):
Logger.ok("MyService started!")
Service.Export(MyService)| Method | When it's called | Use for |
|---|---|---|
onInstall() |
On nautica install, and before onStart
|
For setting up configs, directories |
onSetup(registry) |
On nautica run, After dependencies are ready |
For connecting to databases or other setup |
onStart(registry) |
After onSetup
|
For starting servers, threads, etc. |
onClose(reason) |
On shutdown | Clean up resources, stop threads |
isEnabled() |
Before onSetup
|
Optional, return a boolean indicating if the service is enabled |
By default,
isEnabled()automatically registers a toggle inconfig.n3underservices.<servicename>and reads from it. Override to add custom logic, or callsuper().isEnabled()to combine both.
The registry argument in onStart & onSetup gives you access to other running services.
You can also use Services.get(str)/Services[str] from anywhere outside of onStart, onSetup:
from nautica import Service, Services
class MyService(Service):
def onStart(self, registry):
router = registry.Get("HTTPRouter") # preferred inside onStart
def some_method(self):
router = Services.Get("HTTPRouter") # use this outside of onStartA service has to be exported to be registered with the runtime. Do this at module level, after the class definition:
Service.Export(MyService)If your service needs its own folder under src/, pass srcDir:
Service.Export(MyService, srcDir="myservice") # creates folder "src/myservice"If your service depends on another, use depends_on to make sure it starts first. Nautica will always boot dependencies before your service:
Service.Export(MyService, depends_on=["HTTPRouter"]) # This way HTTP Router will be always availableBy adding ? at the end, you can make the dependency optional:
Service.Export(MyService, depends_on=["HTTPRouter?"] # Will not crash on start if the dependency is missingWhen you need a dependency to start after (not before), you can use :after:
Service.Export(MyService, depends_on=["HTTPServer:after"])
# This will make sure that the HTTP Server starts after your service
Service.Export(MyService, depends_on=["HTTPServer:after?"]) # This works tooServices placed in the plugins/ directory are automatically imported when the project starts.
Create a .py file, define your service and call Service.Export(). To install plugins from the Package Registry, use nautica install <package name>
from nautica import Service, Logger, Config, ConfigBuilder
import threading
class MyService(Service):
def __init__(self):
super().__init__()
self.thread = None
self.running = False
def onInstall(self):
Config.New("myservice",
ConfigBuilder()
.add("interval", 5, comment="Task interval in seconds")
.build()
)
def onSetup(self, registry):
self.thread = threading.Thread(target=self._run, daemon=True)
def onStart(self, registry):
self.running = True
self.thread.start()
Logger.ok("MyService started")
def onClose(self, reason):
self.running = False
Logger.info("MyService stopped")
def _run(self):
import time
while self.running:
Logger.info("MyService tick")
time.sleep(Config("myservice")["interval"])
Service.Export(MyService)- Home
- CLI Reference
- Service Registry
- Built-in Services
- Managers
- Requirement Validators