private void InvokeUpdateClientPackage() { if (ms_clientUpdateQueued) { return; } ms_clientUpdateQueued = true; Task.Run(async() => { await Task.Delay(500); // go var runner = new Tasks.ResourceTaskRunner(); runner.ExecuteTasks(this); // and unlock the lock ms_clientUpdateQueued = false; }); }
public async Task<bool> Start() { if (State == ResourceState.Running) { return true; } if (State != ResourceState.Stopped && State != ResourceState.Starting) { throw new InvalidOperationException("can not start a resource that is not stopped"); } this.Log().Info("Starting resource {0} (last state: {1}).", Name, State); // as this is already done for us in this case if (State != ResourceState.Starting) { if (!Parse()) { State = ResourceState.Error; return false; } // resolve dependencies foreach (var dep in Dependencies) { var res = Manager.GetResource(dep); if (res == null) { this.Log().Warn("Can't resolve dependency {0} from resource {1}.", dep, Name); return false; } await res.Start(); res.AddDependant(Name); } // execute tasks var runner = new Tasks.ResourceTaskRunner(); if (!await runner.ExecuteTasks(this)) { this.Log().Error("Executing tasks for resource {0} failed.", Name); State = ResourceState.Stopped; return false; } m_watcher = new FileSystemWatcher(); // create script environment if (!EnsureScriptEnvironment()) { return false; } } State = ResourceState.Starting; m_scriptEnvironment.DoInitFile(false); // trigger event if (!Manager.TriggerEvent("onResourceStarting", -1, Name)) { // how the h- if (State == ResourceState.Running) { return true; } Stop(); return false; } m_scriptEnvironment.LoadScripts(); // TODO: add development mode check m_watcher.Path = Path; m_watcher.IncludeSubdirectories = true; m_watcher.NotifyFilter = NotifyFilters.LastWrite; m_watcher.Changed += (s, e) => InvokeUpdateClientPackage(); m_watcher.Created += (s, e) => InvokeUpdateClientPackage(); m_watcher.Deleted += (s, e) => InvokeUpdateClientPackage(); m_watcher.Renamed += (s, e) => InvokeUpdateClientPackage(); m_watcher.EnableRaisingEvents = true; State = ResourceState.Running; // trigger event if (!Manager.TriggerEvent("onResourceStart", -1, Name)) { this.Log().Info("Resource start canceled by event."); Stop(); return false; } // log that we started this.Log().Info("Started resource {0}.", Name); // broadcast to current clients var clients = ClientInstances.Clients.Where(c => c.Value.NetChannel != null).Select(c => c.Value); foreach (var client in clients) { client.SendReliableCommand(0xAFE4CD4A, Encoding.UTF8.GetBytes(Name)); // msgResStart } return true; }
public async Task <bool> Start() { if (State == ResourceState.Running) { return(true); } if (State != ResourceState.Stopped && State != ResourceState.Starting) { throw new InvalidOperationException("can not start a resource that is not stopped"); } this.Log().Info("Starting resource {0} (last state: {1}).", Name, State); // as this is already done for us in this case if (State != ResourceState.Starting) { if (!Parse()) { State = ResourceState.Error; return(false); } // resolve dependencies foreach (var dep in Dependencies) { var res = Manager.GetResource(dep); if (res == null) { this.Log().Warn("Can't resolve dependency {0} from resource {1}.", dep, Name); return(false); } await res.Start(); res.AddDependant(Name); } // execute tasks var runner = new Tasks.ResourceTaskRunner(); if (!await runner.ExecuteTasks(this)) { this.Log().Error("Executing tasks for resource {0} failed.", Name); State = ResourceState.Stopped; return(false); } m_watcher = new FileSystemWatcher(); // create script environment if (!EnsureScriptEnvironment()) { return(false); } } State = ResourceState.Starting; m_scriptEnvironment.DoInitFile(false); // trigger event if (!Manager.TriggerEvent("onResourceStarting", -1, Name)) { // how the h- if (State == ResourceState.Running) { return(true); } Stop(); return(false); } m_scriptEnvironment.LoadScripts(); // TODO: add development mode check m_watcher.Path = Path; m_watcher.IncludeSubdirectories = true; m_watcher.NotifyFilter = NotifyFilters.LastWrite; m_watcher.Changed += (s, e) => InvokeUpdateClientPackage(); m_watcher.Created += (s, e) => InvokeUpdateClientPackage(); m_watcher.Deleted += (s, e) => InvokeUpdateClientPackage(); m_watcher.Renamed += (s, e) => InvokeUpdateClientPackage(); m_watcher.EnableRaisingEvents = true; State = ResourceState.Running; // trigger event if (!Manager.TriggerEvent("onResourceStart", -1, Name)) { this.Log().Info("Resource start canceled by event."); Stop(); return(false); } // log that we started this.Log().Info("Started resource {0}.", Name); // broadcast to current clients var clients = ClientInstances.Clients.Where(c => c.Value.NetChannel != null).Select(c => c.Value); foreach (var client in clients) { client.SendReliableCommand(0xAFE4CD4A, Encoding.UTF8.GetBytes(Name)); // msgResStart } return(true); }
private void InvokeUpdateClientPackage() { if (ms_clientUpdateQueued) { return; } ms_clientUpdateQueued = true; Task.Run(async () => { await Task.Delay(500); // go var runner = new Tasks.ResourceTaskRunner(); runner.ExecuteTasks(this); // and unlock the lock ms_clientUpdateQueued = false; }); }