# HG changeset patch # User normaldotcom # Date 2010-02-20 00:43:45 # Node ID 48325fa3515c09387c52b4fdcfd27f21ed78d538 # Parent 96697bb825b9ec5b391f85e350263db706035195 Moved some slave code into functions, fixed memory leak diff --git a/src/server/slave.c b/src/server/slave.c --- a/src/server/slave.c +++ b/src/server/slave.c @@ -136,7 +136,7 @@ int main(int argc, char *argv[]) - fprintf(stderr, "Loading config (fasten your seatbelts for a SEGFAULT :-D )...\n"); + fprintf(stderr, "Loading config...\n"); fprintf(stderr, "Connecting to server...\n"); @@ -169,30 +169,15 @@ int main(int argc, char *argv[]) // Variable Preparation _distren_asprintf(&jobdatapath, "job%d", jobnum); - _distren_asprintf(&urltoTar, "http://protofusion.org/distren/stor/job%d/job%d.tar.gz", jobnum); // Prepares URL to download from + _distren_asprintf(&urltoTar, "http://data.distren.org/job%d/job%d.tar.gz", jobnum); // Prepares URL to download from _distren_asprintf(&pathtoTar, "%s/stor/jobdata/job%d.tar.gz", datadir, jobnum); // Prepares destination to save to _distren_asprintf(&pathtoJobfile, "%s/%s/job.blend", datadir, jobdatapath ); // Prepares the path to the jobfile - _distren_asprintf(&urltoOutput, "http://protofusion.org/distren/stor/tmp/", jobdatapath ); // Prepares the URL where output is posted + _distren_asprintf(&urltoOutput, "http://data.distren.org/tmp/", jobdatapath ); // Prepares the URL where output is posted _distren_asprintf(&pathtoXml, "%s/job%d/job%d.xml", datadir, jobnum ); // Prepares the path to the job's XML file free(jobdatapath); - // Prepare to download the job tar if it isn't present - struct stat buffer; - int fstatus = stat(pathtoJobfile, &buffer); - if(fstatus == -1) - { - // Download the Tar - if( curlget(urltoTar, pathtoTar) == 0){ - fprintf(stderr, "Job data retrieved successfully\n"); - free(urltoTar); - } - else { - fprintf(stderr, "Downloading job data from %s failed. Check your network connection.\n",urltoTar); - return 1; // Eventually make a retry loop - } - } - else - fprintf(stderr, "Using cached job file...\n"); + if(downloadTar(urltoTar, pathtoTar)) + return 1; _distren_asprintf(&outdir, "/tmp/distren/job%d", jobnum); mkdir("/tmp/distren", 0750); /* @FIXME: Change to tmpdir once it exists */ @@ -236,23 +221,9 @@ int main(int argc, char *argv[]) /* Post-execution */ fprintf(stderr, "Finished frame %d in job %d, uploading...\n", framenum, jobnum); - if( !curlpost(pathtoOutput, urltoOutput)) // Uploads output - { - fprintf(stderr,"Upload successful, removing old output...\n"); - remove(pathtoOutput); // Delete the file after its uploaded - } - else - { - fprintf(stderr,"Upload failed. Check your network connection. Retrying upload...\n"); - int tries=1; - while(tries<=10 && curlpost(pathtoOutput, urltoOutput)) - { - fprintf(stderr, "Upload failed. Trying again in 10 seconds... (attempt %d of 10)\n", tries); - tries++; - sleep(10); - } - // @FUTURE: Keep track of files that we were unable to upload, and upload them later - } + + uploadOutput(pathtoOutput, urltoOutput); // @TODO: Handle return value + free(urltoOutput); free(pathtoOutput); diff --git a/src/server/slavefuncs.c b/src/server/slavefuncs.c --- a/src/server/slavefuncs.c +++ b/src/server/slavefuncs.c @@ -338,3 +338,51 @@ int job_build_path(char *filename, unsig { return 0; } + + +int downloadTar(char *url, char *destinationPath){ + // Prepare to download the job tar if it isn't present + struct stat buffer; + int fstatus = stat(destinationPath, &buffer); + if(fstatus == -1) + { + // Download the Tar + if( curlget(url, destinationPath) == 0){ + fprintf(stderr, "Job data retrieved successfully\n"); + free(url); + return 0; + } + else { + fprintf(stderr, "Downloading job data from %s failed. Check your network connection.\n",url); + free(url); + return 1; // Eventually make a retry loop + } + } + else{ + fprintf(stderr, "Tar already exists! Download cancelled.\n"); + return 2; + } +} + + +int uploadOutput(char *pathtoOutput, char *urltoOutput){ + if( !curlpost(pathtoOutput, urltoOutput)) // Uploads output + { + fprintf(stderr,"Upload successful, removing old output...\n"); + remove(pathtoOutput); // Delete the file after its uploaded + return 0; + } + else + { + fprintf(stderr,"Upload failed. Check your network connection. Retrying upload...\n"); + int tries=1; + while(tries<=10 && curlpost(pathtoOutput, urltoOutput)) + { + fprintf(stderr, "Upload failed. Trying again in 10 seconds... (attempt %d of 10)\n", tries); + tries++; + sleep(10); + } + return 1; // Upload failed after multiple tries + // @FUTURE: Keep track of files that we were unable to upload, and upload them later + } +} diff --git a/src/server/slavefuncs.h b/src/server/slavefuncs.h --- a/src/server/slavefuncs.h +++ b/src/server/slavefuncs.h @@ -46,5 +46,7 @@ void xmlinit(); void xmlcleanup(); int distren_mkdir_recurse(char *dirname); int job_build_path(char *filename, unsigned int jobnum); +int downloadTar(char *url, char *destinationPath); +int uploadOutput(char *pathtoOutput, char *urltoOutput); #endif