public void ExecuteScriptJob(JobSchedule job) { var activationStatus = this.jobScheduleRepository.StartJob(job); var workspace = new RelativityWorkspace() { WorkspaceId = job.WorkspaceId }; if (activationStatus == JobActivationStatus.Started) { try { Logger.Trace($"Executing job {job.Id}"); RelativityHelper.InWorkspace( (client, ws) => { this.ExecuteJobInWorkspace(client, job, ws); }, workspace, this.relativityClient.GetRelativityClient()); } catch (Exception ex) { Logger.Warn(ex, $"Execution of job {job.Id} failed"); job.CurrentJobHistory.ResultText = "Exception: " + ex.ToString(); job.CurrentJobHistory.HasError = true; } finally { this.jobScheduleRepository.FinishJob(job); } } }
private RelativityScript GetScript(int scriptArtifactId, RelativityWorkspace workspace) { DTOs.RelativityScript scriptArtifact; // try/catch block needed for R96; see https://devhelp.relativity.com/t/4525/17 try { scriptArtifact = this.RelativityClient.Repositories.RelativityScript.ReadSingle(scriptArtifactId); } catch (APIException ex) when(ex.Message == "Unable to cast object of type 'System.Int32' to type 'System.String'.") { scriptArtifact = this.RelativityClient.Repositories.RelativityScript.Query( new DTOs.Query <DTOs.RelativityScript> { Condition = new TextCondition( "Artifact ID", TextConditionEnum.In, new[] { scriptArtifactId.ToString() }), Fields = DTOs.FieldValue.AllFields }).GetResults().Single(); } return(new RelativityScript() { RelativityScriptId = scriptArtifact.ArtifactID, Name = scriptArtifact.Name, Description = scriptArtifact.Description, WorkspaceId = workspace.WorkspaceId }); }
/// <summary> /// Runs the relativity script job /// </summary> /// <param name="client">the relativity client</param> /// <param name="job">the scheduled job</param> /// <param name="workspace">the workspace we exect to execute the script in</param> private void ExecuteJobInWorkspace(IRSAPIClient client, JobSchedule job, RelativityWorkspace workspace) { var inputs = this.jobScheduleRepository.GetJobInputs(job); Logger.Trace($"found ${inputs.Count} inputs for job ${job.Id}"); // new relativity script pass in artifact id var maxAttempts = 3; var attempts = 0; RelativityScriptResult scriptResult = null; do { attempts++; try { var script = new kCura.Relativity.Client.DTOs.RelativityScript(job.RelativityScriptId); var scriptInputs = inputs.Select(i => new RelativityScriptInput(i.InputName, i.InputValue)).ToList(); scriptResult = this.relativityScriptRepository.ExecuteRelativityScript(script, scriptInputs, workspace); } catch (Exception ex) { if (attempts < maxAttempts) { Task.Delay(1000 * attempts); Logger.Info($"Attempt ${attempts} failed for job ${job.Id} with exception ${ex.Message}"); continue; } else { throw ex; } } } while (attempts < maxAttempts); if (scriptResult != null) { job.CurrentJobHistory.HasError = !scriptResult.Success; job.CurrentJobHistory.ResultText = scriptResult.Message; if (job.CurrentJobHistory.HasError) { Logger.Info($"Job {job.Id} failed with result {scriptResult.Message}"); } } else { job.CurrentJobHistory.HasError = true; job.CurrentJobHistory.ResultText = "scriptResult Not initialized"; } }
public List <Models.ScriptInput> GetScriptInputs(RelativityScript script, RelativityWorkspace workspace) { return(this.InWorkspace( (client, ws) => { var scriptArtifact = new DTOs.RelativityScript(script.RelativityScriptId); var fields = client.Repositories.RelativityScript.GetRelativityScriptInputs(scriptArtifact); return fields.Select(f => new Models.ScriptInput() { Name = f.Name, InputId = f.Id, InputType = f.InputType.ToString(), IsRequired = f.IsRequired }).ToList(); }, workspace)); }
private IEnumerable <RelativityScriptModel> GetScriptList(RelativityWorkspace relativityWorkspace) { var jobSchedules = this.JobScheduleRepository.GetJobSchedules(relativityWorkspace).Select(js => js.ConvertUtcToLocal()).GroupBy(s => s.RelativityScriptId).ToDictionary(s => s.Key, s => s); var scripts = this.RelativityScriptRepository.GetRelativityScripts(relativityWorkspace); foreach (var script in scripts) { if (jobSchedules.ContainsKey(script.RelativityScriptId)) { yield return(new RelativityScriptModel(script, relativityWorkspace, jobSchedules[script.RelativityScriptId])); } else { yield return(new RelativityScriptModel(script, relativityWorkspace)); } } }
private List <RelativityScript> GetAllScripts(IRSAPIClient client, RelativityWorkspace workspace) { var scriptArtifactResults = this.RelativityClient.Repositories.RelativityScript.Query(new DTOs.Query <DTOs.RelativityScript>() { Fields = RelativityHelper.FieldList("Name", "Description") }); if (scriptArtifactResults.Success) { return(scriptArtifactResults.Results.Select(r => new RelativityScript() { Name = r.Artifact.Name, Description = r.Artifact.Description, WorkspaceId = workspace.WorkspaceId, RelativityScriptId = r.Artifact.ArtifactID }).ToList()); } return(new List <RelativityScript>()); }
public static T InWorkspace <T>(Func <IRSAPIClient, RelativityWorkspace, T> workspaceAction, RelativityWorkspace workspace, IRSAPIClient relativityClient) { int currentWorkspaceId = relativityClient.APIOptions.WorkspaceID; try { relativityClient.APIOptions.WorkspaceID = workspace.WorkspaceId; var result = workspaceAction(relativityClient, workspace); return(result); } finally { relativityClient.APIOptions.WorkspaceID = currentWorkspaceId; } }
public List <RelativityScript> GetRelativityScripts(RelativityWorkspace workspace) { var workspaceScripts = this.InWorkspace <List <RelativityScript> >(this.GetAllScripts, workspace); return(workspaceScripts); }
private JobScheduleModel NewJobScheduleModel(RelativityScript relativityScript, RelativityWorkspace relativityWorkspace) { var jobSchedule = new JobSchedule() { Name = $"{relativityWorkspace.Name} - {relativityScript.Name}", RelativityScriptId = relativityScript.RelativityScriptId, WorkspaceId = relativityWorkspace.WorkspaceId }; var inputs = this.RelativityScriptRepository .GetScriptInputs(relativityScript, relativityWorkspace) .Select(i => new JobScriptInputModel(i)) .ToList(); var jobScheduleModel = new JobScheduleModel() { JobSchedule = jobSchedule, RelativityWorkspace = relativityWorkspace, RelativityScript = relativityScript, JobScriptInputs = inputs, IsNew = true }; return(jobScheduleModel); }
/// <summary> /// Returns a list of script input models /// </summary> /// <param name="relativityScript">the reference to the relativity script</param> /// <param name="relativityWorkspace">the reference to the expected execution workspace</param> /// <returns>a list of script input models</returns> private List <JobScriptInputModel> GetScriptInputs(RelativityScript relativityScript, RelativityWorkspace relativityWorkspace) { var inputs = this.RelativityScriptRepository.GetScriptInputs(relativityScript, relativityWorkspace); return(inputs.Select(i => new JobScriptInputModel() { InputId = i.InputId, InputName = i.Name, InputType = i.InputType, IsRequired = i.IsRequired }).ToList()); }
public List <JobSchedule> GetJobSchedules(RelativityWorkspace relativityWorkspace) { return(this.DataContext.JobSchedule .Where(s => s.WorkspaceId == relativityWorkspace.WorkspaceId) .ToList()); }
public RelativityScriptModel(RelativityScript relativityScript, RelativityWorkspace relativityWorkspace) { this.RelativityScript = relativityScript; this.RelativityWorkspace = relativityWorkspace; }
public RelativityScriptModel(RelativityScript relativityScript, RelativityWorkspace relativityWorkspace, IEnumerable <JobSchedule> jobSchedules) : this(relativityScript, relativityWorkspace) { this.JobSchedules = jobSchedules.OrderBy(s => s.NextExecutionTime).ToList(); }
public static void InWorkspace(Action <IRSAPIClient, RelativityWorkspace> workspaceAction, RelativityWorkspace workspace, IRSAPIClient relativityClient) { int currentWorkspaceId = relativityClient.APIOptions.WorkspaceID; try { relativityClient.APIOptions.WorkspaceID = workspace.WorkspaceId; workspaceAction(relativityClient, workspace); } finally { relativityClient.APIOptions.WorkspaceID = currentWorkspaceId; } }
private T InWorkspace <T>(Func <IRSAPIClient, RelativityWorkspace, T> action, RelativityWorkspace workspace) { return(RelativityHelper.InWorkspace(action, workspace, this.RelativityClient)); }
public RelativityScriptResult ExecuteRelativityScript(DTOs.RelativityScript script, List <RelativityScriptInput> inputs, RelativityWorkspace workspace) { if (script == null) { throw new ArgumentNullException("script"); } if (script.ArtifactID == 0) { throw new ArgumentNullException("script.ArtfactID"); } return(this.InWorkspace( (client, ws) => client.Repositories.RelativityScript.ExecuteRelativityScript(script, inputs), workspace)); }
public RelativityScript GetRelativityScript(RelativityWorkspace workspace, int scriptArtifactId) { return(this.InWorkspace( (client, ws) => this.GetScript(scriptArtifactId, ws), workspace)); }