Exemple #1
0
        protected async override Task<NodeResultStatus> PerformExecuteAsync(IExecutionContext<T> context)
        {
            if (!string.IsNullOrEmpty(ExecutedScript))
            {
                LogWriter.Debug("ExecutedScript exists, running this script.");
                var result = new NodeResult{IsSuccess = true};

                using (var scope = new ManagerScope())
                {
                    SetScriptIdIfEmpty();

                    var options = new ClearScript.Manager.ExecutionOptions
                    {
                        HostObjects = SetupHostObjects(context, result),
                        HostTypes = SetupHostTypes()
                    };

                    await scope.RuntimeManager.ExecuteAsync(ScriptId, ExecutedScript, options);

                    if (!result.IsSuccess)
                    {
                        return NodeResultStatus.Failed;
                    }
                }
            }
            LogWriter.Debug("ExecutedScript doesn't exist.");

            return NodeResultStatus.Succeeded;
        }
Exemple #2
0
        public override async Task<bool> ShouldExecuteAsync(IExecutionContext<T> context)
        {
            if (string.IsNullOrEmpty(ShouldExecuteScript)) return true;
            
            var result = new ShouldExecuteResult { ShouldExecute = true };

            using (var scope = new ManagerScope())
            {
                SetScriptIdIfEmpty();

                var options = new ClearScript.Manager.ExecutionOptions
                {
                    HostObjects = SetupHostObjects(context, result),
                    HostTypes = SetupHostTypes()
                };

                await scope.RuntimeManager.ExecuteAsync(ScriptId, ShouldExecuteScript, options);

                return result.ShouldExecute;
            }
        }
        public async Task<V8ScriptEngine> ExecuteAsync(IEnumerable<IncludeScript> scripts, ExecutionOptions options = null)
        {
            var scriptList = PrecheckScripts(scripts);
            if (scriptList == null)
                return null;

            foreach (var includeScript in scriptList)
            {
                if (string.IsNullOrEmpty(includeScript.ScriptId) && !string.IsNullOrEmpty(includeScript.Code))
                {
                    includeScript.ScriptId = includeScript.Code.GetHashCode().ToString(CultureInfo.InvariantCulture);
                }
            }

            if (options == null)
                options = new ExecutionOptions();

            var configAction = new Action<V8ScriptEngine>(engine => engine.ApplyOptions(options));

            return await ExecuteAsync(scriptList, configAction, options);
        }
 public async Task<V8ScriptEngine> ExecuteAsync(string scriptId, string code, ExecutionOptions options = null)
 {
     return await ExecuteAsync(new[] {new IncludeScript {ScriptId = scriptId, Code = code}}, options);
 }
        public async Task<V8ScriptEngine> ExecuteAsync(IEnumerable<IncludeScript> scripts, Action<V8ScriptEngine> configAction, ExecutionOptions options = null)
        {
            var scriptList = PrecheckScripts(scripts);
            if (scriptList == null)
                return null;

            if (options == null)
                options = new ExecutionOptions();

            IEnumerable<V8Script> compiledScripts = scriptList.Select(x => _scriptCompiler.Compile(x, options.AddToCache, options.CacheExpirationSeconds));

            GetEngine();

            if (AddConsoleReference)
            {
                _scriptEngine.AddHostType("Console", typeof (Console));
            }

            RequireManager.BuildRequirer(_scriptCompiler, _scriptEngine);

            if (configAction != null)
            {
                configAction(_scriptEngine);
            }

            if (options.Scripts != null)
            {
                foreach (var script in options.Scripts)
                {
                    var compiledInclude = _scriptCompiler.Compile(script, options.AddToCache);
                    if (compiledInclude != null)
                    {
                        _scriptEngine.Execute(compiledInclude);
                    }
                }
            }

            foreach (var compiledScript in compiledScripts)
            {
                //Only create a wrapping task if the script has a timeout.
                CancellationToken cancellationToken;
                if (TryCreateCancellationToken(out cancellationToken))
                {
                    using (cancellationToken.Register(_scriptEngine.Interrupt))
                    {
                        try
                        {
                            V8Script script = compiledScript;
                            await Task.Run(() => _scriptEngine.Execute(script), cancellationToken).ConfigureAwait(false);
                        }
                        catch (ScriptInterruptedException ex)
                        {
                            var newEx = new ScriptInterruptedException(
                                "Script interruption occurred, this often indicates a script timeout.  Examine the data and inner exception for more information.", ex);
                            newEx.Data.Add("Timeout", _settings.ScriptTimeoutMilliSeconds);
                            newEx.Data.Add("ScriptId", compiledScript.Name);

                            throw newEx;
                        }
                    }
                }
                else
                {
                    _scriptEngine.Execute(compiledScript);
                }
            }

            return _scriptEngine;

        }
 public async Task<V8ScriptEngine> ExecuteAsync(string scriptId, string code, Action<V8ScriptEngine> configAction, ExecutionOptions options = null)
 {
     return await ExecuteAsync(new[] {new IncludeScript {Code = code, ScriptId = scriptId}}, configAction, options);
 }
Exemple #7
0
        public async Task <V8ScriptEngine> ExecuteAsync(IEnumerable <IncludeScript> scripts, ExecutionOptions options = null)
        {
            var scriptList = PrecheckScripts(scripts);

            if (scriptList == null)
            {
                return(null);
            }

            foreach (var includeScript in scriptList)
            {
                if (string.IsNullOrEmpty(includeScript.ScriptId) && !string.IsNullOrEmpty(includeScript.Code))
                {
                    includeScript.ScriptId = includeScript.Code.GetHashCode().ToString(CultureInfo.InvariantCulture);
                }
            }

            if (options == null)
            {
                options = new ExecutionOptions();
            }

            var configAction = new Action <V8ScriptEngine>(engine => engine.ApplyOptions(options));

            return(await ExecuteAsync(scriptList, configAction, options));
        }
Exemple #8
0
 public async Task <V8ScriptEngine> ExecuteAsync(string scriptId, string code, ExecutionOptions options = null)
 {
     return(await ExecuteAsync(new[] { new IncludeScript {
                                           ScriptId = scriptId, Code = code
                                       } }, options));
 }
Exemple #9
0
        public async Task <V8ScriptEngine> ExecuteAsync(IEnumerable <IncludeScript> scripts, Action <V8ScriptEngine> configAction, ExecutionOptions options = null)
        {
            var scriptList = PrecheckScripts(scripts);

            if (scriptList == null)
            {
                return(null);
            }

            if (options == null)
            {
                options = new ExecutionOptions();
            }

            IEnumerable <V8Script> compiledScripts = scriptList.Select(x => _scriptCompiler.Compile(x, options.AddToCache, options.CacheExpirationSeconds));

            GetEngine();

            if (AddConsoleReference)
            {
                _scriptEngine.AddHostType("Console", typeof(Console));
            }

            RequireManager.BuildRequirer(_scriptCompiler, _scriptEngine);

            if (configAction != null)
            {
                configAction(_scriptEngine);
            }

            if (options.Scripts != null)
            {
                foreach (var script in options.Scripts)
                {
                    var compiledInclude = _scriptCompiler.Compile(script, options.AddToCache);
                    if (compiledInclude != null)
                    {
                        _scriptEngine.Execute(compiledInclude);
                    }
                }
            }

            foreach (var compiledScript in compiledScripts)
            {
                //Only create a wrapping task if the script has a timeout.
                CancellationToken cancellationToken;
                if (TryCreateCancellationToken(out cancellationToken))
                {
                    using (cancellationToken.Register(_scriptEngine.Interrupt))
                    {
                        try
                        {
                            V8Script script = compiledScript;
                            await Task.Run(() => _scriptEngine.Execute(script), cancellationToken).ConfigureAwait(false);
                        }
                        catch (ScriptInterruptedException ex)
                        {
                            var newEx = new ScriptInterruptedException(
                                "Script interruption occurred, this often indicates a script timeout.  Examine the data and inner exception for more information.", ex);
                            newEx.Data.Add("Timeout", _settings.ScriptTimeoutMilliSeconds);
                            newEx.Data.Add("ScriptId", compiledScript.Name);

                            throw newEx;
                        }
                    }
                }
                else
                {
                    _scriptEngine.Execute(compiledScript);
                }
            }

            return(_scriptEngine);
        }
Exemple #10
0
 public async Task <V8ScriptEngine> ExecuteAsync(string scriptId, string code, Action <V8ScriptEngine> configAction, ExecutionOptions options = null)
 {
     return(await ExecuteAsync(new[] { new IncludeScript {
                                           Code = code, ScriptId = scriptId
                                       } }, configAction, options));
 }