open-source Notes

Notes of an open-source programmer.
06 Dec

How To: create your own work generator script (part 3)

This part will cover the whole main_loop() function of the work generator that I’m trying to build during this HowTo. The first two parts explained the header definitions and the make_job() function. This part now is the real working function that gets called at the end of main and processes the input files until it is terminated by an error which will lead to the termination of the whole generator.
Here is the the code of this function:

void main_loop() {
    std::string file;
    int retval;

    while (1) {
        check_stop_daemons();
        int n;
        retval = count_unsent_results(n, 0);
        if (n > CUSHION) {
            sleep(60);
        } else {
            DirScanner dirscan(INPUT_FOLDER);
            // check if there are files in INPUT_FOLDER
            if (dirscan.scan(file)) {
                // files found, now create work for them
                int njobs = (CUSHION-n)/REPLICATION_FACTOR;
                log_messages.printf(MSG_DEBUG,
                    "Making %d jobs\n", njobs
                );
                for (int i=0; i<njobs; i++) {
                    retval = make_job(file.c_str());
                    if (retval) {
                        log_messages.printf(MSG_CRITICAL,
                            "can't make job: %d\n", retval
                        );
                        exit(retval);
                    }
                    if (!dirscan.scan(file)) break; // no more files but CUSHION not yet reached
                }
            } else {
                // no more files found, wait some time
                log_messages.printf(MSG_DEBUG,
                        "no more input files in: %s\n", INPUT_FOLDER
                );
                sleep(60); //increase this time if you can't supply a steady stream of input files
            }
            // Now sleep for a few seconds to let the transitioner
            // create instances for the jobs we just created.
            // Otherwise we could end up creating an excess of jobs.
            sleep(5);
        }
    }
}

As the name already says this is the main loop that’s trying to do work generation. The big while loop will run until some error occurs and the exit() function is called. What it does is realy simple (once you understand it):

First it checks if it should run at all (check_stop_daemons()) and then retrieves the number of unsent tasks in the database (count_unsent_results()). If this is greater than the cushion we set in the header section it will wait a minute and than retrieve this value again.

If there are not enough tasks we will now scan the input directory for files and if found generate some jobs. If there are no files in the input dir we will just wait a minute and look again. You should increase this time or comment out the debug message if it bothers you.

The work generation loop is also not very complicated. It determines how many jobs have to be created (Note that if you create 10 jobs with a replication factor of 2 you get 20 tasks!) and calls the former described function make_job() for each input file. The class DirScanner comes from lib/filesys.h and is used by other BOINC daemons also. That’s all about the main_loop() function.

If you want to know from where this function is called you have to read the article about the main function (yet to be written).

Read the previous parts of this How To: the header of the generator and the make_job() function
Read the next part of this How To: the main function.

Leave a Reply

404 Not Found

404 Not Found


nginx/0.8.53

© 2012 open-source Notes | Entries (RSS) and Comments (RSS)

GPS Reviews and news from GPS Gazette

404 Not Found


nginx/0.8.53
" title="Powered by Wordpress">wordpress logo