Advertisement
natli

Untitled

Nov 16th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.34 KB | None | 0 0
  1. struct workerFunc
  2. {
  3.     workerFunc(char* direntard,int argc,char** argv) : direntard(direntard),argc(argc),argv(argv) { }
  4.  
  5.     void operator()()
  6.     {
  7.         stringstream ss;
  8.         string s;
  9.         ss << direntard;
  10.         ss >> s;
  11.         cout << "s: " << s << endl;
  12.  
  13.         char maindir[5000];
  14.         strcpy (maindir,CurrentPath);
  15.         strcat (maindir,"/bin/Debug");
  16.  
  17.         stringstream ssdir;
  18.         string fullpath;
  19.         ssdir << maindir;
  20.         ssdir >> fullpath;
  21.         fullpath.append("/jobhold/");
  22.         fullpath.append(s);
  23.  
  24.         mailwhen = 1;
  25.  
  26.         forUrlVector(jobFileToVector(fullpath),direntard); //This function handles each url. It passes as arguments: a newly created vector and direntard.
  27.  
  28.     //AT THIS POINT THE FILE LOCK MAY BE RELEASED
  29.  
  30.         //Mail results
  31.         mailfunc(argc,argv,direntard,CurrentPath); //Mail results
  32.  
  33.     }
  34.     char* direntard;
  35.     int argc;
  36.     char** argv;
  37. };
  38.  
  39. void getjobs(int argc, char** argv)
  40. {
  41.  
  42.     char maindir[5000];
  43.     strcpy (maindir,CurrentPath);
  44.     strcat (maindir,"/bin/Debug");
  45.  
  46.     char openthisdir[5000];
  47.     strcpy (openthisdir,maindir);
  48.     strcat (openthisdir,"/jobs");
  49.  
  50.     //Do each text file in the jobs folder
  51.     DIR *pDIR;
  52.     struct dirent *entry;
  53.     if( pDIR=opendir(openthisdir) )
  54.     {
  55.         boost::thread_group group;
  56.         while(entry = readdir(pDIR))
  57.         {
  58.             if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 )
  59.             {
  60.                 urlVector.clear();
  61.  
  62.                     char filename[5000];
  63.                     strcpy (filename,maindir);
  64.                     strcat (filename,"/jobs/");
  65.  
  66.                     char dest[5000];
  67.                     strcpy (dest,maindir);
  68.                     strcat (dest,"/jobhold");
  69.  
  70.                     char filenamedone[5000];
  71.                     strcpy (filenamedone,filename);
  72.                     strcat (filenamedone,entry->d_name);
  73.  
  74.                     char filenamedonemove[5000];
  75.                     strcpy (filenamedonemove,"mv ");
  76.                     strcat (filenamedonemove,filename);
  77.                     strcat (filenamedonemove,entry->d_name);
  78.                     strcat (filenamedonemove," ");
  79.                     strcat (filenamedonemove,dest);
  80.  
  81.                     char filetime[5000];
  82.                     strcpy (filetime,maindir);
  83.                     strcat (filetime,"/jobstime/");
  84.                     strcat (filetime,entry->d_name);
  85.  
  86.                     ifstream fin(filenamedone);
  87.                     if (fin)
  88.                     {
  89.                        fin.close();
  90.                        cout << "File found, moving..." << filenamedonemove << endl;
  91.                        system(filenamedonemove);
  92.                     }
  93.  
  94.             //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.
  95.  
  96.                     time_t seconds;
  97.                     seconds = time (NULL);
  98.  
  99.                     cout << "Writing " << seconds << "to " << filetime << endl;
  100.  
  101.                     ofstream myfile;
  102.                     myfile.open (filetime);
  103.                     myfile << seconds;
  104.                     myfile.close();
  105.  
  106.                     workerFunc startit(entry->d_name,argc,argv); //Create new worker function
  107.                     group.create_thread(startit); //Create a new thread for this worker function
  108.             }
  109.         }
  110.         group.join_all(); //Wait for all threads to finish
  111.         closedir(pDIR);
  112.     }
  113. }
  114.  
  115. void checkFiles()
  116. {
  117.     char maindir[5000];
  118.     strcpy (maindir,CurrentPath);
  119.     strcat (maindir,"/bin/Debug");
  120.  
  121.     char openthisdir[5000];
  122.     strcpy (openthisdir,maindir);
  123.     strcat (openthisdir,"/jobstime");
  124.  
  125.     DIR *pDIR;
  126.     struct dirent *entry;
  127.     if( pDIR=opendir(openthisdir) )
  128.     {
  129.         boost::thread_group group;
  130.         while(entry = readdir(pDIR))
  131.         {
  132.             if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 )
  133.             {
  134.  
  135.                     char maindir[5000];
  136.                     strcpy (maindir,CurrentPath);
  137.                     strcat (maindir,"/bin/Debug");
  138.  
  139.                     char timefilecheckname[5000];
  140.                     strcpy (timefilecheckname,maindir);
  141.                     strcat (timefilecheckname,"/jobstime/");
  142.                     strcat (timefilecheckname,entry->d_name);
  143.  
  144.  
  145.                       string line;
  146.                       ifstream myfile (timefilecheckname);
  147.                       if (myfile.is_open())
  148.                       {
  149.                         while ( myfile.good() )
  150.                         {
  151.                           getline (myfile,line);
  152.                         }
  153.                         myfile.close();
  154.                       }
  155.  
  156.                     int timeval = atoi(line.c_str());
  157.  
  158.                     time_t seconds;
  159.                     seconds = time (NULL);
  160.  
  161.                     if((timeval + 60) < seconds)
  162.                     {
  163.  
  164.                         char filenamedone[5000];
  165.                         strcpy (filenamedone,maindir);
  166.                         strcat (filenamedone,"/jobhold/");
  167.                         strcat (filenamedone,entry->d_name);
  168.  
  169.                         char filenamedonemove[5000];
  170.                         strcpy (filenamedonemove,"mv ");
  171.                         strcat (filenamedonemove,maindir);
  172.                         strcat (filenamedonemove,"/jobhold/");
  173.                         strcat (filenamedonemove,entry->d_name);
  174.                         strcat (filenamedonemove," ");
  175.                         strcat (filenamedonemove,maindir);
  176.                         strcat (filenamedonemove,"/jobs");
  177.  
  178.                         ifstream fin(filenamedone);
  179.                         if (fin)
  180.                         {
  181.                            fin.close();
  182.                            cout << "File found, moving..." << filenamedonemove << endl;
  183.                            system(filenamedonemove);
  184.                         }
  185.  
  186.                         char filetimedone[5000];
  187.                         strcpy (filetimedone,maindir);
  188.                         strcat (filetimedone,"/jobstime/");
  189.                         strcat (filetimedone,entry->d_name);
  190.  
  191.                         char filetimeremove[5000];
  192.                         strcpy (filetimeremove,"rm ");
  193.                         strcat (filetimeremove,maindir);
  194.                         strcat (filetimeremove,"/jobstime/");
  195.                         strcat (filetimeremove,entry->d_name);
  196.  
  197.                         ifstream fin2(filetimedone);
  198.                         if (fin2)
  199.                         {
  200.                            fin2.close();
  201.                            cout << endl << "Time file found, removing..." << filetimeremove << endl;
  202.                            system(filetimeremove);
  203.                         }
  204.                     }
  205.  
  206.             }
  207.         }
  208.         closedir(pDIR);
  209.     }
  210. }
  211.  
  212. int main(int argc, char** argv)
  213. {
  214.     GetCurrentPath(CurrentPath); //Get current path
  215.  
  216.     //Infinite loop
  217.     while (1)
  218.     {
  219.         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.
  220.         getjobs(argc,argv); //Handle all jobs in the "jobs" folder
  221.         sleep(5);
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement