Add error handling an cleaning up for Archiving
This commit is contained in:
parent
af21f771d0
commit
4a2bd5b38f
@ -22,6 +22,7 @@ namespace TeamHobby.HobbyProjectGenerator.Archive
|
||||
// Find the root drive of the program
|
||||
string drive = Path.GetPathRoot(path: curPath);
|
||||
// Check if Drive exists or not
|
||||
try {
|
||||
if (!Directory.Exists(drive))
|
||||
{
|
||||
Console.WriteLine("Drive {0} not found", drive);
|
||||
@ -34,6 +35,12 @@ namespace TeamHobby.HobbyProjectGenerator.Archive
|
||||
{
|
||||
Console.WriteLine("Creating a new folder at: {0}", archiveFolder);
|
||||
Directory.CreateDirectory(archiveFolder);
|
||||
//return true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex){
|
||||
// If the creating a folder failed, return false
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -91,7 +98,11 @@ namespace TeamHobby.HobbyProjectGenerator.Archive
|
||||
if (_conn.GetType() == typeof(SqlDAO))
|
||||
{
|
||||
sqlDS = (SqlDAO)_conn;
|
||||
}
|
||||
|
||||
// Check to make sure that Sql Data source connection is not null
|
||||
if (sqlDS == null){
|
||||
return false;
|
||||
}
|
||||
// TODO: Remember to add try catch block here to make sure it is completed.
|
||||
|
||||
@ -103,11 +114,11 @@ namespace TeamHobby.HobbyProjectGenerator.Archive
|
||||
Console.WriteLine("");
|
||||
|
||||
// Creating a file name:
|
||||
string filePath = @"C:\HobbyArchive";
|
||||
//string filePath = @"C:\HobbyArchive";
|
||||
//Console.WriteLine("Creating file name ... ");
|
||||
//string curPath = archive.CreateOutFileName();
|
||||
Console.WriteLine("Creating Output file name ...");
|
||||
string curPath = CreateOutFileName();
|
||||
string outPath = CreateOutFileName();
|
||||
Console.WriteLine("Output file name created ...");
|
||||
Console.WriteLine("----------------");
|
||||
Console.WriteLine("");
|
||||
@ -116,23 +127,25 @@ namespace TeamHobby.HobbyProjectGenerator.Archive
|
||||
//string pathTemp = "C:/Temp/oldlogs10.txt";
|
||||
//string pathTempBack = @"C:\Temp\oldlogs10.txt";
|
||||
|
||||
// Try catch block to delete abort the compressing process
|
||||
try{
|
||||
//Output SQL to a text file
|
||||
Console.WriteLine("Copying to a text file ...");
|
||||
sqlDS.CopyToFile(curPath);
|
||||
sqlDS.CopyToFile(outPath);
|
||||
Console.WriteLine("Copying completed ...");
|
||||
Console.WriteLine("----------------");
|
||||
Console.WriteLine("");
|
||||
|
||||
// Compress the file
|
||||
Console.WriteLine("Copressing the text file ...");
|
||||
sqlDS.CompressFile(curPath);
|
||||
sqlDS.CompressFile(outPath);
|
||||
Console.WriteLine("Copression completed ...");
|
||||
Console.WriteLine("----------------");
|
||||
Console.WriteLine("");
|
||||
|
||||
//Remove output file
|
||||
Console.WriteLine("Removing the text file ...");
|
||||
sqlDS.RemoveOutputFile(curPath);
|
||||
sqlDS.RemoveOutputFile(outPath);
|
||||
Console.WriteLine("Text File removal completed ...");
|
||||
Console.WriteLine("----------------");
|
||||
Console.WriteLine("");
|
||||
@ -144,18 +157,33 @@ namespace TeamHobby.HobbyProjectGenerator.Archive
|
||||
Console.WriteLine("----------------");
|
||||
Console.WriteLine("");
|
||||
|
||||
// Return true if compressing sequence is successful
|
||||
return true;
|
||||
}
|
||||
catch (Exception e){
|
||||
// Abort the compressing sequence and clean up uneccesary files.
|
||||
Console.WriteLine("Archiving Sequenced failed. Cleaning up resources.");
|
||||
|
||||
// Delete the text output file if any of the process failed
|
||||
if (File.Exists(outPath)){
|
||||
File.Delete(outPath);
|
||||
}
|
||||
|
||||
// Delete the compressed file if Removing entries and removing text file output failed
|
||||
string compFile = outPath + ".gz";
|
||||
if (File.Exists(compFile)){
|
||||
File.Delete(compFile);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
// Testing date time patters
|
||||
//string date = DateTime.Now.ToString("d");
|
||||
//Console.WriteLine(date);
|
||||
//Console.WriteLine(DateTime.Now.ToString("M_d_yyyy_H:mm:ss"));
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -149,7 +149,6 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess
|
||||
// Get the stream of the original file
|
||||
using FileStream origFile = File.Open(fileName, FileMode.Open);
|
||||
|
||||
|
||||
// Get the attribute of the file
|
||||
FileAttributes atrribute = File.GetAttributes(fileName);
|
||||
|
||||
@ -168,8 +167,9 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error when compressing a file !!, will be handled higher up the call stack");
|
||||
Console.WriteLine(ex.Message);
|
||||
return false;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@ -204,10 +204,10 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess
|
||||
return true;
|
||||
|
||||
}
|
||||
catch
|
||||
catch (Exception ex)
|
||||
{
|
||||
_conn.Close();
|
||||
Console.WriteLine("Error when copying query to a file !!");
|
||||
Console.WriteLine("Error when copying query to a file !!, will be handled higher up the call stack");
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
@ -229,7 +229,7 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess
|
||||
}
|
||||
catch {
|
||||
Console.WriteLine("Removing file failed!!");
|
||||
return false;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -4,7 +4,6 @@ using System.Data.Odbc;
|
||||
using TeamHobby.HobbyProjectGenerator.Archive;
|
||||
using TeamHobby.HobbyProjectGenerator.DataAccess;
|
||||
|
||||
|
||||
namespace TeamHobby.HobbyProjectGenerator.Main
|
||||
{
|
||||
public class GetCredentials
|
||||
|
||||
Loading…
Reference in New Issue
Block a user