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 name = args[0];
|
||||||
|
|
||||||
var result = ServiceManager.instance.start(name);
|
var result = ServiceManager.start(name);
|
||||||
return handleResult(result);
|
return handleResult(result);
|
||||||
},"<name>");
|
},"<name>");
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ class Service extends CLIAppBase {
|
|||||||
|
|
||||||
var name = args[0];
|
var name = args[0];
|
||||||
|
|
||||||
var result = ServiceManager.instance.stop(name);
|
var result = ServiceManager.stop(name);
|
||||||
return handleResult(result);
|
return handleResult(result);
|
||||||
},"<name>");
|
},"<name>");
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ class Service extends CLIAppBase {
|
|||||||
var binName = args[1];
|
var binName = args[1];
|
||||||
var rest = args.slice(2);
|
var rest = args.slice(2);
|
||||||
|
|
||||||
var result = ServiceManager.instance.register(name, binName, rest);
|
var result = ServiceManager.register(name, binName, rest);
|
||||||
return handleResult(result);
|
return handleResult(result);
|
||||||
},"<name> <binary> [args...]");
|
},"<name> <binary> [args...]");
|
||||||
|
|
||||||
@ -49,13 +49,13 @@ class Service extends CLIAppBase {
|
|||||||
|
|
||||||
var name = args[0];
|
var name = args[0];
|
||||||
|
|
||||||
var result = ServiceManager.instance.unregister(name);
|
var result = ServiceManager.unregister(name);
|
||||||
return handleResult(result);
|
return handleResult(result);
|
||||||
},"<name>");
|
},"<name>");
|
||||||
|
|
||||||
registerSyncSubcommand("list", (args) ->{
|
registerSyncSubcommand("list", (args) ->{
|
||||||
|
|
||||||
var list = ServiceManager.instance.listRunning();
|
var list = ServiceManager.listRunning();
|
||||||
|
|
||||||
for (name in list) {
|
for (name in list) {
|
||||||
this.handle.writeLine(name);
|
this.handle.writeLine(name);
|
||||||
@ -69,7 +69,7 @@ class Service extends CLIAppBase {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceManager.instance.enable(args[0]);
|
ServiceManager.enable(args[0]);
|
||||||
return true;
|
return true;
|
||||||
},"<name>");
|
},"<name>");
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ class Res extends CLIAppBase {
|
|||||||
},"<url>");
|
},"<url>");
|
||||||
|
|
||||||
registerAsyncSubcommand("register",(args)->{
|
registerAsyncSubcommand("register",(args)->{
|
||||||
var srv: Null<ResManager> = ServiceManager.instance.get("resmgr");
|
var srv: Null<ResManager> = ServiceManager.get("resmgr");
|
||||||
|
|
||||||
var addr = args[0];
|
var addr = args[0];
|
||||||
var name = args[1];
|
var name = args[1];
|
||||||
|
@ -54,6 +54,6 @@ class Init {
|
|||||||
KernelEvents.startEventLoop();
|
KernelEvents.startEventLoop();
|
||||||
});
|
});
|
||||||
|
|
||||||
ServiceManager.instance = new ServiceManager();
|
ServiceManager.init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,20 +7,18 @@ import lib.KVStore;
|
|||||||
using tink.CoreApi;
|
using tink.CoreApi;
|
||||||
|
|
||||||
class ServiceManager {
|
class ServiceManager {
|
||||||
public static var instance: ServiceManager;
|
private static final services:Map<String,Service> = new Map();
|
||||||
|
|
||||||
private final services:Map<String,Service> = new Map();
|
|
||||||
|
|
||||||
@:allow(kernel.Init)
|
@:allow(kernel.Init)
|
||||||
private function new() {
|
private static function init() {
|
||||||
this.startAllEnabled();
|
startAllEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Add a service to be automatically started.
|
Add a service to be automatically started.
|
||||||
**/
|
**/
|
||||||
public function enable(name: String) {
|
public static function enable(name: String) {
|
||||||
if (!this.services.exists(name)){
|
if (!services.exists(name)){
|
||||||
return; // Service must be started
|
return; // Service must be started
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +34,7 @@ class ServiceManager {
|
|||||||
/**
|
/**
|
||||||
Remove a service from being automatically started.
|
Remove a service from being automatically started.
|
||||||
**/
|
**/
|
||||||
private function disable(name: String) {
|
private static function disable(name: String) {
|
||||||
var store = KVStore.getStoreForClass();
|
var store = KVStore.getStoreForClass();
|
||||||
var enabled: Array<String> = store.get("enabled");
|
var enabled: Array<String> = store.get("enabled");
|
||||||
var index = enabled.indexOf(name);
|
var index = enabled.indexOf(name);
|
||||||
@ -48,15 +46,15 @@ class ServiceManager {
|
|||||||
store.save();
|
store.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function startAllEnabled() {
|
private static function startAllEnabled() {
|
||||||
var store = KVStore.getStoreForClass();
|
var store = KVStore.getStoreForClass();
|
||||||
var enabled: Array<String> = store.get("enabled",[]);
|
var enabled: Array<String> = store.get("enabled",[]);
|
||||||
for (name in 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}');
|
var store = new KVStore('service/${name}');
|
||||||
store.load();
|
store.load();
|
||||||
if (!store.exists("service")){
|
if (!store.exists("service")){
|
||||||
@ -66,12 +64,12 @@ class ServiceManager {
|
|||||||
return store.get("service");
|
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){
|
if (BinStore.getBinByAlias(binName) == null){
|
||||||
return Failure("bin not found");
|
return Failure("bin not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.load(name) != null){
|
if (load(name) != null){
|
||||||
return Failure("service already exists");
|
return Failure("service already exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,8 +83,8 @@ class ServiceManager {
|
|||||||
return Success(Noise);
|
return Success(Noise);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function unregister(name: String): Outcome<Noise,String> {
|
public static function unregister(name: String): Outcome<Noise,String> {
|
||||||
if (this.services.exists(name)){
|
if (services.exists(name)){
|
||||||
return Failure("service is running");
|
return Failure("service is running");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,48 +93,48 @@ class ServiceManager {
|
|||||||
return Success(Noise);
|
return Success(Noise);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start(name: String): Outcome<Noise,String> {
|
public static function start(name: String): Outcome<Noise,String> {
|
||||||
var service = this.load(name);
|
var service = load(name);
|
||||||
if (service == null){
|
if (service == null){
|
||||||
return Failure("service not found");
|
return Failure("service not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
service.start();
|
service.start();
|
||||||
this.services.set(name,service);
|
services.set(name,service);
|
||||||
|
|
||||||
Log.info('Service ${name} started');
|
Log.info('Service ${name} started');
|
||||||
return Success(Noise);
|
return Success(Noise);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stop(name: String): Outcome<Noise,String> {
|
public static function stop(name: String): Outcome<Noise,String> {
|
||||||
if (!this.services.exists(name)){
|
if (!services.exists(name)){
|
||||||
return Failure("service not found");
|
return Failure("service not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
var service = this.services.get(name);
|
var service = services.get(name);
|
||||||
service.stop();
|
service.stop();
|
||||||
this.services.remove(name);
|
services.remove(name);
|
||||||
|
|
||||||
Log.info('Service ${name} stopped');
|
Log.info('Service ${name} stopped');
|
||||||
return Success(Noise);
|
return Success(Noise);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function listRunning(): Array<String> {
|
public static function listRunning(): Array<String> {
|
||||||
var running = [];
|
var running = [];
|
||||||
for (name in this.services.keys()){
|
for (name in services.keys()){
|
||||||
running.push(name);
|
running.push(name);
|
||||||
}
|
}
|
||||||
return running;
|
return running;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(name: String): Null<Dynamic> {
|
public static function get(name: String): Null<Dynamic> {
|
||||||
if (!this.services.exists(name)){
|
if (!services.exists(name)){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Maybe there is a way to check types here?
|
// TODO: Maybe there is a way to check types here?
|
||||||
|
|
||||||
var srv = this.services.get(name);
|
var srv = services.get(name);
|
||||||
return srv.ps;
|
return srv.ps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user