// Circle Worker Manager Example // // This example demonstrates how to use the service manager to dynamically launch // circle workers for new freezone residents. This is the primary use case requested // by the team. // // Usage: // // On macOS (uses launchctl): // herodo examples/service_manager/circle_worker_manager.rhai // // On Linux (uses zinit - requires zinit to be running): // First start zinit: zinit -s /tmp/zinit.sock init // herodo examples/service_manager/circle_worker_manager.rhai // Circle Worker Manager Example // This example uses the SAL service manager through Rhai integration print("๐Ÿš€ Circle Worker Manager Example"); print("================================="); // Create the appropriate service manager for the current platform let service_manager = create_service_manager(); print("โœ… Created service manager for current platform"); // Simulate a new freezone resident registration let resident_id = "resident_12345"; let worker_name = `circle-worker-${resident_id}`; print(`\n๐Ÿ“ New freezone resident registered: ${resident_id}`); print(`๐Ÿ”ง Creating circle worker service: ${worker_name}`); // Create service configuration for the circle worker let config = #{ name: worker_name, binary_path: "/bin/sh", args: [ "-c", `echo 'Circle worker for ${resident_id} starting...'; sleep 30; echo 'Circle worker for ${resident_id} completed'` ], working_directory: "/tmp", environment: #{ "RESIDENT_ID": resident_id, "WORKER_TYPE": "circle", "LOG_LEVEL": "info" }, auto_restart: true }; print("๐Ÿ“‹ Service configuration created:"); print(` Name: ${config.name}`); print(` Binary: ${config.binary_path}`); print(` Args: ${config.args}`); print(` Auto-restart: ${config.auto_restart}`); print(`\n๐Ÿ”„ Demonstrating service lifecycle for: ${worker_name}`); // 1. Check if service already exists print("\n1๏ธโƒฃ Checking if service exists..."); if exists(service_manager, worker_name) { print("โš ๏ธ Service already exists, removing it first..."); remove(service_manager, worker_name); print("๐Ÿ—‘๏ธ Existing service removed"); } else { print("โœ… Service doesn't exist, ready to create"); } // 2. Start the service print("\n2๏ธโƒฃ Starting the circle worker service..."); start(service_manager, config); print("โœ… Service started successfully"); // 3. Check service status print("\n3๏ธโƒฃ Checking service status..."); let status = status(service_manager, worker_name); print(`๐Ÿ“Š Service status: ${status}`); // 4. List all services to show our service is there print("\n4๏ธโƒฃ Listing all managed services..."); let services = list(service_manager); print(`๐Ÿ“‹ Managed services (${services.len()}):`); for service in services { let marker = if service == worker_name { "๐Ÿ‘‰" } else { " " }; print(` ${marker} ${service}`); } // 5. Wait a moment and check status again print("\n5๏ธโƒฃ Waiting 3 seconds and checking status again..."); sleep(3000); // 3 seconds in milliseconds let status = status(service_manager, worker_name); print(`๐Ÿ“Š Service status after 3s: ${status}`); // 6. Get service logs print("\n6๏ธโƒฃ Retrieving service logs..."); let logs = logs(service_manager, worker_name, 10); if logs.trim() == "" { print("๐Ÿ“„ No logs available yet (this is normal for new services)"); } else { print("๐Ÿ“„ Recent logs:"); let log_lines = logs.split('\n'); for i in 0..5 { if i < log_lines.len() { print(` ${log_lines[i]}`); } } } // 7. Demonstrate start_and_confirm with timeout print("\n7๏ธโƒฃ Testing start_and_confirm (should succeed quickly since already running)..."); start_and_confirm(service_manager, config, 5); print("โœ… Service confirmed running within timeout"); // 8. Stop the service print("\n8๏ธโƒฃ Stopping the service..."); stop(service_manager, worker_name); print("๐Ÿ›‘ Service stopped"); // 9. Check status after stopping print("\n9๏ธโƒฃ Checking status after stop..."); let status = status(service_manager, worker_name); print(`๐Ÿ“Š Service status after stop: ${status}`); // 10. Restart the service print("\n๐Ÿ”Ÿ Restarting the service..."); restart(service_manager, worker_name); print("๐Ÿ”„ Service restarted successfully"); // 11. Final cleanup print("\n๐Ÿงน Cleaning up - removing the service..."); remove(service_manager, worker_name); print("๐Ÿ—‘๏ธ Service removed successfully"); // 12. Verify removal print("\nโœ… Verifying service removal..."); if !exists(service_manager, worker_name) { print("โœ… Service successfully removed"); } else { print("โš ๏ธ Service still exists after removal"); } print("\n๐ŸŽ‰ Circle worker management demonstration complete!");