Merge pull request #22 from jejem/master
Adding retry (default 5 attempts) for file download and md5 check
This commit is contained in:
@@ -66,6 +66,8 @@ public class Client {
|
|||||||
private boolean running;
|
private boolean running;
|
||||||
private boolean suspended;
|
private boolean suspended;
|
||||||
|
|
||||||
|
private int maxDownloadFileAttempts = 5;
|
||||||
|
|
||||||
public Client(Gui gui_, Configuration config, String url_) {
|
public Client(Gui gui_, Configuration config, String url_) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.server = new Server(url_, this.config, this);
|
this.server = new Server(url_, this.config, this);
|
||||||
@@ -714,27 +716,58 @@ public class Client {
|
|||||||
private int downloadFile(Job ajob, String local_path, String md5_server, String url, String update_ui) {
|
private int downloadFile(Job ajob, String local_path, String md5_server, String url, String update_ui) {
|
||||||
File local_path_file = new File(local_path);
|
File local_path_file = new File(local_path);
|
||||||
|
|
||||||
if (local_path_file.exists() == false) {
|
if (local_path_file.exists() == true) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// must download the archive
|
// must download the archive
|
||||||
int ret = this.server.HTTPGetFile(url, local_path, this.gui, update_ui);
|
int ret = this.server.HTTPGetFile(url, local_path, this.gui, update_ui);
|
||||||
|
boolean md5_check = this.checkFile(ajob, local_path, md5_server);
|
||||||
|
int attempts = 1;
|
||||||
|
|
||||||
|
while ((ret != 0 || md5_check == false) && attempts < this.maxDownloadFileAttempts) {
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
this.gui.error("Client::downloadFile problem with Utils.HTTPGetFile returned " + ret);
|
this.gui.error("Client::downloadFile problem with Utils.HTTPGetFile returned " + ret);
|
||||||
this.log.debug("Client::downloadFile problem with Utils.HTTPGetFile (return: " + ret + ") removing local file (path: " + local_path + ")");
|
this.log.debug("Client::downloadFile problem with Utils.HTTPGetFile (return: " + ret + ") removing local file (path: " + local_path + ")");
|
||||||
|
}
|
||||||
|
else if (md5_check == false) {
|
||||||
|
this.gui.error("Client::downloadFile problem with Client::checkFile mismatch on md5");
|
||||||
|
this.log.debug("Client::downloadFile problem with Client::checkFile mismatch on md5, removing local file (path: " + local_path + ")");
|
||||||
|
}
|
||||||
|
local_path_file.delete();
|
||||||
|
|
||||||
|
this.log.debug("Client::downloadFile failed, let's try again (" + (attempts + 1) + "/" + this.maxDownloadFileAttempts + ") ...");
|
||||||
|
|
||||||
|
ret = this.server.HTTPGetFile(url, local_path, this.gui, update_ui);
|
||||||
|
md5_check = this.checkFile(ajob, local_path, md5_server);
|
||||||
|
attempts++;
|
||||||
|
|
||||||
|
if ((ret != 0 || md5_check == false) && attempts >= this.maxDownloadFileAttempts) {
|
||||||
|
this.log.debug("Client::downloadFile failed after " + this.maxDownloadFileAttempts + " attempts, removing local file (path: " + local_path + "), stopping...");
|
||||||
local_path_file.delete();
|
local_path_file.delete();
|
||||||
return -9;
|
return -9;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkFile(Job ajob, String local_path, String md5_server) {
|
||||||
|
File local_path_file = new File(local_path);
|
||||||
|
|
||||||
|
if (local_path_file.exists() == false) {
|
||||||
|
this.log.error("Client::checkFile cannot check md5 on a nonexistent file (path: " + local_path + ")");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
String md5_local = Utils.md5(local_path);
|
String md5_local = Utils.md5(local_path);
|
||||||
|
|
||||||
if (md5_local.equals(md5_server) == false) {
|
if (md5_local.equals(md5_server) == false) {
|
||||||
this.log.error("Client::downloadFile mismatch on md5 local: '" + md5_local + "' server: '" + ajob.getRenderMd5() + "' (local size: " + new File(local_path).length() + ")");
|
this.log.error("Client::checkFile mismatch on md5 local: '" + md5_local + "' server: '" + ajob.getRenderMd5() + "' (local size: " + new File(local_path).length() + ")");
|
||||||
// md5 of the downloaded file doesn't match the expected hash
|
return false;
|
||||||
this.log.debug("Client::downloadFile mismatch md5 removing local file (path: " + local_path + ")");
|
|
||||||
local_path_file.delete();
|
|
||||||
return -10;
|
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int prepareWorkingDirectory(Job ajob) {
|
protected int prepareWorkingDirectory(Job ajob) {
|
||||||
|
|||||||
Reference in New Issue
Block a user