public async Task <ProcessModel> RunScriptById(int id, ScriptParamArray paramArray)
        {
            Script script = await _context.Scripts.FindAsync(id);

            if (script == null)
            {
                throw new AppException($"Script with id {id} not found in database");
            }

            ScriptType scriptType = await _scriptService.GetTypeByIdAsync(script.Type);

            string scriptFilePath = CreateScriptFileWithContent(script.Content, scriptType.FileExtension);

            string processArgs = PrepareScriptArguments(scriptType, scriptFilePath, paramArray);

            _logger.LogDebug("Process arguments : {args}", processArgs);

            try
            {
                ProcessModel processModel = await RunProcessAsync(processArgs, scriptType.Runner);

                _logger.LogDebug("Process result : {@model}", processModel);
                return(processModel);
            }
            finally {
                await DeleteScriptFileAsync(scriptFilePath);
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> GetScripTypeById(int Id)
        {
            _logger.LogInformation("Get script type with id : {id}", Id);
            try
            {
                ScriptType scriptType = await _scriptService.GetTypeByIdAsync(Id);

                var model = _mapper.Map <ScriptTypeModel>(scriptType);
                _logger.LogInformation("Retrieved script type : {scriptName}", model.Name);

                return(Ok(model));
            }
            catch (AppException ex)
            {
                _logger.LogWarning(ex, $"Failed find script type with id {Id}");
                return(BadRequest(new { message = ex.Message }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Fatal failure while getting script type with id {Id}");
                return(StatusCode(StatusCodes.Status500InternalServerError, new { message = "Fatal internal error. Please contact administrator" }));
            }
        }