Exemplo n.º 1
0
/**
 * SSH Job: Initialize
 */
static int
ngislSSHjobInitialize(
    ngisJob_t *job,
    ngisOptionContainer_t *opts)
{
    ngisSSHjob_t *sshJob = (ngisSSHjob_t *)job;
    ngisSSHjobAttributes_t *attr = NULL;
    char *jobID = NULL;
    int result;
    pid_t pid = -1;
    ngisLog_t *log = NULL;
    static const char fName[] = "ngislSSHjobInitialize";
    
    NGIS_ASSERT(sshJob != NULL);
    NGIS_ASSERT(opts   != NULL);

    log = job->ngj_log;
    
    ngislSSHjobInitializeMember(sshJob);

    /* Function Queue */
    NGIS_LIST_INITIALIZE(ngisSSHjobFunc_t, &sshJob->ngsj_funcQueue);
    
    /* Create SSH Job Attribute from Invoke Server Options */
    attr = ngisSSHjobAttributesCreate(opts);
    if (attr == NULL) {
        ngisErrorPrint(log, fName, "Can't create SSH job attributes.\n");
        goto error;
    }
    sshJob->ngsj_attributes = attr;
    ngislSSHjobWarnIgnoredOptions(sshJob, opts);

    /* Set JobManager*/
    if ((attr->ngsja_jobManager == NULL) ||
        (strcmp(attr->ngsja_jobManager, "jobmanager-fork") == 0)) {
        sshJob->ngsj_jobManagerType = NGIS_SSH_JOB_MANAGER_NORMAL;
    } else if (strcmp(attr->ngsja_jobManager, "jobmanager-sge") == 0) {
        sshJob->ngsj_jobManagerType = NGIS_SSH_JOB_MANAGER_SGE;
    } else if (strcmp(attr->ngsja_jobManager, "jobmanager-pbs") == 0) {
        sshJob->ngsj_jobManagerType = NGIS_SSH_JOB_MANAGER_PBS;
    } else {
        ngisErrorPrint(log, fName, "Invalid job manager.\n");
        goto error;
    }
    
    /* Number of the job of queuing system */
    switch (attr->ngsja_jobBackend) {
    case NGIS_BACKEND_NORMAL:
        switch(sshJob->ngsj_jobManagerType) {
        case NGIS_SSH_JOB_MANAGER_PBS:
        case NGIS_SSH_JOB_MANAGER_SGE:
            sshJob->ngsj_nExecutables = 1;
            break;
        case NGIS_SSH_JOB_MANAGER_NORMAL:
            sshJob->ngsj_nExecutables = attr->ngsja_count;
            break;
        }
        break;
    case NGIS_BACKEND_MPI:
    case NGIS_BACKEND_BLACS:
        sshJob->ngsj_nExecutables = 1;
        break;
    default:
        NGIS_ASSERT_NOTREACHED();
    }

    /* Create Executable Handle */
    sshJob->ngsj_executables =
        ngislExecutableCreate(sshJob->ngsj_nExecutables, log);
    if (sshJob->ngsj_executables == NULL) {
        ngisErrorPrint(log, fName, "Can't create the executables.\n");
        goto error;
    }

    /* Execute SSH */
    pid = ngislSSHjobExecuteSSH(sshJob);
    if (pid < 0) {
        ngisErrorPrint(log, fName, "Can't execute SSH.\n");
        goto error;
    }

    /* Create Line Buffer */
    sshJob->ngsj_lBufStdout =
        ngisLineBufferCreate(sshJob->ngsj_stdio.ngsio_out, "\n");
    if (sshJob->ngsj_lBufStdout == NULL) {
        ngisErrorPrint(log, fName,
            "Can't create line buffer for the STDOUT.\n");
        goto error;
    }

    sshJob->ngsj_lBufStderr =
        ngisLineBufferCreate(sshJob->ngsj_stdio.ngsio_error, "\n");
    if (sshJob->ngsj_lBufStderr == NULL) {
        ngisErrorPrint(log, fName,
            "Can't create line buffer for the STDERR.\n");
        goto error;
    }

    /* Register function for reading STDERR */
    result = ngisLineBufferReadLine(
        sshJob->ngsj_lBufStderr, ngisSSHjobErrorCallback, sshJob);
    if (result == 0) {
        ngisErrorPrint(log, fName,
            "Can't register callback for reading STDERR.\n");
        goto error;
    }

    /* Prepare submitting the job */
    switch (sshJob->ngsj_jobManagerType) {
    case NGIS_SSH_JOB_MANAGER_NORMAL:
        result = ngisSSHjobPrepare(sshJob);
        break;
    case NGIS_SSH_JOB_MANAGER_SGE:
        result = ngisSSHjobSGEprepare(sshJob);
        break;
    case NGIS_SSH_JOB_MANAGER_PBS:
        result = ngisSSHjobPBSprepare(sshJob);
        break;
    default:
        NGIS_ASSERT_NOTREACHED();
    }
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't prepare.\n");
        goto error;
    }

    /* Job ID is process ID of SSH */
    jobID = ngisStrdupPrintf("%ld", (long)pid);
    if (jobID == NULL) {
        ngisErrorPrint(log, fName, "Can't create Job ID.\n");
        goto error;
    }

    result = ngisJobRegisterID(job, jobID);    
    if (result == 0) {
        ngisErrorPrint(log, fName,
            "Can't register Job ID to protocol.\n");
        goto error;
    }

    result = ngisSSHjobFunctionPop(sshJob);
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't execute the next process.\n");
        goto error;
    }

    NGIS_NULL_CHECK_AND_FREE(jobID);
 
    return 1;

    /* Error occurred */
error:
    NGIS_NULL_CHECK_AND_FREE(jobID);

    result = ngislSSHjobFinalize(job);
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't finalize the ssh job.\n");
    }
    return 0;
}
Exemplo n.º 2
0
static int
ngislSSHjobPBScreateScript(
    ngisSSHjob_t *job)
{
    char *filename = NULL;
    FILE *fp = NULL;
    int result;
    int ret = 1;
    ngisLog_t *log;
    static const char fName[] = "ngislSSHjobPBScreateScript";

    NGIS_ASSERT(job != NULL);

    log = job->ngsj_log;

    if ((job->ngsj_attributes->ngsja_jobBackend == NGIS_BACKEND_NORMAL) && 
        (job->ngsj_attributes->ngsja_count > 1)) {
        /* For Array Job */
        result = ngisSSHjobCreateScriptForArrayJob(job);
        if (result == 0) {
            ngisErrorPrint(log, fName, "Can't create script for array job.\n");
            ret = 0;
            goto finalize;
        }
    }

    filename = ngisMakeTempFile(job->ngsj_attributes->ngsja_tmpDir);
    if (filename == NULL) {
        ngisErrorPrint(log, fName, "Can't make temporary file.\n");
        ret = 0;
        goto finalize;
    }
    job->ngsj_localScriptName = filename;

    fp = fopen(filename, "w");
    if (fp == NULL) {
        ngisErrorPrint(log, fName, "Can't create FILE structure.\n");
        ret = 0;
        goto finalize;
    }

    result = ngislSSHjobPBSwriteScript(job, fp);
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't write script to file.\n");
        ret = 0;
        goto finalize;
    }
    
finalize:
    /* Close */
    if (fp != NULL) {
        result = fclose(fp);
        if (result != 0) {
            ngisErrorPrint(log, fName, "fclose: %s.\n", strerror(errno));
            ret = 0;
        }
        fp = NULL;
    }

    ngisLogDumpFile(log, NGIS_LOG_LEVEL_DEBUG,
        fName, job->ngsj_localScriptName);

    if (ret != 0) {
        result = ngisSSHjobFunctionPop(job);
        if (result == 0) {
            ngisErrorPrint(log, fName, "Can't execute the next process.\n");
            ret = 0;
        }
    }
    return ret;
}
Exemplo n.º 3
0
int
ngisSSHjobProcessAfterExit(
    ngisSSHjob_t *job)
{
    int result;
    ngisLog_t *log = NULL;
    int i;
    ngisExecutable_t *exe = NULL;
    ngisExecutableStatus_t jobStatus;
    static const char fName[] = "ngisSSHjobProcessAfterExit";
    
    NGIS_ASSERT(job != NULL);

    log  = job->ngsj_log;
    jobStatus = ngisSSHjobGetStatus(job);

    ngisSSHjobFunctionReset(job);
    job->ngsj_iExecutables = 0;

    /* Stop polling */
    if (ngisCallbackIsValid(job->ngsj_timerCallback)) {
        ngisCallbackCancel(job->ngsj_timerCallback);
        job->ngsj_timerCallback = NULL;
    }

    if (job->ngsj_afterExit != 0) {
        goto error;
    }
    job->ngsj_afterExit = 1;

    if ((job->ngsj_attributes->ngsja_redirectEnable != 0) &&
        (jobStatus > NGIS_EXECUTABLE_STATUS_SUBMITTING)   &&
        (jobStatus < NGIS_EXECUTABLE_STATUS_STAGEOUT)) {

        for (i = 0;i < job->ngsj_nExecutables;++i) {
            exe = &job->ngsj_executables[i];
            if (exe->nge_status == NGIS_EXECUTABLE_STATUS_FAILED) {
                result = ngisSSHjobExecutableSetStatus(
                    job, exe, NGIS_EXECUTABLE_STATUS_FAILED_STAGEOUT);
            } else {
                result = ngisSSHjobExecutableSetStatus(
                    job, exe, NGIS_EXECUTABLE_STATUS_STAGEOUT);
            }
            if (result == 0) {
                ngisErrorPrint(log, fName, "Can't set status.\n");            
                goto error;
            }
        }

        /* Redirect outerr */
        result = ngisSSHjobFunctionPush(job, ngislSSHjobTransferStdout);
        if (result == 0) {
            ngisErrorPrint(log, fName,
                "Can't register the function for transfer STDOUT.\n");
            goto error;
        }
        result = ngisSSHjobFunctionPush(job, ngislSSHjobTransferStderr);
        if (result == 0) {
            ngisErrorPrint(log, fName,
                "Can't register the function for transfer STDERR.\n");
            goto error;
        }
    } 

    result = ngisSSHjobFunctionPush(job, ngisSSHjobDone);
    if (result == 0) {
        ngisErrorPrint(log, fName,
            "Can't register the function for transfer job done.\n");
        goto error;
    }

    result = ngisSSHjobFunctionPop(job);
    if (result == 0) {
        ngisErrorPrint(NULL, fName,
            "Fails the registered function.\n");
        return 0;
    }
    
    return 1;
error:
    result = ngisSSHjobDone(job);
    if (result == 0) {
        ngisErrorPrint(NULL, fName, "Can't done the job.\n");
    }
    return 0;
}