示例#1
0
        public async Task <RegisterResult> RegistHandleAsync(IScriptHandle handler,
                                                             CancellationToken cancellationToken)
        {
            var mappedEntity = _mapFunction(new ScriptVersions
            {
                ExecutedTime = DateTime.UtcNow,
                ScriptId     = handler.ScriptId
            });

            try
            {
                await _mongoDatabase
                .GetCollection <TTracked>(_collectionName)
                .InsertManyAsync(new[]
                {
                    mappedEntity
                },
                                 cancellationToken: cancellationToken);

                return(new RegisterResult(RegisterResultStatus.Success));
            }
            catch (Exception ex)
            {
                return(new RegisterResult(RegisterResultStatus.Failure, ex.Message));
            }
        }
 private async static Task <string> GetFileContentAsync(IScriptHandle script, CancellationToken cancellationToken)
 {
     using (var stream = await script.GetReadStreamAsync(cancellationToken))
     {
         using (var reader = new StreamReader(stream, Encoding.UTF8))
         {
             return(reader.ReadToEnd());
         }
     }
 }
        public async Task <ExecutionResult> ExecuteAsync(IScriptHandle script, CancellationToken cancellationToken)
        {
            var engine = _wrappedEngines.FirstOrDefault(t => t.CanExecute(script));

            if (engine == null)
            {
                var scriptId = script.ScriptId;
                var engines  = string.Join(",", _wrappedEngines
                                           .Select(t => t.Engine.GetType().Name));

                throw new InvalidOperationException(
                          $"Script {scriptId} is not supported by these engines: [{engines}]");
            }

            return(await engine.Engine.ExecuteAsync(script, cancellationToken));
        }
        public async Task <ExecutionResult> ExecuteAsync(IScriptHandle script,
                                                         CancellationToken cancellationToken)
        {
            if (script == null)
            {
                throw new ArgumentNullException(nameof(script));
            }

            var sqlScript = await GetFileContentAsync(script, cancellationToken);

            var transaction = default(TDbTransaction);

            try
            {
                await _dbConnection.OpenAsync();

                transaction = BeginTransaction(_dbConnection);
                using (var command = CreateCommand(sqlScript, _dbConnection, transaction))
                {
                    await command.ExecuteNonQueryAsync(cancellationToken);
                }
                transaction.Commit();

                return(new ExecutionResult(ExecutionResultStatus.Success));
            }
            catch (Exception e)
            {
                // Exception happened, roll-back everything.
                if (transaction != default(TDbTransaction))
                {
                    transaction.Rollback();
                }
                return(new ExecutionResult(ExecutionResultStatus.Errors, e.Message));
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Dispose();
                }
                _dbConnection.Close();
                _dbConnection.Dispose();
            }
        }
示例#5
0
        public async Task <ExecutionResult> ExecuteAsync(IScriptHandle script, CancellationToken cancellationToken)
        {
            if (script == null)
            {
                throw new ArgumentNullException(nameof(script));
            }

            var compiledAssembly = await _assemblyProvider.CompileCodeAsync(script, cancellationToken);

            var entryPoint = compiledAssembly
                             .Types
                             .SingleOrDefault(t => typeof(IScriptEntryPoint).IsAssignableFrom(t));

            if (entryPoint == null)
            {
                return(new ExecutionResult(ExecutionResultStatus.Warnings, "No entry point found!"));
            }

            if (CanSkipService(entryPoint))
            {
                return(new ExecutionResult(ExecutionResultStatus.Skipped));
            }
            OnBeforeExecution(entryPoint);

            var service = (IScriptEntryPoint)GetService(entryPoint);

            try
            {
                await service.ExecuteAsync(cancellationToken);
            }
            catch (AggregateException exception)
            {
                return(new ExecutionResult(ExecutionResultStatus.Errors, exception
                                           .InnerExceptions
                                           .Select(t => t.Message)));
            }

            return(new ExecutionResult(ExecutionResultStatus.Success));
        }
示例#6
0
 public bool CanExecute(IScriptHandle script)
 {
     return(_canExecute(script));
 }
示例#7
0
        public async Task <IAssembly> CompileCodeAsync(IScriptHandle codeFile, CancellationToken cancellationToken)
        {
            var fileContents = await GetFileContentAsync(codeFile, cancellationToken);

            return(await _assemblyCompiler.CompileCodeAsync(fileContents, cancellationToken));
        }