"""
This is the file in which all constants relative to the Condor-IPP
framework are defined for the chip stage.

It defines the following constants:
- REQUIREMENTS: The Condor requirements to run Chip jobs
- EMAIL_SUBJECT_FAILED_REVERT: The e-mail subject which is sent when
  revert has failed
- EMAIL_BODY_FAILED_REVERT: The e-mail body which is sent when revert
  has failed
 
"""
from ipp.exceptions.UninstantiableIppException import UninstantiableIppException
from ipp.constants.Globals import Globals as GlobalConstants

class Chip:
    """
    This class is not intended to be instantiated.
    All its members are static.
    """
    def __init__(self):
        raise UninstantiableIppException()

    # Change it according to new needs
    REQUIREMENTS = GlobalConstants.REQUIREMENTS

    # This is the e-mail body which is sent when revert has failed. It
    # is expected to valuate this expression with two parameters:
    # - #1: The label (string)
    # - #2: The exposure name (string)
    EMAIL_SUBJECT_FAILED_REVERT = 'Chip stage failed for label %s / exposure %s)'

    # This is the e-mail body which is sent when revert has
    # failed. Some values need to be replaced, namely:
    # - @COUNT_FAILED_CHIPS@: the number of chips which have failed;
    # - @LABEL@: the label;
    # - @EXPOSURE_NAME@: the exposure name;
    # - @LIST_PROCESSING_LOGS@: the list of log files that should be
    #   checked by the operator;
    # - @CHIPTOOL_UPDATEPROCESSEDIMFILE@: the list of commands that
    #   could be run to ignore some flaky OTAs
    # - @CHIPTOOL_REVERTPROCESSEDIMFILE@: the command that shoudl be
    #   run to revert the exposure.
    # It is also expected to valuate this expression with one argument:
    # - #1: The path to Globals.LOCAL_TMP_DIR
    EMAIL_BODY_FAILED_REVERT = """Hi,

this is an e-mail generated by the ippdor subsystem.

@COUNT_FAILED_CHIPS@ chips have a fault after 3 revert attempts.

You can identify them by running the query:
SELECT exp_name, chip_id, class_id, chipProcessedImfile.fault, path_base FROM chipProcessedImfile JOIN chipRun USING(chip_id) JOIN rawExp ON chipRun.exp_id = rawExp.exp_id WHERE chipRun.label = '@LABEL@' AND exp_name = '@EXPOSURE_NAME@' AND chipProcessedImfile.fault!=0;

You can have a look at the processing logs:
@LIST_PROCESSING_LOGS@

Please fix those faults.

Note that some people will use:
@CHIPTOOL_UPDATEPROCESSEDIMFILE@
and then:
@CHIPTOOL_REVERTPROCESSEDIMFILE@

Once you have FIXED ALL PROBLEMS FOR ALL EXPOSURES, on the host where the Condor task was submitted, submit the DAG (the host name is shown in the following line) again:
condor_submit_dag -maxidle 1000 -maxjobs 500 %s/@LABEL@/ch2st.dag

Thanks,

"""
