Skip to content

Commit

Permalink
Merge pull request #14 from asimiqbal11/master
Browse files Browse the repository at this point in the history
Fixed progress file issue in concurrent requests by adding a "Resume" switch parameter in Copy-SFItem command
  • Loading branch information
apikeith committed Apr 9, 2015
2 parents 5b54ea7 + 89486c2 commit d620240
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -34,3 +34,5 @@ packages/*
Build/runbuild.txt
docs/*

*/bin
*/obj
Binary file not shown.
Binary file not shown.
65 changes: 42 additions & 23 deletions ShareFileSnapIn/CopySfItem.cs
Expand Up @@ -72,41 +72,57 @@ public string[] Path
[Parameter(Mandatory = false)]
public bool Force { get; set; }

/// <summary>
/// Resume flag, if 'true' then it will keep track of command status
/// if command is unsuccessful due to disconnection then it will prompt user next time to complete it first
/// </summary>
[Parameter()]
[Alias("ResumeFlag")]
public SwitchParameter Resume { get; set; }

protected override void ProcessRecord()
{
ResumeSupport = new Resume.ResumeSupport();
FileSupport = new FileSupport(MarkFileStatus);

if (ResumeSupport.IsPending)
{
Logger.Instance.Info("Last command wasn't successful due to discconnection.");

Console.Write("Last command wasn't successful due to discconnection, enter 'y' to complete or any other key to ignore: ");
Collection<PSObject> result = InvokeCommand.InvokeScript("Read-Host");
string userOption = result != null && result.Count > 0 ? result[0].ToString() : string.Empty;

if (userOption.ToLower().Equals("y"))
if (Resume)
{
if (ResumeSupport.IsPending)
{
Logger.Instance.Info("Copying those files which were missed due to discconnection with following parameters. Source Path:{0} Destination:{1} Forced:{2} Details:{3}",
String.Join(",", ResumeSupport.GetPath), ResumeSupport.GetDestination, ResumeSupport.GetForce, ResumeSupport.GetDetails);
Logger.Instance.Info("Last command wasn't successful due to discconnection.");

StartCopying(ResumeSupport.GetPath, ResumeSupport.GetDestination, ResumeSupport.GetForce, ResumeSupport.GetDetails);
WriteObject("Last command wasn't successful due to discconnection, enter 'y' to complete or any other key to ignore: ");
Collection<PSObject> result = InvokeCommand.InvokeScript("Read-Host");
string userOption = result != null && result.Count > 0 ? result[0].ToString() : string.Empty;

Logger.Instance.Info("Copying operation completed for missing files");
ResumeSupport.End();
WriteObject("Last command completed, starting current operation");
if (userOption.ToLower().Equals("y"))
{
Logger.Instance.Info("Copying those files which were missed due to discconnection with following parameters. Source Path:{0} Destination:{1} Forced:{2} Details:{3}",
String.Join(",", ResumeSupport.GetPath), ResumeSupport.GetDestination, ResumeSupport.GetForce, ResumeSupport.GetDetails);

StartCopying(ResumeSupport.GetPath, ResumeSupport.GetDestination, ResumeSupport.GetForce, ResumeSupport.GetDetails);

Logger.Instance.Info("Copying operation completed for missing files");
ResumeSupport.End();
WriteObject("Last command completed, starting current operation");
}
}
}
else
{
ResumeSupport.UnmarkResumeFlag();
}

Logger.Instance.Info(String.Format("Starting Copy Operation with following parameters. Source Path:{0} Destination:{1} Forced:{2} Details:{3}",
Logger.Instance.Info(String.Format("Starting Copy Operation with following parameters. Source Path:{0} Destination:{1} Forced:{2} Details:{3}",
String.Join(",", Path), Destination, Force, Details));
ResumeSupport.Start(Path, Destination, Force, Details);

if (Resume) ResumeSupport.Start(Path, Destination, Force, Details);

StartCopying(Path, Destination.Trim(), Force, Details);
Thread.Sleep(100);

ResumeSupport.End();
Logger.Instance.Info("Command executed successfully");
if (Resume) ResumeSupport.End();

WriteObject("Files copied successfully");
}

Expand Down Expand Up @@ -233,7 +249,7 @@ private void RecursiveUpload(ShareFileClient client, int uploadId, FileSystemInf
{
var newFolder = new Models.Folder() { Name = source.Name };
newFolder = client.Items.CreateFolder(target.url, newFolder, Force || ResumeSupport.IsPending, false).Execute();

ActionManager actionManager = new ActionManager(this, source.Name);
ActionType actionType = Force ? ActionType.Force : ActionType.None;

Expand All @@ -252,7 +268,7 @@ private void RecursiveUpload(ShareFileClient client, int uploadId, FileSystemInf
}
}
}

actionManager.Execute();
}
else if (source is FileInfo)
Expand Down Expand Up @@ -316,7 +332,10 @@ private void RecursiveDownload(ShareFileClient client, int downloadId, Models.It

private void MarkFileStatus(String fileName)
{
ResumeSupport.MarkFileStatus(fileName);
if (Resume)
{
ResumeSupport.MarkFileStatus(fileName);
}
}

#region Previous Implementations (commented) > Upload/Download
Expand Down Expand Up @@ -410,7 +429,7 @@ private void MarkFileStatus(String fileName)
// }
// }
//}

#endregion

protected void RecursiveCopy(FileSystemInfo source, DirectoryInfo target)
Expand Down
5 changes: 3 additions & 2 deletions ShareFileSnapIn/Parallel/DownloadAction.cs
Expand Up @@ -21,7 +21,7 @@ class DownloadAction : IAction
private int downloadId;
private FileSystemInfo target;
private ActionType actionType;
private FileSupport fileSupport;
private FileSupport fileSupportDelegate;

public DownloadAction(FileSupport fileSupport, Client.ShareFileClient client, int downloadId, Models.File child, FileSystemInfo target, ActionType type)
{
Expand All @@ -30,7 +30,7 @@ public DownloadAction(FileSupport fileSupport, Client.ShareFileClient client, in
this.downloadId = downloadId;
this.target = target;
this.actionType = type;
this.fileSupport = fileSupport;
this.fileSupportDelegate = fileSupport;
}

void IAction.CopyFileItem(ProgressInfo progressInfo)
Expand Down Expand Up @@ -63,6 +63,7 @@ void IAction.CopyFileItem(ProgressInfo progressInfo)
downloader.DownloadToAsync(fileStream).Wait();

fileStream.Close();
fileSupportDelegate(fileName);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions ShareFileSnapIn/Parallel/UploadAction.cs
Expand Up @@ -24,16 +24,16 @@ class UploadAction : IAction
private Client.ShareFileClient client;
private Models.Item uploadTarget;
private String details;
private FileSupport copySfItem;
private FileSupport fileSupportDelegate;
private ActionType actionType;

public UploadAction(FileSupport cmdLet, Client.ShareFileClient client, FileSystemInfo source, Models.Item target, String details, ActionType type)
public UploadAction(FileSupport fileSupport, Client.ShareFileClient client, FileSystemInfo source, Models.Item target, String details, ActionType type)
{
this.client = client;
this.child = source;
this.uploadTarget = target;
this.details = details;
this.copySfItem = cmdLet;
this.fileSupportDelegate = fileSupport;
actionType = type;
}

Expand Down Expand Up @@ -88,6 +88,7 @@ void IAction.CopyFileItem(ProgressInfo progressInfo)
};

Task.Run(() => uploader.UploadAsync()).Wait();
fileSupportDelegate(fileInfo.Name);
}
}

Expand Down
12 changes: 9 additions & 3 deletions ShareFileSnapIn/Resume/ResumeSupport.cs
Expand Up @@ -18,6 +18,7 @@ class ResumeSupport
{
private String XML_FILE_NAME = @".progressfile";
private ProgressFile progressObject;
private bool flagResume;

/// <summary>
/// Initialize/Load the progressfile and de-serialize the object
Expand All @@ -27,7 +28,7 @@ public ResumeSupport()

string directory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
XML_FILE_NAME = Path.Combine(directory, Resources.AppName, XML_FILE_NAME);

flagResume = true;
progressObject = LoadFile();
}

Expand All @@ -40,7 +41,7 @@ public ResumeSupport()
/// <param name="details">Upload Specification Request Details</param>
public void Start(String[] source, String target, bool force, String details)
{
progressObject = new ProgressFile();
progressObject = new ProgressFile();

progressObject.ArgumentSource = source;
progressObject.ArgumentTarget = target;
Expand Down Expand Up @@ -91,7 +92,7 @@ public bool IsPending
{
get
{
return progressObject.IsExist && progressObject.IsPending;
return flagResume && progressObject.IsExist && progressObject.IsPending;
}
}

Expand Down Expand Up @@ -139,6 +140,11 @@ public String GetDetails
}
}

public void UnmarkResumeFlag()
{
this.flagResume = false;
}

/// <summary>
/// Save/serialize progress to ".progressfile" file
/// It will be called after each operation i.e. file completion, command completion & new command
Expand Down
19 changes: 11 additions & 8 deletions ShareFileSnapIn/Resume/SupportHandler.cs
Expand Up @@ -20,17 +20,20 @@ class SupportHandler<T> where T : class
/// <returns>File Object</returns>
public static T Load(string path)
{
T serializableObject = null;

using (Stream textReader = CreateTextReader(path))
lock (syncFileLock)
{
XmlSerializer xmlSerializer = CreateXmlSerializer();
serializableObject = xmlSerializer.Deserialize(textReader) as T;
T serializableObject = null;

textReader.Close();
}
using (Stream textReader = CreateTextReader(path))
{
XmlSerializer xmlSerializer = CreateXmlSerializer();
serializableObject = xmlSerializer.Deserialize(textReader) as T;

textReader.Close();
}

return serializableObject;
return serializableObject;
}
}

/// <summary>
Expand Down
Binary file modified ShareFileSnapIn/bin/Release/ShareFileSnapIn.dll
Binary file not shown.

0 comments on commit d620240

Please sign in to comment.