Created
February 21, 2018 21:36
-
-
Save digoreis/7f55aede30b3226ec222b2ca9ef2eacf to your computer and use it in GitHub Desktop.
Sample service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Dispatch | |
import Foundation | |
public protocol Service { | |
static func run() | |
} | |
class ServiceApplication { | |
static private let queue = DispatchQueue.global(qos: .background) | |
static private var services = [Service.Type]() | |
static private var config : Dictionary<String,Any>? | |
static func register(_ service: Service.Type) { | |
services.append(service) | |
} | |
static private func load() { | |
services.forEach { service in | |
queue.async { | |
service.run() | |
} | |
} | |
} | |
static private func loadConfigDictionary() { | |
let currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath) | |
if #available(OSX 10.11, *) { | |
let url = URL(fileURLWithPath: CommandLine.arguments[1], relativeTo: currentDirectoryURL) | |
config = NSDictionary(contentsOf: url) as? Dictionary<String,Any> | |
} else { | |
// Fallback on earlier versions | |
} | |
} | |
static func getConfig(_ key: String) -> Any? { | |
return config?[key] | |
} | |
static func run() { | |
if CommandLine.argc > 1 { | |
loadConfigDictionary() | |
} | |
load() | |
while true { | |
} | |
} | |
} | |
// ***************************************************************************** | |
// SAMPLE | |
// ***************************************************************************** | |
public class PrintService : Service { | |
static public func run() { | |
for i in 0..<10 { | |
print("hello world : \(i)") | |
} | |
} | |
} | |
public class Print2Service : Service { | |
static public func run() { | |
for i in 0..<10 { | |
print("hello world2 : \(i*i)") | |
} | |
} | |
} | |
ServiceApplication.register(PrintService.self) | |
ServiceApplication.register(Print2Service.self) | |
ServiceApplication.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment