private int UnloadExistingScripts(IEnumerable <ScriptRunnerInfo> runnerInfos) { var count = 0; foreach (var runnerInfo in runnerInfos) { try { Cancel(runnerInfo); var writerProxy = new NotifierProxy(ServerEvents, runnerInfo.ScriptId); writerProxy.SendScriptExecutionResults(new ScriptExecutionResult { Status = ScriptStatus.Cancelled }); count++; } catch (Exception ex) { Log.Error(ex); } } return(count); }
public ScriptExecutionResult RunAsync(string mainScript, List <string> scripts, List <string> references, NotifierProxy writerProxy) { this.mainScript = mainScript; this.scripts = scripts; this.references = references; var writer = new ConsoleWriter(writerProxy); Console.SetOut(writer); tokenSource = new CancellationTokenSource(); var result = runner.ExecuteAsync(mainScript, scripts, references, writerProxy, tokenSource.Token); /*TextWriter tmp = Console.Out; * task.ContinueWith((_) => * { * Console.SetOut(tmp); * writer.Close(); * });*/ return(result); }
public ScriptExecutionResult Run(string mainScript, List <string> scripts, List <string> references, NotifierProxy writerProxy) { this.mainScript = mainScript; this.scripts = scripts; this.references = references; var result = new ScriptExecutionResult(); var tmp = Console.Out; using (var writer = new ConsoleWriter(writerProxy)) { Console.SetOut(writer); try { var task = runner.Execute(mainScript, scripts, references); //Session.SetScriptTask(task); result = task.Result; //ServerEvents.NotifySession(Session.GetSessionId(), result); } catch (Exception e) { result.Exception = e; } Console.SetOut(tmp); } return(result); }
public object Any(RunScript request) { if (request.ScriptId == null) { throw new ArgumentException("ScriptId"); } AppData.AssertNoIllegalTokens(request.MainSource); AppData.AssertNoIllegalTokens(request.Sources.ToArray()); var result = new ScriptExecutionResult(); var existingUserScripts = GetExistingActiveUserScripts(); var runnerInfo = LocalCache.GetScriptRunnerInfo(request.ScriptId); //stop script if run if (runnerInfo?.ScriptDomain != null) { var scriptStatus = runnerInfo.DomainWrapper.GetScriptStatus(); if (request.ForceRun || scriptStatus != ScriptStatus.PrepareToRun && scriptStatus != ScriptStatus.Running) { Cancel(runnerInfo); } else { result.Status = ScriptStatus.AnotherScriptExecuting; return(new RunScriptResponse { Result = result }); } } List <AssemblyReference> normalizedReferences; var addedReferences = AddReferencesFromPackages(request.References, request.PackagesConfig, out normalizedReferences); var evidence = new Evidence(AppDomain.CurrentDomain.Evidence); var setup = new AppDomainSetup { PrivateBinPath = Path.Combine(VirtualFiles.RootDirectory.RealPath, "bin"), ApplicationBase = VirtualFiles.RootDirectory.RealPath }; var domain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), evidence, setup); var asm = typeof(DomainWrapper).Assembly.FullName; var type = typeof(DomainWrapper).FullName; var wrapper = (DomainWrapper)domain.CreateInstanceAndUnwrap(asm, type); wrapper.ScriptId = request.ScriptId; var writerProxy = new NotifierProxy(ServerEvents, request.ScriptId); result = wrapper.RunAsync(request.MainSource, request.Sources, addedReferences.Select(r => r.Path).ToList(), writerProxy); LocalCache.SetScriptRunnerInfo(request.ScriptId, new ScriptRunnerInfo { ScriptId = request.ScriptId, SessionId = base.Request.GetPermanentSessionId(), CreatedDate = DateTime.UtcNow, ScriptDomain = domain, DomainWrapper = wrapper, }); var unloadOldUserScripts = existingUserScripts .Where(x => x.ScriptId != request.ScriptId && DateTime.UtcNow - x.CreatedDate > TimeSpan.FromMinutes(1)); var scriptsRemoved = UnloadExistingScripts(unloadOldUserScripts); return(new RunScriptResponse { Result = result, References = normalizedReferences, ScriptsRemoved = scriptsRemoved, }); }
public ConsoleWriter(NotifierProxy proxy) { this.proxy = proxy; }
public object Any(RunEmbedScript request) { if (request.GistHash == null) { throw new ArgumentException("GistHash"); } AppData.AssertNoIllegalTokens(request.MainSource); AppData.AssertNoIllegalTokens(request.Sources.ToArray()); var result = new EmbedScriptExecutionResult(); var codeHash = GetSourceCodeHash(request); if (!request.NoCache) { var mr = AppData.GetMemoizedResult(codeHash); if (mr != null) { result = mr.Result; return(new RunEmbedScriptResponse { Result = result }); } } List <AssemblyReference> normalizedReferences; var addedReferences = AddReferencesFromPackages(request.References, request.Packages, out normalizedReferences); //Create domain and run script var evidence = new Evidence(AppDomain.CurrentDomain.Evidence); var setup = new AppDomainSetup { PrivateBinPath = Path.Combine(VirtualFiles.RootDirectory.RealPath, "bin"), ApplicationBase = VirtualFiles.RootDirectory.RealPath }; var domain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), evidence, setup); var asm = typeof(DomainWrapper).Assembly.FullName; var type = typeof(DomainWrapper).FullName; var wrapper = (DomainWrapper)domain.CreateInstanceAndUnwrap(asm, type); wrapper.ScriptId = request.ScriptId; var writerProxy = new NotifierProxy(ServerEvents, request.ScriptId); var info = new ScriptRunnerInfo { ScriptId = request.ScriptId, ScriptDomain = domain, DomainWrapper = wrapper }; Cache.Set(request.ScriptId, info); var lockEvt = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(_ => { try { var sr = wrapper.Run(request.MainSource, request.Sources, addedReferences.Select(r => r.Path).ToList(), writerProxy); result.Exception = sr.Exception; result.Errors = sr.Errors; //get json of last variable if (sr.Variables != null && sr.Variables.Count > 0) { var value = wrapper.GetVariableValue(sr.Variables[sr.Variables.Count - 1].Name); result.LastVariableJson = ScriptUtils.ToJson(value); } } finally { lockEvt.Set(); } }); lockEvt.WaitOne(); AppData.SetMemoizedResult(new MemoizedResult { CodeHash = codeHash, Result = result }); //Unload appdomain only in synchroneous version //AppDomain.Unload(domain); return(new RunEmbedScriptResponse { Result = result, References = normalizedReferences }); }