made ServiceManager a static class
This commit is contained in:
parent
3b3c69ee56
commit
088fce0aaa
@ -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);
|
||||
},"<name>");
|
||||
|
||||
@ -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);
|
||||
},"<name>");
|
||||
|
||||
@ -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);
|
||||
},"<name> <binary> [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);
|
||||
},"<name>");
|
||||
|
||||
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;
|
||||
},"<name>");
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ class Res extends CLIAppBase {
|
||||
},"<url>");
|
||||
|
||||
registerAsyncSubcommand("register",(args)->{
|
||||
var srv: Null<ResManager> = ServiceManager.instance.get("resmgr");
|
||||
var srv: Null<ResManager> = ServiceManager.get("resmgr");
|
||||
|
||||
var addr = args[0];
|
||||
var name = args[1];
|
||||
|
@ -54,6 +54,6 @@ class Init {
|
||||
KernelEvents.startEventLoop();
|
||||
});
|
||||
|
||||
ServiceManager.instance = new ServiceManager();
|
||||
ServiceManager.init();
|
||||
}
|
||||
}
|
||||
|
@ -7,20 +7,18 @@ import lib.KVStore;
|
||||
using tink.CoreApi;
|
||||
|
||||
class ServiceManager {
|
||||
public static var instance: ServiceManager;
|
||||
|
||||
private final services:Map<String,Service> = new Map();
|
||||
private static final services:Map<String,Service> = 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<String> = 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<String> = store.get("enabled",[]);
|
||||
for (name in enabled){
|
||||
this.start(name);
|
||||
start(name);
|
||||
}
|
||||
}
|
||||
|
||||
private function load(name: String): Null<Service> {
|
||||
private static function load(name: String): Null<Service> {
|
||||
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<String>): Outcome<Noise,String> {
|
||||
public static function register(name: String, binName: String,args: Array<String>): Outcome<Noise,String> {
|
||||
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<Noise,String> {
|
||||
if (this.services.exists(name)){
|
||||
public static function unregister(name: String): Outcome<Noise,String> {
|
||||
if (services.exists(name)){
|
||||
return Failure("service is running");
|
||||
}
|
||||
|
||||
@ -95,48 +93,48 @@ class ServiceManager {
|
||||
return Success(Noise);
|
||||
}
|
||||
|
||||
public function start(name: String): Outcome<Noise,String> {
|
||||
var service = this.load(name);
|
||||
public static function start(name: String): Outcome<Noise,String> {
|
||||
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<Noise,String> {
|
||||
if (!this.services.exists(name)){
|
||||
public static function stop(name: String): Outcome<Noise,String> {
|
||||
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<String> {
|
||||
public static function listRunning(): Array<String> {
|
||||
var running = [];
|
||||
for (name in this.services.keys()){
|
||||
for (name in services.keys()){
|
||||
running.push(name);
|
||||
}
|
||||
return running;
|
||||
}
|
||||
|
||||
public function get(name: String): Null<Dynamic> {
|
||||
if (!this.services.exists(name)){
|
||||
public static function get(name: String): Null<Dynamic> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user