0 Error handling (cookbook)
David Anderson edited this page 2024-08-19 12:59:47 -07:00

Now we'll add validation to our example app. We'll use replication: each job will have two instances.

The validator will do two things:

  • For each job instance, it checks that the output file is entirely uppercase.
  • When there are two instances that pass this check, it checks whether their output files are identical.

Our validator will use two Python scripts. The first script, validate_init.py, looks at a single output file, checks that it's uppercase, and exits 0 or 1 accordingly.

def is_uc(path):
    with open(path) as f:
        data = f.read()
    return data == data.upper()

exit(0 if is_uc(sys.argv[1]) else 1)

The second script, validate_compare.py, checks whether 2 output files are identical:

def read(path):
    with open(path) as f:
        data = f.read()
    return data

exit(0 if read(sys.argv[1])==read(sys.argv[2]) else 1)

These scripts are in the BOINC source tree, in samples/worker. Copy them to ~/projects/test/bin.

Add the following to ~/projects/test/config.xml, in the <daemons> section:

<daemon>
   <cmd>script_validator --app worker --init_script "validate_init.py" --compare_script "validate_compare.py" </cmd>
   <output>validator_worker.out</output>
   <pid_file>validator_worker.pid</pid_file>
</daemon>

Then edit test/templates/worker_in to set the replication parameters:

    <workunit>
        <min_quorum>2</min_quorum>
        <target_nresults>2</target_nresults>
        <max_success_results>2</max_success_results>
        <max_error_results>3</max_error_results>
        <max_total_results>5</max_total_results>
        <file_ref>
        ...