Skip to content

Service Registry

Xellu edited this page Jun 14, 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
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 in config.n3 under services.<servicename> and reads from it. Override to add custom logic, or call super().isEnabled() to combine both.

Accessing Other Services

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 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

By 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 missing

When 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 too

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(). To install plugins from the Package Registry, use nautica install <package name>

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.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)

Clone this wiki locally