/// <summary> /// Executes this instance. /// </summary> public override void Execute(ref ServiceAppContext context) { // Will write to the console when in debug mode, and will write to the SAC that it is hosted in. Console.WriteLine( "User {0}. Message: {1}. Parameters: {2}", Environment.UserName, ConfigurationManager.AppSettings["Message"], Implementation.Utils.Settings.Parameters); bool throwEx; if (bool.TryParse(ConfigurationManager.AppSettings["ThrowException"], out throwEx) && throwEx) { throw new TestException("Testing exception throwing"); } bool testMemory; if (bool.TryParse(ConfigurationManager.AppSettings["TestMemory"], out testMemory) && testMemory) { Random rand = new Random(); StringBuilder builder = new StringBuilder(); for (long i = 0; i < 250 * 1000L * 1000L; i++) { builder.Append((char)rand.Next(32, 127)); } } Thread.Sleep(int.Parse(ConfigurationManager.AppSettings["SleepSeconds"]) * 1000); }
/// <summary> /// Signals to start a new execution at the next available slot. /// </summary> protected void QueueExecution() { lock (_syncExecution) { if (!this.IsLoaded || this.IsStopped) { throw new InvalidOperationException("Cannot execute. Service app is not loaded or has stopped."); } bool createContext = false; switch (this.ExecutionMode) { case ExecutionMode.Default: case ExecutionMode.Idempotent: if (!this._executionContexts.Any() || this._executionContexts.All(c => c.Failed)) { createContext = true; } break; case ExecutionMode.Inline: createContext = true; break; case ExecutionMode.Concurrent: if (Settings.MaxConcurrentExecutions == 0 || this._executionContexts.Count(c => c.IsExecuting) <= Settings.MaxConcurrentExecutions) { createContext = true; } break; default: throw new NotImplementedException("Execution mode not yet implemented"); } if (createContext) { var context = new ServiceAppContext() { Failed = false, QueuedTime = DateTimeResolver.Resolve(), Guid = Guid.NewGuid().ToString(), Name = this.DisplayName }; this._executionContexts.Add(context); } } }
/// <summary> /// Serializes the message as a performance message. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> public override string SerializeAsPerformance(ServiceAppContext context) { return(serializer.Serialize(new { performance = new { name = context.Name, guid = context.Guid, startTime = context.StartTime, endTime = context.EndTime, failed = context.Failed, message = context.CustomMessage } })); }
/// <summary> /// Executes this instance using the specified execution context. /// </summary> /// <param name="context">The current execution context. In derived classes that implement this method, this object /// contains information about the current execution.</param> /// <remarks> /// <para> /// The context is passed in by ref to prevent direct invocation of this outside of the SACS.Implemetation and /// passing in a null. /// </para> /// <para> /// ServiceAppContext is sealed and cannot be instantiated outside of the SACS.Implementation assembly. To run this /// method, use <see cref="QueueExecution"/>. /// </para></remarks> public abstract void Execute(ref ServiceAppContext context);
/// <summary> /// Handles the 'tick' event of the executionTimer. /// </summary> /// <param name="state">The state.</param> private void ExecutionTimer_Tick(object state) { if (this.IsStopped) { _executionTimer.Change(Timeout.Infinite, Timeout.Infinite); return; } ServiceAppContext currentContext = null; switch (this.ExecutionMode) { case Execution.ExecutionMode.Default: case Execution.ExecutionMode.Idempotent: case Execution.ExecutionMode.Concurrent: currentContext = this._executionContexts.FirstOrDefault(c => c.CanExecute); break; case Execution.ExecutionMode.Inline: currentContext = this._executionContexts.FirstOrDefault(c => c.CanExecute && !this.IsExecuting); break; default: throw new NotImplementedException("Execution mode not yet implemented"); } if (currentContext != null) { Task executionTask = Task.Run(() => { Messages.WriteState(Enums.State.Executing); Messages.WriteDebug("Starting exection for {0}. Time: {1}", this.DisplayName, currentContext.StartTime); currentContext.StartTime = DateTimeResolver.Resolve(); Messages.WritePerformance(currentContext); try { this.Execute(ref currentContext); Messages.WriteState(Enums.State.Idle); } catch (Exception e) { currentContext.Failed = true; currentContext.CustomMessage = e.Message; this.HandleException(e, false); Messages.WriteState(Enums.State.Failed); } finally { currentContext.EndTime = DateTimeResolver.Resolve(); Messages.WriteDebug("Ended exection for {0}. Duration: {1}", this.DisplayName, currentContext.Duration); Messages.WritePerformance(currentContext); } this._executionContexts.Remove(currentContext); }); currentContext.Handle = executionTask; } this.CleanUpServiceAppContexts(); this.CheckParentProcessAlive(); }
/// <summary> /// Writes the provided string as a "performance" message to the standard output stream. /// </summary> /// <param name="context">The service app context to get the prformance information from.</param> internal static void WritePerformance(ServiceAppContext context) { Console.WriteLine(Provider.SerializeAsPerformance(context)); FileLogger.Log("Sent performance information"); }
public override void Execute(ref ServiceAppContext context) { ExecuteResolver(context); }
public override void Execute(ref ServiceAppContext context) { // do nothing }
/// <summary> /// Serializes the message as a performance message. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> public abstract string SerializeAsPerformance(ServiceAppContext context);
public DbRepository(ServiceAppContext context) { this.context = context; this.dbSet = this.context.Set <TEntity>(); }