private void Process(ActionQueueItem item, CancellationTokenSource cancelSource) { try { var simulated = _actionSimulation ? " (simulated)" : string.Empty; var threaded = cancelSource != null ? " thread" : string.Empty; Console.WriteLine($"Start{threaded}{simulated} action {item.Action.GetType().Name}, config: {item.Action.ConfigName} ({item.Host})"); if (!_actionSimulation) { CancellationToken token = cancelSource == null ? CancellationToken.None : cancelSource.Token; IActionHandler handler = _actionHandlers[item.Action.GetType().FullName]; item.ActionHandler = handler; // get config; allow it an action to have an empty config dynamic config = null; string configName = item.ConfigName.Trim(); if (!string.IsNullOrWhiteSpace(configName)) { config = _configManager[configName]; } handler.Action(item.Action, token, config); } Console.WriteLine($"End{threaded}{simulated} action {item.Action.GetType().Name}, config: {item.Action.ConfigName} ({item.Host})"); } catch (Exception ex) { Error($"Error while processing action: {ex.ToString()}"); } }
public void Enqueue(ActionQueueItem item) { if (!_configManager.Exists(item.ConfigName)) { throw new ApplicationException("config not found: " + item.ConfigName); } _actionQueue.Add(item); }
public void addActionQueueItem(CustomAction a, GameObject t = null, int id = 0) { ActionQueueItem aqi = new ActionQueueItem(); aqi.action = a; aqi.target = t; aqi.id = id; this.actionQueue.Add(aqi); }
private void LoadStartupActions() { // startup actions should exist in the "startup" directory var folder = "startup"; var directory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), folder); if (!Directory.Exists(directory)) { Log($"Directory \"{folder}\" does not exist; no startup actions (.json files) loaded"); return; } var files = Directory.EnumerateFiles(directory, "*.json"); foreach (var file in files) { Log($"{nameof(ActionConfigManager)} loading startup file: {Path.GetFileName(file)}"); ActionBase[] actions = null; try { var jsonText = File.ReadAllText(file); actions = JsonConvert.DeserializeObject <ActionBase[]>( jsonText, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full, }); } catch (JsonSerializationException ex) { Error($" File ignored, action deserialization failure: {ex.Message}"); } if (actions != null) { foreach (var action in actions) { if (!action.Enabled) { Log($" Action skipped (not enabled): {action.GetType().FullName}, config: {action.ConfigName}"); continue; } Log($" Action queued: {action.GetType().FullName}, config: {action.ConfigName}"); ActionQueueItem item = new ActionQueueItem(action, action.ConfigName, "localhost"); this.Enqueue(item); } } } }
private async Task DoAction(ActionBase[] actions) { Console.WriteLine($"Received {actions.Length} action(s); queuing..."); await Task.Run(() => { foreach (var action in actions) { try { string fromHost = Request.GetOwinContext().Request.RemoteIpAddress; ActionQueueItem item = new ActionQueueItem(action, action.ConfigName, fromHost); ActionQueueManager.Instance.Enqueue(item); } catch (Exception ex) { Error($"Error while queuing action: {ex.ToString()}"); } } }); }
public void BeginObserving(string actionName /*, bool onlyBuildings = false*/, string modname, bool autoObserving = false) { if (!LoadingExtension.Instsance.m_detoured) { return; } if (autoObserving && Observing && ObservedItem != null && !ObservedItem.AutoObserving) { return; } if (Observing) { EndObserving(); } ObservedItem = new ActionQueueItem(actionName); ObservedItem.ModName = modname; ObservedItem.AutoObserving = autoObserving; ObservedCashBalance = EconomyManager.instance.InternalCashAmount; Observing = true; Invalidated = false; //ObservingOnlyBuildings = onlyBuildings; }
private void Process(ActionQueueItem item, CancellationTokenSource cancelSource) { try { var simulated = _actionSimulation ? " (simulated)" : string.Empty; var threaded = cancelSource != null ? " thread" : string.Empty; Console.WriteLine($"Start{threaded}{simulated} action {item.Action.GetType().Name}, instance: {item.Action.InstanceName} ({item.Host})"); if (!_actionSimulation) { CancellationToken token = cancelSource == null ? CancellationToken.None : cancelSource.Token; IActionHandler handler = _actionHandlers[item.InstanceName]; handler.Action(item.Action, token, _configManager[item.InstanceName]); } Console.WriteLine($"End{threaded}{simulated} action {item.Action.GetType().Name}, instance: {item.Action.InstanceName} ({item.Host})"); } catch (Exception ex) { Error($"Error while processing action: {ex.ToString()}"); } }
private void ProcessQueueItem(ActionQueueItem item) { ImageSource image = IconCache.Instance.GetIconImage(item.FileSystemObject); item.ReturnAction(image); }
private void ProcessQueue(CancellationToken cancelToken) { while (!_actionQueue.IsCompleted && !cancelToken.IsCancellationRequested) { ActionQueueItem queueItem = null; // Blocks if number.Count == 0 // IOE means that Take() was called on a completed collection. // Some other thread can call CompleteAdding after we pass the // IsCompleted check but before we call Take. // In this example, we can simply catch the exception since the // loop will break on the next iteration. try { queueItem = _actionQueue.Take(); } catch (InvalidOperationException) { // when we shut down, we expect this exception is the queue is complete; but // if not complete... if (!_actionQueue.IsCompleted) { throw; } } if (queueItem != null) { bool doThreaded = _actionThreading && !string.IsNullOrWhiteSpace(queueItem.Action.TaskId); if (doThreaded) { try { // create a task item without a real task at this point; we'll set it once we know // we can add it successfully (and no dups) ActionTaskItem taskItem = new ActionTaskItem(queueItem, null, new CancellationTokenSource()); if (!_actionTasks.TryAdd(queueItem.Action.TaskId, taskItem)) { Error($"Duplicate threadId found; ignoring action. ID: {queueItem.Action.TaskId.ToString()}"); continue; } else { Log($"Adding thread id: {queueItem.Action.TaskId.ToString()}"); } // run the task, saving "t" as this task; if we did Run and Continue in one statement, // the returned task would be the Continue task and would be waiting Task t = Task.Run(() => Process(queueItem, taskItem.CancellationTokenSource)); t.ContinueWith(task => { if (task.IsFaulted) { Error(task.Exception.ToString()); } ActionTaskItem tempTaskItem; if (!_actionTasks.TryRemove(queueItem.Action.TaskId, out tempTaskItem)) { Error($"Unable to remove action thread by thread id: {queueItem.Action.TaskId.ToString()}"); } else { Log($"Removing thread id: {queueItem.Action.TaskId.ToString()}"); } }); // not that the task is running, set the task item Task taskItem.Task = t; } finally { } } else { Process(queueItem, cancelSource: null); } } } }
private void FileWatcher_Change(object sender, FileSystemEventArgs e) { var now = DateTimeOffset.Now; try { if (this.IsRunning == false) { return; } var fw = (FileSystemWatcher)sender; Action <ListViewItem> actionToEnqueue = null; if (File.Exists(e.FullPath)) { var re = e as RenamedEventArgs; if (re == null) { switch (e.ChangeType) { case WatcherChangeTypes.Changed: actionToEnqueue = this.CreateFileChangedAction(e); break; case WatcherChangeTypes.Created: if (Directory.Exists(e.FullPath)) { actionToEnqueue = this.CreateFileCreatedAction(e); } break; case WatcherChangeTypes.Deleted: actionToEnqueue = this.CreateFileDeletedAction(e); break; } } else { // rename operation actionToEnqueue = this.CreateFileRenamedAction(re); } } if (actionToEnqueue != null) { var newActionItem = new ActionQueueItem(e.ChangeType, e.FullPath, now, actionToEnqueue); // find similar / existing queue item var existingActionItem = this._ACTION_QUEUE .FirstOrDefault(i => newActionItem.Equals(i)); if (existingActionItem == null) { // add if does not exist in current queue this._ACTION_QUEUE .Add(newActionItem); } } } catch { //TODO log } }