Submit all LeafInputFiles together
This commit is contained in:
@@ -4,10 +4,10 @@ namespace LeafWeb.Core.Remote
|
||||
{
|
||||
public interface IPiscalClient
|
||||
{
|
||||
void RunLeafInputFile(PiscalLeafInputFile file);
|
||||
PiscalStatus GetLeafInputFileStatus(PiscalLeafInputFile file);
|
||||
IEnumerable<PiscalLeafOutputFile> RetrieveLeafOutput(PiscalLeafInputFile file);
|
||||
void CleanupLeafProcess(PiscalLeafInputFile file);
|
||||
string GetErrorMessage(PiscalLeafInputFile file);
|
||||
void RunLeafInput(PiscalLeafInput leafInput);
|
||||
PiscalStatus GetLeafInputStatus(PiscalLeafInput leafInput);
|
||||
IEnumerable<PiscalLeafOutputFile> RetrieveLeafOutput(PiscalLeafInput leafInput);
|
||||
void CleanupLeafProcess(PiscalLeafInput leafInput);
|
||||
string GetErrorMessage(PiscalLeafInput leafInput);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,48 @@
|
||||
using AutoMapper;
|
||||
using System.Linq;
|
||||
using AutoMapper;
|
||||
using LeafWeb.Core.Entities;
|
||||
using LeafWeb.Core.Utility;
|
||||
|
||||
namespace LeafWeb.Core.Remote
|
||||
{
|
||||
public class PiscalLeafInput
|
||||
{
|
||||
private static readonly IMapper Mapper;
|
||||
public int LeafInputId { get; set; }
|
||||
public string PhotosyntheticType { get; set; }
|
||||
public string DirectoryName { get; set; }
|
||||
public PiscalLeafInputFile[] InputFiles { get; set; }
|
||||
|
||||
static PiscalLeafInput()
|
||||
{
|
||||
var config =
|
||||
new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<LeafInput, PiscalLeafInput>()
|
||||
.ForMember(dest => dest.DirectoryName,
|
||||
opt => opt.MapFrom(src => PiscalUtility.GetPiscalDirectoryName(src)))
|
||||
.ForMember(dest => dest.LeafInputId, opt => opt.MapFrom(src => src.Id))
|
||||
.ForMember(dest => dest.InputFiles, opt => opt.MapFrom(src => src.InputFiles.Select(f => new PiscalLeafInputFile(f)).ToArray()))
|
||||
.ForMember(
|
||||
dest => dest.PhotosyntheticType,
|
||||
opt => opt.MapFrom(src => src.PhotosynthesisType.Id.WhitespaceToUnderscore()));
|
||||
});
|
||||
Mapper = config.CreateMapper();
|
||||
}
|
||||
|
||||
public PiscalLeafInput() { }
|
||||
|
||||
public PiscalLeafInput(LeafInput leafInput)
|
||||
{
|
||||
Mapper.Map(leafInput, this);
|
||||
}
|
||||
}
|
||||
|
||||
public class PiscalLeafInputFile
|
||||
{
|
||||
private static readonly IMapper Mapper;
|
||||
|
||||
public int LeafInputId { get; set; }
|
||||
public string Filename { get; set; }
|
||||
public byte[] Contents { get; set; }
|
||||
public string DirectoryName { get; set; }
|
||||
public string PhotosyntheticType { get; set; }
|
||||
|
||||
static PiscalLeafInputFile()
|
||||
{
|
||||
@@ -20,14 +50,8 @@ namespace LeafWeb.Core.Remote
|
||||
new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<LeafInputFile, PiscalLeafInputFile>()
|
||||
.ForMember(dest => dest.DirectoryName,
|
||||
opt => opt.MapFrom(src => PiscalUtility.GetPiscalDirectoryName(src)))
|
||||
.ForMember(dest => dest.Filename, opt =>
|
||||
opt.MapFrom(src => src.Filename.WhitespaceToUnderscore().FilterValidFilename()))
|
||||
.ForMember(dest => dest.LeafInputId, opt => opt.MapFrom(src => src.Id))
|
||||
.ForMember(
|
||||
dest => dest.PhotosyntheticType,
|
||||
opt => opt.MapFrom(src => src.LeafInput.PhotosynthesisType.Id.WhitespaceToUnderscore()));
|
||||
.ForMember(dest => dest.Filename, opt =>
|
||||
opt.MapFrom(src => src.Filename.WhitespaceToUnderscore().FilterValidFilename()));
|
||||
});
|
||||
Mapper = config.CreateMapper();
|
||||
}
|
||||
|
||||
@@ -39,25 +39,35 @@ namespace LeafWeb.Core.Remote
|
||||
return new ScpClient(_connectionInfo);
|
||||
}
|
||||
|
||||
public void RunLeafInputFile(PiscalLeafInputFile file)
|
||||
private void CopyLeafInput(PiscalLeafInput leafInput, string directory)
|
||||
{
|
||||
var inputPath = $"{BaseDirectory}/{file.DirectoryName}/{file.Filename}";
|
||||
|
||||
// copy file
|
||||
// copy files
|
||||
using (var scp = GetScpClient())
|
||||
using (var stream = new MemoryStream(file.Contents))
|
||||
foreach (var file in leafInput.InputFiles)
|
||||
{
|
||||
Console.WriteLine(inputPath);
|
||||
scp.Connect();
|
||||
scp.Upload(stream, inputPath);
|
||||
scp.Disconnect();
|
||||
var inputPath = $"{directory}/{file.Filename}";
|
||||
using (var stream = new MemoryStream(file.Contents))
|
||||
{
|
||||
Console.WriteLine(inputPath);
|
||||
scp.Connect();
|
||||
scp.Upload(stream, inputPath);
|
||||
scp.Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RunLeafInput(PiscalLeafInput leafInput)
|
||||
{
|
||||
var inputDirectory = $"{BaseDirectory}/{leafInput.DirectoryName}";
|
||||
|
||||
CopyLeafInput(leafInput, inputDirectory);
|
||||
|
||||
// begin processing
|
||||
using (var ssh = GetSshClient())
|
||||
{
|
||||
ssh.Connect();
|
||||
var commandText = $"{RemoteScriptPath} -d {file.DirectoryName} -f {file.Filename} -p {file.PhotosyntheticType}";
|
||||
var commandText = $"{RemoteScriptPath} -d {leafInput.DirectoryName} -p {leafInput.PhotosyntheticType}";
|
||||
Console.Write(commandText);
|
||||
var command = ssh.CreateCommand(commandText);
|
||||
command.Execute();
|
||||
ssh.Disconnect();
|
||||
@@ -69,9 +79,9 @@ namespace LeafWeb.Core.Remote
|
||||
}
|
||||
}
|
||||
|
||||
public PiscalStatus GetLeafInputFileStatus(PiscalLeafInputFile file)
|
||||
public PiscalStatus GetLeafInputStatus(PiscalLeafInput leafInput)
|
||||
{
|
||||
var statusRaw = GetLeafInputStatusRaw(file);
|
||||
var statusRaw = GetLeafInputStatusRaw(leafInput);
|
||||
|
||||
switch (statusRaw[0])
|
||||
{
|
||||
@@ -86,12 +96,12 @@ namespace LeafWeb.Core.Remote
|
||||
}
|
||||
}
|
||||
|
||||
private string[] GetLeafInputStatusRaw(PiscalLeafInputFile file)
|
||||
private string[] GetLeafInputStatusRaw(PiscalLeafInput leafInput)
|
||||
{
|
||||
using (var ssh = GetSshClient())
|
||||
{
|
||||
ssh.Connect();
|
||||
var commandText = $"{RemoteScriptPath} -d {file.DirectoryName} -s";
|
||||
var commandText = $"{RemoteScriptPath} -d {leafInput.DirectoryName} -s";
|
||||
var command = ssh.CreateCommand(commandText);
|
||||
command.Execute();
|
||||
ssh.Disconnect();
|
||||
@@ -109,10 +119,10 @@ namespace LeafWeb.Core.Remote
|
||||
/// <summary>
|
||||
/// Gets the leaf output from piscal, only run on if result status is success
|
||||
/// </summary>
|
||||
public IEnumerable<PiscalLeafOutputFile> RetrieveLeafOutput(PiscalLeafInputFile file)
|
||||
public IEnumerable<PiscalLeafOutputFile> RetrieveLeafOutput(PiscalLeafInput leafInput)
|
||||
{
|
||||
// get output files
|
||||
var status = GetLeafInputStatusRaw(file);
|
||||
var status = GetLeafInputStatusRaw(leafInput);
|
||||
if (status[0] != StatusSuccess)
|
||||
throw new PiscalClientException("output not available, status is " + status[0]);
|
||||
|
||||
@@ -131,7 +141,7 @@ namespace LeafWeb.Core.Remote
|
||||
{
|
||||
Contents = stream.ToArray(),
|
||||
Filename = filePath.FilenameFromPath(),
|
||||
DirectoryName = file.DirectoryName
|
||||
DirectoryName = leafInput.DirectoryName
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -140,9 +150,9 @@ namespace LeafWeb.Core.Remote
|
||||
}
|
||||
}
|
||||
|
||||
public string GetErrorMessage(PiscalLeafInputFile file)
|
||||
public string GetErrorMessage(PiscalLeafInput leafInput)
|
||||
{
|
||||
var status = GetLeafInputStatusRaw(file);
|
||||
var status = GetLeafInputStatusRaw(leafInput);
|
||||
if (status[0] != StatusError)
|
||||
return string.Empty;
|
||||
|
||||
@@ -150,16 +160,16 @@ namespace LeafWeb.Core.Remote
|
||||
return errorLines.Join(Environment.NewLine);
|
||||
}
|
||||
|
||||
public void CleanupLeafProcess(PiscalLeafInputFile file)
|
||||
public void CleanupLeafProcess(PiscalLeafInput leafInput)
|
||||
{
|
||||
var status = GetLeafInputStatusRaw(file);
|
||||
var status = GetLeafInputStatusRaw(leafInput);
|
||||
if (status[0] == StatusRunning)
|
||||
throw new PiscalClientException("Trying to cleanup a running process");
|
||||
|
||||
using (var ssh = GetSshClient())
|
||||
{
|
||||
ssh.Connect();
|
||||
var commandText = $"{RemoteScriptPath} -d {file.DirectoryName} -c";
|
||||
var commandText = $"{RemoteScriptPath} -d {leafInput.DirectoryName} -c";
|
||||
var command = ssh.CreateCommand(commandText);
|
||||
command.Execute();
|
||||
ssh.Disconnect();
|
||||
|
||||
@@ -7,21 +7,21 @@ namespace LeafWeb.Core.Remote
|
||||
{
|
||||
public static class PiscalUtility
|
||||
{
|
||||
public static string GetPiscalDirectoryName(int id, string name, string identifier)
|
||||
public static string GetPiscalDirectoryName(int id, string identifier)
|
||||
{
|
||||
return $"{id}_{name.FilterAlphaNumeric()}_{identifier.FilterAlphaNumeric()}";
|
||||
return $"{id}_{identifier.FilterAlphaNumeric()}";
|
||||
}
|
||||
|
||||
public static string GetPiscalDirectoryName(LeafInputFile leafInputFile)
|
||||
public static string GetPiscalDirectoryName(LeafInput leafInput)
|
||||
{
|
||||
return GetPiscalDirectoryName(leafInputFile.Id, leafInputFile.LeafInput.Name, leafInputFile.LeafInput.Identifier);
|
||||
return GetPiscalDirectoryName(leafInput.Id, leafInput.Identifier);
|
||||
}
|
||||
|
||||
public static int GetIdFromDirectoryName(string directoryName)
|
||||
{
|
||||
var match = Regex.Match(directoryName, @"\d_");
|
||||
if (!match.Success)
|
||||
throw new FormatException("DirectoryName expected to be formatted {number}_{name}_{identifier}: " + directoryName);
|
||||
throw new FormatException("DirectoryName expected to be formatted {number}_{identifier}: " + directoryName);
|
||||
return int.Parse(match.Captures[0].Value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user