Skip to content

Service Registry

Xellu edited this page May 31, 2026 · 4 revisions

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)

Lifecycle Hooks

Method When it's called Use for
onInstall() On nautica install, and before onStart For setting up configs, directories
onStart(registry) On nautica run, after dependencies are ready For starting threads, connecting to DBs, etc.
onClose(reason) On shutdown Clean up resources, stop threads
isEnabled() Before onStart Return a boolean indicating if the service is enabled

Accessing Other Services

The registry argument in onStart gives you access to other running services. You can also use Services.Get() from anywhere outside of onStart:

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 onStart

Exporting

A service has to be exported to be registered with the runtime. Do this at module level, after the class definition:

Service.Export(MyService)

Creating a source directory

If your service needs its own folder under src/, pass srcDir:

Service.Export(MyService, srcDir="myservice") # creates folder "src/myservice"

Adding Dependencies

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 available

Plugins

Services placed in the plugins/ directory are automatically imported when the project starts. Create a .py file, define your service and call Service.Export()

Full Example

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.Update("nautica",
            ConfigBuilder()
                .add("services.myservice", True, comment="Enable MyService")
                .build()
        )
        Config.New("myservice",
            ConfigBuilder()
                .add("interval", 5, comment="Task interval in seconds")
                .build()
        )

    def isEnabled(self):
        return Config("nautica")["services.myservice"]

    def onStart(self, registry):
        self.running = True
        self.thread = threading.Thread(target=self._run, daemon=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)

Clone this wiki locally