Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct workerFunc
- {
- workerFunc(char* direntard,int argc,char** argv) : direntard(direntard),argc(argc),argv(argv) { }
- void operator()()
- {
- stringstream ss;
- string s;
- ss << direntard;
- ss >> s;
- cout << "s: " << s << endl;
- char maindir[5000];
- strcpy (maindir,CurrentPath);
- strcat (maindir,"/bin/Debug");
- stringstream ssdir;
- string fullpath;
- ssdir << maindir;
- ssdir >> fullpath;
- fullpath.append("/jobhold/");
- fullpath.append(s);
- mailwhen = 1;
- forUrlVector(jobFileToVector(fullpath),direntard); //This function handles each url. It passes as arguments: a newly created vector and direntard.
- //AT THIS POINT THE FILE LOCK MAY BE RELEASED
- //Mail results
- mailfunc(argc,argv,direntard,CurrentPath); //Mail results
- }
- char* direntard;
- int argc;
- char** argv;
- };
- void getjobs(int argc, char** argv)
- {
- char maindir[5000];
- strcpy (maindir,CurrentPath);
- strcat (maindir,"/bin/Debug");
- char openthisdir[5000];
- strcpy (openthisdir,maindir);
- strcat (openthisdir,"/jobs");
- //Do each text file in the jobs folder
- DIR *pDIR;
- struct dirent *entry;
- if( pDIR=opendir(openthisdir) )
- {
- boost::thread_group group;
- while(entry = readdir(pDIR))
- {
- if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 )
- {
- urlVector.clear();
- char filename[5000];
- strcpy (filename,maindir);
- strcat (filename,"/jobs/");
- char dest[5000];
- strcpy (dest,maindir);
- strcat (dest,"/jobhold");
- char filenamedone[5000];
- strcpy (filenamedone,filename);
- strcat (filenamedone,entry->d_name);
- char filenamedonemove[5000];
- strcpy (filenamedonemove,"mv ");
- strcat (filenamedonemove,filename);
- strcat (filenamedonemove,entry->d_name);
- strcat (filenamedonemove," ");
- strcat (filenamedonemove,dest);
- char filetime[5000];
- strcpy (filetime,maindir);
- strcat (filetime,"/jobstime/");
- strcat (filetime,entry->d_name);
- ifstream fin(filenamedone);
- if (fin)
- {
- fin.close();
- cout << "File found, moving..." << filenamedonemove << endl;
- system(filenamedonemove);
- }
- //WE JUST MOVED THE FILE FROM JOBS TO JOBHOLD, BUT WE DONT HAVE A LOCK SO PHP CAN PICK IT UP! We need to lock the file, and not release it until inside the thread we are about to create.
- time_t seconds;
- seconds = time (NULL);
- cout << "Writing " << seconds << "to " << filetime << endl;
- ofstream myfile;
- myfile.open (filetime);
- myfile << seconds;
- myfile.close();
- workerFunc startit(entry->d_name,argc,argv); //Create new worker function
- group.create_thread(startit); //Create a new thread for this worker function
- }
- }
- group.join_all(); //Wait for all threads to finish
- closedir(pDIR);
- }
- }
- void checkFiles()
- {
- char maindir[5000];
- strcpy (maindir,CurrentPath);
- strcat (maindir,"/bin/Debug");
- char openthisdir[5000];
- strcpy (openthisdir,maindir);
- strcat (openthisdir,"/jobstime");
- DIR *pDIR;
- struct dirent *entry;
- if( pDIR=opendir(openthisdir) )
- {
- boost::thread_group group;
- while(entry = readdir(pDIR))
- {
- if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 )
- {
- char maindir[5000];
- strcpy (maindir,CurrentPath);
- strcat (maindir,"/bin/Debug");
- char timefilecheckname[5000];
- strcpy (timefilecheckname,maindir);
- strcat (timefilecheckname,"/jobstime/");
- strcat (timefilecheckname,entry->d_name);
- string line;
- ifstream myfile (timefilecheckname);
- if (myfile.is_open())
- {
- while ( myfile.good() )
- {
- getline (myfile,line);
- }
- myfile.close();
- }
- int timeval = atoi(line.c_str());
- time_t seconds;
- seconds = time (NULL);
- if((timeval + 60) < seconds)
- {
- char filenamedone[5000];
- strcpy (filenamedone,maindir);
- strcat (filenamedone,"/jobhold/");
- strcat (filenamedone,entry->d_name);
- char filenamedonemove[5000];
- strcpy (filenamedonemove,"mv ");
- strcat (filenamedonemove,maindir);
- strcat (filenamedonemove,"/jobhold/");
- strcat (filenamedonemove,entry->d_name);
- strcat (filenamedonemove," ");
- strcat (filenamedonemove,maindir);
- strcat (filenamedonemove,"/jobs");
- ifstream fin(filenamedone);
- if (fin)
- {
- fin.close();
- cout << "File found, moving..." << filenamedonemove << endl;
- system(filenamedonemove);
- }
- char filetimedone[5000];
- strcpy (filetimedone,maindir);
- strcat (filetimedone,"/jobstime/");
- strcat (filetimedone,entry->d_name);
- char filetimeremove[5000];
- strcpy (filetimeremove,"rm ");
- strcat (filetimeremove,maindir);
- strcat (filetimeremove,"/jobstime/");
- strcat (filetimeremove,entry->d_name);
- ifstream fin2(filetimedone);
- if (fin2)
- {
- fin2.close();
- cout << endl << "Time file found, removing..." << filetimeremove << endl;
- system(filetimeremove);
- }
- }
- }
- }
- closedir(pDIR);
- }
- }
- int main(int argc, char** argv)
- {
- GetCurrentPath(CurrentPath); //Get current path
- //Infinite loop
- while (1)
- {
- checkFiles(); //Check if enough time has passed for jobs in "jobhold" to be run again. If yes, the job will be moved to the "jobs" folder.
- getjobs(argc,argv); //Handle all jobs in the "jobs" folder
- sleep(5);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement