//单线程即可 public static Tuple <int, string> m_fSendFile(m_cRecModel m_mRecModel, string m_sRemoteDirectory, bool m_bTest, LogTyper lt = LogTyper.LogLogger) { int status = 0; string msg = string.Empty; try { ///链接(S)FTP SessionOptions sessionOptions = new SessionOptions(); sessionOptions.Protocol = (Protocol)(int.Parse(m_cConfigConstants.m_sProtocol)); sessionOptions.HostName = m_cConfigConstants.m_sIP; sessionOptions.PortNumber = int.Parse(m_cConfigConstants.m_sPort); sessionOptions.UserName = m_cConfigConstants.m_sUa; sessionOptions.FtpMode = (FtpMode)(int.Parse(m_cConfigConstants.m_sMode)); if (!string.IsNullOrWhiteSpace(m_cConfigConstants.m_sPwd)) { sessionOptions.Password = m_cConfigConstants.m_sPwd; } if (!string.IsNullOrWhiteSpace(m_cConfigConstants.m_sKey)) { sessionOptions.SshHostKeyFingerprint = m_cConfigConstants.m_sKey; } ///放入测试 using (WinSCP.Session session = new WinSCP.Session()) { session.Open(sessionOptions); TransferEventArgs transferEventArgs; TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; //是否需要创建文件夹,逐级创建 if (!string.IsNullOrWhiteSpace(m_sRemoteDirectory) && !session.FileExists(RemotePath.EscapeFileMask(m_sRemoteDirectory))) { //查找出所有目录 string[] m_lDirectory = m_sRemoteDirectory.Split('/'); string m_sPrefixMulu = string.Empty; foreach (string m_sMulu in m_lDirectory) { if (!string.IsNullOrWhiteSpace(m_sMulu)) { m_sPrefixMulu += $"/{m_sMulu}"; if (!session.FileExists(RemotePath.EscapeFileMask(m_sPrefixMulu))) { session.CreateDirectory(RemotePath.EscapeFileMask(m_sPrefixMulu)); Log.Instance.Debug($"逐级创建(S)FTP目录:“{m_sPrefixMulu}”", lt); } } } } //判断文件是否已经上传 if (!session.FileExists(RemotePath.EscapeFileMask($"{m_sRemoteDirectory}/{System.IO.Path.GetFileName(m_mRecModel.mimiFile)}"))) { //如果为测试,文件不进行删除 transferEventArgs = session.PutFileToDirectory(m_mRecModel.mimiFile, RemotePath.EscapeFileMask(m_sRemoteDirectory), !m_bTest, transferOptions); string m_sErrMsg = string.Join(",", transferEventArgs?.Error?.Message); if (!string.IsNullOrWhiteSpace(m_sErrMsg)) { Log.Instance.Debug(m_sErrMsg, lt); status = 1; msg = m_sErrMsg; } else { status = 0; msg = "录音上传(S)FTP成功"; } } else { status = 0; msg = "录音已存在,略过上传(S)FTP"; if (!m_bTest) { Log.Instance.Debug($"{m_mRecModel.Id}:“{msg}”", lt); } } } } catch (Exception ex) { status = 1; msg = ex.Message; //如果有误 if (!m_bTest) { m_cSQL.m_fSetActionState(m_mRecModel.Id, "3", $"上传(S)FTP错误:{msg}", lt); } //打印一下错误,处理一下此处导致计划任务无法继续的问题 Log.Instance.Debug(ex, lt); } return(new Tuple <int, string>(status, msg)); }
public void TransferData() { bool nhsRun; string[] fields; SessionOptions sessionOptions = new SessionOptions(); //get run parameters string runId = Framework.GetRunIdFromRunInfoXml(Path.Combine(localRunDir, @"RunInfo.xml")); //Parse samplesheet SampleSheetHeader sampleSheetHeader = new SampleSheetHeader(Path.Combine(localRunDir, @"SampleSheet.csv")); sampleSheetHeader.Populate(); //write variables to log Framework.WriteLog(@"Local run directory: " + localRunDir, 0); Framework.WriteLog(@"Run identifier: " + runId, 0); Framework.WriteLog(@"Local SampleSheet path: " + Path.Combine(localRunDir, @"SampleSheet.csv"), 0); Framework.WriteLog(@"Experiment name: " + sampleSheetHeader.getExperimentName, 0); //determine institution if (sampleSheetHeader.getInvestigatorName == null || sampleSheetHeader.getInvestigatorName == "" || !sampleSheetHeader.getInvestigatorName.Contains('-')) { throw new InvalidDataException(@"Investigator name field not provided or malformed."); } //split investigtor name field fields = sampleSheetHeader.getInvestigatorName.Split('-'); if (fields[1].ToUpper() == @"CU") { nhsRun = false; Framework.WriteLog(@"Investigator name: " + fields[0], 0); Framework.WriteLog(@"Institution name: Cardiff University", 0); sessionOptions.SshHostKeyFingerprint = config.getWotanSshHostKeyFingerprint; sessionOptions.HostName = config.getWotanHostName; sessionOptions.UserName = config.getWotanSshUserName; sessionOptions.SshPrivateKeyPath = config.getWotanSshPrivateKeyPath; } else if (fields[1].ToUpper() == @"NHS") { nhsRun = true; Framework.WriteLog(@"Investigator name: " + fields[0], 0); Framework.WriteLog(@"Institution name: NHS", 0); sessionOptions.SshHostKeyFingerprint = config.getCvxGenSshHostKeyFingerprint; sessionOptions.HostName = config.getCvxGenHostName; sessionOptions.UserName = config.getCvxGenSshUserName; sessionOptions.SshPrivateKeyPath = config.getCvxGenSshPrivateKeyPath; } else { throw new InvalidDataException(@"Institute not recognised. CU an NHS are only available options."); } //convert metrics into table Framework.WriteLog(@"Writing SAV table ...", 0); string savFilePath = Path.Combine(Path.GetTempPath(), runId + @"_SAV.txt"); Framework.CallSequenceAnalysisViewer(savFilePath, localRunDir, config.getSavExePath); //transfer data to cluster if (nhsRun) { Framework.WriteLog(@"Starting transfer to cvx-gen ...", 0); using (Session session = new Session()) { string remotePath; if (config.getIsMiSeqHost) { remotePath = config.getCvxGenRemoteArchivePath + @"/miseq/" + runId; } else { remotePath = config.getCvxGenRemoteArchivePath + @"/hiseq/" + runId; } session.Open(sessionOptions); Framework.WriteLog(@"Connected!", 0); TransferOperationResult transferResult; TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; //make remote project directory Framework.WriteLog(@"Creating remote directory: " + remotePath, 0); session.CreateDirectory(remotePath); //transfer run folder to archive Framework.WriteLog(@"Tranfer started ...", 0); transferResult = session.PutFiles(localRunDir, remotePath, false, transferOptions); transferResult.Check(); // Throw on any error //transfer SAV file transferResult = session.PutFiles(savFilePath, config.getCvxGenRemoteSAVPath, false, transferOptions); transferResult.Check(); // Throw on any error //execute remote function Framework.WriteLog(@"Execuing post-run command ...", 0); session.ExecuteCommand(@"bash " + config.getCvxGenRemoteScriptPath + ' ' + remotePath + ' ' + config.getCvxGenRemoteResultsPath + '/' + runId + @" >> ~/runPipeline.log"); } } else { //TODO transfer to Wotan } //write run transfer completion using (StreamWriter file = new StreamWriter(Path.Combine(localRunDir, @"TransferComplete.txt"))) { file.WriteLine(@"NGSTransferComplete," + Program.programVersion); file.Close(); } }