Ejemplo n.º 1
0
 void fsw_HandleFileEvent(string fullPath, WatcherChangeTypes changeType)
 {
     if (!isEventHandled || AllowMultipleNotifications)
     {
         Log.Info($"Action detected: {fullPath} ==> {changeType}");
         if (this.DelayAfterFileDetection != 0)
         {
             Log.Info($"Delay after action detected: {this.DelayAfterFileDetection} ms");
             Thread.Sleep(this.DelayAfterFileDetection);
         }
         if (ProgramToExecute.ToLower().StartsWith("http"))
         {
             try
             {
                 Log.Info("Using service.");
                 var result = UseService(fullPath, changeType);
                 Log.Info($"Call complete: {result.Result}");
             }
             catch (Exception ex)
             {
                 Log.Info($"Service exception: {ex}");
             }
         }
         else
         {
             UseProcess(fullPath, changeType);
         }
     }
 }
Ejemplo n.º 2
0
 public async Task <IActionResult> ExecuterScript([FromBody] ProgramToExecute programToExecute)
 {
     try
     {
         return(OkEnveloppe(await _interpretationService.Interprete(programToExecute)));
     }
     catch
     {
         return(BadRequestEnveloppe(null, ModelState));
     }
 }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public async Task <string> Interprete(ProgramToExecute program)
        {
            var logger = _contextAccessor.HttpContext.RequestServices.GetService(typeof(ILogger <InterpretationService>)) as ILogger <InterpretationService>;

            if (logger is null)
            {
                throw new InvalidProgramException("ILogger is not register correclty");
            }

            try
            {
                int timeout = 2000;

                var task = Task.Run(() =>
                {
                    using var sw = new StringWriter();

                    var interpreteur = new Interpreteur(sw);

                    interpreteur.Interprete(program.Text);

                    return(sw.ToString());
                }, CancellationToken.None);

                if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
                {
                    return(task.Result);
                }
                else
                {
                    try
                    {
                        task.Dispose();
                    }
                    catch (Exception e)
                    {
                        logger.LogError(new EventId(546, "HLHML Task Dispose Exception"), e, "Exception was throwed when try to dispose HLHML interpretation Task");
                    }

                    throw new TimeoutException("You program passed the timeout of 2 seconds...");
                }
            }
            catch (Exception e)
            {
                var modelstate = _contextAccessor.HttpContext.Features.Get <ModelStateDictionary>();

                modelstate.AddModelError("text", e.Message);

                throw;
            }
        }
Ejemplo n.º 4
0
        private async Task <string> UseService(string fullPath, WatcherChangeTypes changeType)
        {
            var reqMsg = $"{ProgramToExecute.TrimEnd('/')}&fullpath={System.Net.WebUtility.UrlEncode(fullPath)}&changetype={System.Net.WebUtility.UrlEncode(changeType.ToString())}";

            Log.Info($"Sending request: {reqMsg}");
            var client   = new HttpClient();
            var request  = new HttpRequestMessage(HttpMethod.Post, reqMsg);
            var response = await client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                Log.Info($"Success sending request");
                return(await response.Content.ReadAsStringAsync());
            }
            Log.Info($"Failed sending request: {response.Content.ReadAsStringAsync()}");
            return(string.Empty);
        }