- Improve Linux support by automatically discovering zinit sockets using environment variables and common paths. - Add fallback to systemd if no zinit server is detected. - Enhance README with detailed instructions for zinit usage, including custom socket path configuration. - Add example demonstrating zinit socket discovery. - Add logging to show socket discovery process. - Add unit tests for service manager creation and socket discovery.
86 lines
2.3 KiB
Plaintext
86 lines
2.3 KiB
Plaintext
// Basic Service Manager Usage Example
|
||
//
|
||
// This example demonstrates the basic API of the service manager.
|
||
// It works on both macOS (launchctl) and Linux (zinit/systemd).
|
||
//
|
||
// Prerequisites:
|
||
//
|
||
// Linux: The service manager will automatically discover running zinit servers
|
||
// or fall back to systemd. To use zinit, start it with:
|
||
// zinit -s /tmp/zinit.sock init
|
||
//
|
||
// You can also specify a custom socket path:
|
||
// export ZINIT_SOCKET_PATH=/your/custom/path/zinit.sock
|
||
//
|
||
// macOS: No additional setup required (uses launchctl).
|
||
//
|
||
// Usage:
|
||
// herodo examples/service_manager/basic_usage.rhai
|
||
|
||
// Service Manager Basic Usage Example
|
||
// This example uses the SAL service manager through Rhai integration
|
||
|
||
print("🚀 Basic Service Manager Usage Example");
|
||
print("======================================");
|
||
|
||
// Create a service manager for the current platform
|
||
let manager = create_service_manager();
|
||
|
||
print("🍎 Using service manager for current platform");
|
||
|
||
// Create a simple service configuration
|
||
let config = #{
|
||
name: "example-service",
|
||
binary_path: "/bin/echo",
|
||
args: ["Hello from service manager!"],
|
||
working_directory: "/tmp",
|
||
environment: #{
|
||
"EXAMPLE_VAR": "hello_world"
|
||
},
|
||
auto_restart: false
|
||
};
|
||
|
||
print("\n📝 Service Configuration:");
|
||
print(` Name: ${config.name}`);
|
||
print(` Binary: ${config.binary_path}`);
|
||
print(` Args: ${config.args}`);
|
||
|
||
// Start the service
|
||
print("\n🚀 Starting service...");
|
||
start(manager, config);
|
||
print("✅ Service started successfully");
|
||
|
||
// Check service status
|
||
print("\n📊 Checking service status...");
|
||
let status = status(manager, "example-service");
|
||
print(`Status: ${status}`);
|
||
|
||
// List all services
|
||
print("\n📋 Listing all managed services...");
|
||
let services = list(manager);
|
||
print(`Found ${services.len()} services:`);
|
||
for service in services {
|
||
print(` - ${service}`);
|
||
}
|
||
|
||
// Get service logs
|
||
print("\n📄 Getting service logs...");
|
||
let logs = logs(manager, "example-service", 5);
|
||
if logs.trim() == "" {
|
||
print("No logs available");
|
||
} else {
|
||
print(`Logs:\n${logs}`);
|
||
}
|
||
|
||
// Stop the service
|
||
print("\n🛑 Stopping service...");
|
||
stop(manager, "example-service");
|
||
print("✅ Service stopped");
|
||
|
||
// Remove the service
|
||
print("\n🗑️ Removing service...");
|
||
remove(manager, "example-service");
|
||
print("✅ Service removed");
|
||
|
||
print("\n🎉 Example completed successfully!");
|