public JsonResult StartJob(string filename)//or maybe you've already uploaded and have a fileId instead { var jobState = new ResumableJobState { CurrentIteration = 0, InputFile = filename, OutputFile = filename + "_output.csv" }; var job = new LongRunningJob { State = "Running", ResumableJobState = jobState }; _context.ResumableJobStates.Add(jobState); _context.LongRunningJobs.Add(job); var result = _context.SaveChanges(); if (result == 0) throw new Exception("Error saving to database"); _jobinatorService.StartOrResume(job); return Json(job); }
public void StartOrResume(LongRunningJob job) { Task.Run(() => { using (var inputFile = System.IO.File.OpenRead(_filePath + job.ResumableJobState.InputFile)) using (var outputFile = System.IO.File.OpenWrite(_filePath + job.ResumableJobState.OutputFile)) { inputFile.Position = job.ResumableJobState.CurrentIteration; for (int i = (int)inputFile.Position; i < inputFile.Length; i++)//casting long to int, what could possibly go wrong? { if (job.State == "Input Required" && job.ResumableJobState.RequiredInputType != null) {//We needed input and received it //You might want to do a switch..case on the various inputs, and branch into different functions if (job.ResumableJobState.RequiredInputType.InputName == "6*7") if (job.ResumableJobState.RequiredInputType.IntValue.Value == 42) break;//Pass Go, collect 42 dollars; } outputFile.WriteByte((byte)inputFile.ReadByte());//Don't try this at home! job.ResumableJobState.CurrentIteration = i;//or row, or line, or however you delimit processing job.ResumableJobState.InputFileBufferReadPosition = inputFile.Position;//or something if (i % 7 == 0) job.ResumableJobState.RequiredInputType = _context.RequiredInputTypes.First(t => t.InputName == "Row 7 Input"); if (i % 42 == 0) job.ResumableJobState.RequiredInputType = _context.RequiredInputTypes.First(t => t.InputName == "6*7"); if (job.ResumableJobState.RequiredInputType != null) job.State = "Input Required"; _context.SaveChanges(); if (job.State != "Running") return; } job.State = "Completed"; _context.SaveChanges(); } }); return; }