public static string GetActiveWindowFile() { var id = GetForegroundWindow().ToInt64(); if (_files.TryGetValue(id, out var file)) { return(file); } _files.Clear(); foreach (var p in Process.GetProcesses()) { using (p) try { _files[p.MainWindowHandle.ToInt64()] = p.MainModule?.FileName ?? string.Empty; } catch (Exception) { _files[p.MainWindowHandle.ToInt64()] = string.Empty; } } if (_files.TryGetValue(id, out file)) { return(file); } return(string.Empty); }
protected void CleanupDatabase(string db, bool skipIfActive) { using (ResourcesStoresCache.WithAllLocks()) { DateTime time; Task <DocumentDatabase> databaseTask; if (ResourcesStoresCache.TryGetValue(db, out databaseTask) == false) { databaseLastRecentlyUsed.TryRemove(db, out time); return; } if (databaseTask.Status == TaskStatus.Faulted || databaseTask.Status == TaskStatus.Canceled) { databaseLastRecentlyUsed.TryRemove(db, out time); ResourcesStoresCache.TryRemove(db, out databaseTask); return; } if (databaseTask.Status != TaskStatus.RanToCompletion) { return; // still starting up } var database = databaseTask.Result; if (skipIfActive && (SystemTime.UtcNow - database.WorkContext.LastWorkTime).TotalMinutes < 5) { // this document might not be actively working with user, but it is actively doing indexes, we will // wait with unloading this database until it hasn't done indexing for a while. // This prevent us from shutting down big databases that have been left alone to do indexing work. return; } try { database.Dispose(); } catch (Exception e) { logger.ErrorException("Could not cleanup tenant database: " + db, e); return; } databaseLastRecentlyUsed.TryRemove(db, out time); ResourcesStoresCache.TryRemove(db, out databaseTask); } }
public static string GetActiveWindowName() { var id = GetForegroundWindow().ToInt64(); if (_names.TryGetValue(id, out var name)) { return(name); } _names.Clear(); foreach (var p in Process.GetProcesses()) { using (p) _names[p.MainWindowHandle.ToInt64()] = p.MainWindowTitle; } if (_names.TryGetValue(id, out name)) { return(name); } return(string.Empty); }
public void Cleanup(string db, bool skipIfActive, Func <TResource, bool> shouldSkip = null) { using (ResourcesStoresCache.WithAllLocks()) { DateTime time; Task <TResource> databaseTask; if (ResourcesStoresCache.TryGetValue(db, out databaseTask) == false) { LastRecentlyUsed.TryRemove(db, out time); return; } if (databaseTask.Status == TaskStatus.Faulted || databaseTask.Status == TaskStatus.Canceled) { LastRecentlyUsed.TryRemove(db, out time); ResourcesStoresCache.TryRemove(db, out databaseTask); return; } if (databaseTask.Status != TaskStatus.RanToCompletion) { return; // still starting up } var database = databaseTask.Result; if ((skipIfActive && (SystemTime.UtcNow - LastWork(database)).TotalMinutes < 10) || (shouldSkip != null && shouldSkip(database))) { // this document might not be actively working with user, but it is actively doing indexes, we will // wait with unloading this database until it hasn't done indexing for a while. // This prevent us from shutting down big databases that have been left alone to do indexing work. return; } try { database.Dispose(); } catch (Exception e) { Logger.ErrorException("Could not cleanup tenant database: " + db, e); return; } LastRecentlyUsed.TryRemove(db, out time); ResourcesStoresCache.TryRemove(db, out databaseTask); var onDatabaseCleanupOccured = CleanupOccured; if (onDatabaseCleanupOccured != null) { onDatabaseCleanupOccured(db); } } }
protected TConnectionState GetOrAddConnectionState(string name, string watchCommand, string unwatchCommand, Action afterConnection, Action beforeDisconnect, string value) { var counter = Counters.GetOrAdd(name, s => { Func <Task> onZero = () => { beforeDisconnect(); Counters.Remove(name); return(Send(unwatchCommand, value)); }; Func <TConnectionState, Task> ensureConnection = existingConnectionState => { TConnectionState _; if (Counters.TryGetValue(name, out _)) { return(_.Task); } Counters.GetOrAdd(name, x => existingConnectionState); return(AfterConnection(() => { afterConnection(); return Send(watchCommand, value); })); }; var counterSubscriptionTask = AfterConnection(() => { afterConnection(); return(Send(watchCommand, value)); }); return(CreateTConnectionState(onZero, ensureConnection, counterSubscriptionTask)); }); return(counter); }
private void ExecuteTimer(object state) { var span = state.ToString(); Tuple <Timer, ConcurrentSet <IRepeatedAction> > tuple; if (timers.TryGetValue(span, out tuple) == false) { return; } foreach (var repeatedAction in tuple.Item2) { if (repeatedAction.IsValid == false) { tuple.Item2.TryRemove(repeatedAction); } try { repeatedAction.Execute(); } catch (Exception e) { logger.ErrorException("Could not execute repeated task", e); } } if (tuple.Item2.Count != 0) { return; } if (timers.TryRemove(span, out tuple) == false) { return; } tuple.Item1.Dispose(); }
public void Cleanup(string resource, TimeSpan?skipIfActiveInDuration, Func <TResource, bool> shouldSkip = null, DocumentChangeTypes notificationType = DocumentChangeTypes.None) { if (Cleanups.TryAdd(resource, new ManualResetEvent(false)) == false) { return; } try { using (ResourcesStoresCache.WithAllLocks()) { DateTime time; Task <TResource> resourceTask; if (ResourcesStoresCache.TryGetValue(resource, out resourceTask) == false) { LastRecentlyUsed.TryRemove(resource, out time); return; } if (resourceTask.Status == TaskStatus.Faulted || resourceTask.Status == TaskStatus.Canceled) { LastRecentlyUsed.TryRemove(resource, out time); ResourcesStoresCache.TryRemove(resource, out resourceTask); return; } if (resourceTask.Status != TaskStatus.RanToCompletion) { return; // still starting up } var database = resourceTask.Result; if ((skipIfActiveInDuration != null && (SystemTime.UtcNow - LastWork(database)) < skipIfActiveInDuration) || (shouldSkip != null && shouldSkip(database))) { // this document might not be actively working with user, but it is actively doing indexes, we will // wait with unloading this database until it hasn't done indexing for a while. // This prevent us from shutting down big databases that have been left alone to do indexing work. return; } try { database.Dispose(); } catch (Exception e) { Logger.ErrorException("Could not cleanup tenant database: " + resource, e); return; } LastRecentlyUsed.TryRemove(resource, out time); ResourcesStoresCache.TryRemove(resource, out resourceTask); if (notificationType == DocumentChangeTypes.Delete) { TransportState transportState; ResourseTransportStates.TryRemove(resource, out transportState); if (transportState != null) { transportState.Dispose(); } } var onResourceCleanupOccured = CleanupOccured; if (onResourceCleanupOccured != null) { onResourceCleanupOccured(resource); } } } finally { ManualResetEvent cleanupLock; if (Cleanups.TryRemove(resource, out cleanupLock)) { cleanupLock.Set(); } } }