From 088fce0aaa2aa1d150cead342c20f0b02cde0cbc Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sun, 30 Jul 2023 15:48:22 +0200 Subject: [PATCH] made ServiceManager a static class --- src/bin/Service.hx | 12 +++---- src/bin/exporter/Res.hx | 2 +- src/kernel/Init.hx | 2 +- src/kernel/service/ServiceManager.hx | 52 +++++++++++++--------------- 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/bin/Service.hx b/src/bin/Service.hx index 870e43d..98a8f0e 100644 --- a/src/bin/Service.hx +++ b/src/bin/Service.hx @@ -14,7 +14,7 @@ class Service extends CLIAppBase { var name = args[0]; - var result = ServiceManager.instance.start(name); + var result = ServiceManager.start(name); return handleResult(result); },""); @@ -25,7 +25,7 @@ class Service extends CLIAppBase { var name = args[0]; - var result = ServiceManager.instance.stop(name); + var result = ServiceManager.stop(name); return handleResult(result); },""); @@ -38,7 +38,7 @@ class Service extends CLIAppBase { var binName = args[1]; var rest = args.slice(2); - var result = ServiceManager.instance.register(name, binName, rest); + var result = ServiceManager.register(name, binName, rest); return handleResult(result); }," [args...]"); @@ -49,13 +49,13 @@ class Service extends CLIAppBase { var name = args[0]; - var result = ServiceManager.instance.unregister(name); + var result = ServiceManager.unregister(name); return handleResult(result); },""); registerSyncSubcommand("list", (args) ->{ - var list = ServiceManager.instance.listRunning(); + var list = ServiceManager.listRunning(); for (name in list) { this.handle.writeLine(name); @@ -69,7 +69,7 @@ class Service extends CLIAppBase { return false; } - ServiceManager.instance.enable(args[0]); + ServiceManager.enable(args[0]); return true; },""); } diff --git a/src/bin/exporter/Res.hx b/src/bin/exporter/Res.hx index e80f461..98cdb36 100644 --- a/src/bin/exporter/Res.hx +++ b/src/bin/exporter/Res.hx @@ -25,7 +25,7 @@ class Res extends CLIAppBase { },""); registerAsyncSubcommand("register",(args)->{ - var srv: Null = ServiceManager.instance.get("resmgr"); + var srv: Null = ServiceManager.get("resmgr"); var addr = args[0]; var name = args[1]; diff --git a/src/kernel/Init.hx b/src/kernel/Init.hx index 11e0b80..e45bb1f 100644 --- a/src/kernel/Init.hx +++ b/src/kernel/Init.hx @@ -54,6 +54,6 @@ class Init { KernelEvents.startEventLoop(); }); - ServiceManager.instance = new ServiceManager(); + ServiceManager.init(); } } diff --git a/src/kernel/service/ServiceManager.hx b/src/kernel/service/ServiceManager.hx index c14d2bd..6c03872 100644 --- a/src/kernel/service/ServiceManager.hx +++ b/src/kernel/service/ServiceManager.hx @@ -7,20 +7,18 @@ import lib.KVStore; using tink.CoreApi; class ServiceManager { - public static var instance: ServiceManager; - - private final services:Map = new Map(); + private static final services:Map = new Map(); @:allow(kernel.Init) - private function new() { - this.startAllEnabled(); + private static function init() { + startAllEnabled(); } /** Add a service to be automatically started. **/ - public function enable(name: String) { - if (!this.services.exists(name)){ + public static function enable(name: String) { + if (!services.exists(name)){ return; // Service must be started } @@ -36,7 +34,7 @@ class ServiceManager { /** Remove a service from being automatically started. **/ - private function disable(name: String) { + private static function disable(name: String) { var store = KVStore.getStoreForClass(); var enabled: Array = store.get("enabled"); var index = enabled.indexOf(name); @@ -48,15 +46,15 @@ class ServiceManager { store.save(); } - private function startAllEnabled() { + private static function startAllEnabled() { var store = KVStore.getStoreForClass(); var enabled: Array = store.get("enabled",[]); for (name in enabled){ - this.start(name); + start(name); } } - private function load(name: String): Null { + private static function load(name: String): Null { var store = new KVStore('service/${name}'); store.load(); if (!store.exists("service")){ @@ -66,12 +64,12 @@ class ServiceManager { return store.get("service"); } - public function register(name: String, binName: String,args: Array): Outcome { + public static function register(name: String, binName: String,args: Array): Outcome { if (BinStore.getBinByAlias(binName) == null){ return Failure("bin not found"); } - if (this.load(name) != null){ + if (load(name) != null){ return Failure("service already exists"); } @@ -85,8 +83,8 @@ class ServiceManager { return Success(Noise); } - public function unregister(name: String): Outcome { - if (this.services.exists(name)){ + public static function unregister(name: String): Outcome { + if (services.exists(name)){ return Failure("service is running"); } @@ -95,48 +93,48 @@ class ServiceManager { return Success(Noise); } - public function start(name: String): Outcome { - var service = this.load(name); + public static function start(name: String): Outcome { + var service = load(name); if (service == null){ return Failure("service not found"); } service.start(); - this.services.set(name,service); + services.set(name,service); Log.info('Service ${name} started'); return Success(Noise); } - public function stop(name: String): Outcome { - if (!this.services.exists(name)){ + public static function stop(name: String): Outcome { + if (!services.exists(name)){ return Failure("service not found"); } - var service = this.services.get(name); + var service = services.get(name); service.stop(); - this.services.remove(name); + services.remove(name); Log.info('Service ${name} stopped'); return Success(Noise); } - public function listRunning(): Array { + public static function listRunning(): Array { var running = []; - for (name in this.services.keys()){ + for (name in services.keys()){ running.push(name); } return running; } - public function get(name: String): Null { - if (!this.services.exists(name)){ + public static function get(name: String): Null { + if (!services.exists(name)){ return null; } // TODO: Maybe there is a way to check types here? - var srv = this.services.get(name); + var srv = services.get(name); return srv.ps; }