コード例 #1
0
ファイル: Utils.py プロジェクト: mobyle2-legacy/mobyle2.core
def emailResults(cfg, userEmail, registry, ID, job_path, serviceName, jobKey, FileName=None):
    """
    @param cfg: the configuration of Mobyle    
    @type cfg: Config instance
    @param userEmail: the user email address
    @type userEmail: EmailAddress instance
    @param registry: the registry of deployed services
    @type registry: Registry.registry object
    @param ID: the ID of the job
    @type ID: string
    @param job_path: the absolute path to the job 
    @type job_path: string
    @param serviceName: the name of the service
    @type serviceName: string
    @param jobKey: the key of the job
    @type jobKey: string
    @param FileName: the absolute path of zip file to attach to the email
    @type FileName: string or None
    """
    from Mobyle.Net import Email
    from Mobyle.MobyleError import EmailError, TooBigError
    import os

    dont_email_result, maxmailsize = cfg.mailResults()
    if dont_email_result:
        return
    else:
        if userEmail:
            mail = Email(userEmail)
            jobInPortalUrl = "%s/portal.py#jobs::%s" % (cfg.cgi_url(), registry.getJobPID(ID))

            if FileName is not None:
                zipSize = os.path.getsize(FileName)
                mailDict = {
                    "SENDER": cfg.sender(),
                    "HELP": cfg.mailHelp(),
                    "SERVER_NAME": cfg.portal_url(),
                    "JOB_URL": jobInPortalUrl,
                    "RESULTS_REMAIN": cfg.remainResults(),
                    "JOB_NAME": serviceName,
                    "JOB_KEY": jobKey,
                }
                if zipSize > maxmailsize - 2048:
                    # 2048 octet is an estimated size of email headers
                    try:
                        mail.send("RESULTS_TOOBIG", mailDict)
                        return
                    except EmailError, err:
                        msg = str(err)
                        adm = Admin(job_path)
                        adm.setMessage(msg)
                        adm.commit()
                        u_log.error("%s/%s : %s" % (serviceName, jobKey, msg))
                        return
                else:
                    try:
                        mail.send("RESULTS_FILES", mailDict, files=[FileName])
                        return
                    except TooBigError, err:
                        try:
                            mail.send("RESULTS_TOOBIG", mailDict)
                        except EmailError, err:
                            msg = str(err)
                            adm = Admin(job_path)
                            adm.setMessage(msg)
                            adm.commit()
                            u_log.error("%s/%s : %s" % (serviceName, jobKey, msg))

                        return