int main (int argc, char **argv )
{
    int  rc;
    char action[20];
    char jid_s[20];
    int  jid = 0;
    char contact[500];
    char rsl_file[1024];
    int  status = -1;
    char info[500];
    int end = 0;
    fd_set in_pipes;
    int j;
    char c;
    char str[4096];
    struct timeval tv;
    int timer_interval = 300;
    time_t last_timer = 0;
    time_t the_time;

    struct timeval t1,t2;
    double waited;

    setbuf(stdout,NULL);

    rc = globus_module_activate(GLOBUS_COMMON_MODULE);

    if ( rc != GLOBUS_SUCCESS )
        return -1;

    waited = 0;

    while (!end)
    {
        FD_ZERO(&in_pipes);
        FD_SET (0,&in_pipes);

        tv.tv_sec  = 0;
        tv.tv_usec = 1000;

        gettimeofday(&t1, NULL);

        rc = select(1, &in_pipes, NULL, NULL, &tv);

        gettimeofday(&t2, NULL);

        waited += ((t2.tv_sec - t1.tv_sec)*1000000) + (t2.tv_usec - t1.tv_usec);

        if ( waited > 999 )
        {
            globus_poll();
            waited = 0;
        }

        if (rc == -1)
        {
            exit(-1);
        }
        else if (rc == 1)
        {
            j = 0;

            do
            {
                rc = read(0, (void *) &c, sizeof(char));
                str[j++] = c;
            }
            while ( rc > 0 && c != '\n' );

            str[j] = '\0';

            if (rc <= 0)
                exit(-1);

            rc = sscanf(str, "%s %s %s %[^\n]", action, jid_s, contact,
                        rsl_file);

            if (rc != 4 )
            {
                printf("FAILURE Not all four arguments defined\n");
                continue;
            }

            jid = atoi(jid_s);

            if (strcmp(action, "INIT") == 0 )
            {
                status = gw_em_mad_init(jid, info);
            }
            else if (strcmp(action, "SUBMIT") == 0 )
            {
                status = gw_em_mad_submit(jid, contact, rsl_file, info);
            }
            else if (strcmp(action, "RECOVER") == 0 )
            {
                status = gw_em_mad_recover(jid, contact, info);
            }
            else if (strcmp(action, "CANCEL") == 0 )
            {
                status = gw_em_mad_cancel(jid, info);
            }
            else if (strcmp(action, "POLL") == 0 )
            {
                status = gw_em_mad_poll(jid, info);
            }
            else if (strcmp(action, "FINALIZE") == 0 )
            {
                status = gw_em_mad_finalize(info);
                end = 1;

                return 0;
            }

            if (status != 0)
                printf("%s %d FAILURE %s\n", action, jid, info);
        }

        the_time = time(NULL);

        if (the_time - last_timer >=  timer_interval)
        {
            last_timer = the_time;

            if (mad.initialized == 1)
            {
                status = gw_em_mad_check_credentials(info);

                if (status != 0)
                    printf("%s %d FAILURE %s\n", action, jid, info);
            }
        }
    }

    return 0;
}
Exemple #2
0
void gw_em_timer()
{
    int i;
    gw_job_t *job;
    time_t now;
    static int mark = 0;
    int *_job_id;
    gw_em_mad_t *mad;
    
    mark = mark + GW_EM_TIMER_PERIOD;
    if ( mark >= 300 )
    {
        gw_log_print("EM",'I',"-- MARK --\n");
    	mark = 0;
    }    
    
    now = time(NULL);

    for (i= 0; i< gw_conf.number_of_jobs ; i++)
    {
        job = gw_job_pool_get(i, GW_TRUE);
        
        if ( job != NULL )
        {   
            if ( job->history == NULL )
            {
                pthread_mutex_unlock(&(job->mutex));
                continue;
            }
        		        	               
            if ( (job->job_state == GW_JOB_STATE_PRE_WRAPPER) 
                    || (job->job_state == GW_JOB_STATE_WRAPPER)  
                    || (job->job_state == GW_JOB_STATE_MIGR_CANCEL)
                    || (job->job_state == GW_JOB_STATE_STOP_CANCEL)
                    || (job->job_state == GW_JOB_STATE_KILL_CANCEL))                                  
            {
            	if (issubmitted(job->em_state))
            	{
                    if ( now >= job->next_poll_time )
                    {
                        gw_log_print("EM",'I',"Checking execution state of job %i.\n", i);
                            
                        mad = job->history->em_mad;

                        /* Warning! When in Migration Cancel, the previous MAD should be used */
                        if (job->job_state == GW_JOB_STATE_MIGR_CANCEL)
                        {
                            if (job->history->next == NULL) 
                            {
                                gw_log_print("EM",'E',"Previous history record of job %i no longer exists\n", i);
                                pthread_mutex_unlock(&(job->mutex));                        
                                continue;
                            } 
                            else
                                mad = job->history->next->em_mad;
                        }                            
                                                     
                        gw_em_mad_poll(mad, i);
                    
                        job->next_poll_time += gw_conf.poll_interval; /* Wait for next poll */
                    }            		
            	}
            	else if ((job->em_state == GW_EM_STATE_FAILED)
               	             && (job->history->counter != -1))
            	{
            		job->history->counter--;
            		
            		if (job->history->counter == 0)
            		{
                           job->history->counter = -1;
            			
            		    _job_id    = (int *) malloc (sizeof(int));
            		    *(_job_id) = i;
            		    
            		    gw_am_trigger(&(gw_em.am),"GW_EM_SUBMIT", _job_id);
            		}
            	}            	            	            	
            }
            
            pthread_mutex_unlock(&(job->mutex));            
        }
    }
}