Example #1
0
int main()
{
    struct job Ajob(5), Bjob(6);
    struct queue *Queue;
    //Ajob.j_id = 5;
    //Ajob->j_prev = Ajob->j_next =NULL;
    //Bjob.j_id = 6;
    //Bjob->j_prev = Bjob->j_next =NULL;
    queue_init(Queue);
    job_insert(Queue, &Ajob);
    job_insert(Queue, &Bjob);   
    job_find(Queue, 6);

}
Example #2
0
void main()
{
    struct queue Q;
    int i;
    int num[10]={1,2,3,4,5,6,7,8,9,10};
    queue_init(&Q);
    for(i=0;i<5;++i){
	struct job *pj;
	int err,ntid;
	pj=(struct job *)malloc(sizeof(struct job));
	err=pthread_create(&ntid,NULL,thr_fn,num+i);
	if(err != 0)
	    err_quit("can't create thread:%s \n",strerror(err));
	pj->j_id=ntid;
	job_insert(&Q,pj);
    }
    for(i=5;i<10;++i){
	struct job *pj;
	int err,ntid;
	pj=(struct job *)malloc(sizeof(struct job));
	err=pthread_create(&ntid,NULL,thr_fn,num+i);
	if(err != 0)
	    err_quit("can't create thread:%s \n",strerror(err));
	pj->j_id=ntid;
	job_append(&Q,pj);
    }
    sleep(2);

}
Example #3
0
/* process new job request from web */
static int __process_web_job(char * buf, int size, int ent_id)
{
    logdebug(_("%s called\n"), __func__);

    if (size != sizeof(int32_t)) {
        logerror(_("unexpected web job data size\n"));
        return -1;
    }

    int job_id = *(int32_t *)buf;
    logdebug(_("web job id %d\n"), job_id);

    LYJobInfo *job = malloc(sizeof(LYJobInfo));
    if (job == NULL) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        return -1;
    }

    job->j_id = job_id;
    if (db_job_get(job) != 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        free(job);
        return -1;
    }

    if (job_exist(job)){
        logwarn(_("job %d exists already\n"), job_id);
        free(job);
        return 0;
    }

    int ret = job_check(job);
    if (ret){
        logwarn(_("job check for job %d returns %d\n"), job_id, ret);
        if (!JOB_IS_CANCELLED(ret))
            ret = LY_S_CANCEL_INTERNAL_ERROR;
        /* can not use job_remove */
        time(&job->j_started);
        time(&job->j_ended);
        job->j_status = ret;
        db_job_update_status(job);
        free(job);
        return 0;
    }

    if (job_insert(job) != 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        time(&job->j_started);
        time(&job->j_ended);
        job->j_status = LY_S_CANCEL_INTERNAL_ERROR;
        db_job_update_status(job);
        free(job);
        return -1;
    }

    return 0;
}
Example #4
0
int db_job_get_all(void)
{
    char sql[LINE_MAX];
    if (snprintf(sql, LINE_MAX,
                 "SELECT id, status, "
                 "extract(epoch FROM created), "
                 "extract(epoch FROM started), "
                 "target_type, target_id, action "
                 "from job "
                 "where (status >= %d and status < %d) or status = %d;",
                 LY_S_INITIATED, LY_S_RUNNING_LAST_STATUS,
                 LY_S_PENDING) >= LINE_MAX) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        return -1;
    }
    PGresult * res = __db_select(sql);
    if (res == NULL)
        return -1;

    int ret = PQntuples(res);
    int r;
    for (r = 0; r < ret; r++) {
        LYJobInfo * job = malloc(sizeof(LYJobInfo));
        if (job == NULL)
            return -1;
        job->j_id = atoi(PQgetvalue(res, r, 0));
        job->j_status = atoi(PQgetvalue(res, r, 1));
        job->j_created = atol(PQgetvalue(res, r, 2));
        job->j_started = atol(PQgetvalue(res, r, 3));
        job->j_target_type = atoi(PQgetvalue(res, r, 4));
        job->j_target_id = atoi(PQgetvalue(res, r, 5));
        job->j_action = atoi(PQgetvalue(res, r, 6));
        job_insert(job);
    }

    PQclear(res);
    return ret;
}
Example #5
0
// Readrec5 is like readrec, but it reads a record in "version 5"
// of the log format.
static int
readrec5(File *f, job l, int *err)
{
    int r, sz = 0;
    size_t namelen;
    Jobrec5 jr;
    job j;
    tube t;
    char tubename[MAX_TUBE_NAME_LEN];

    r = read(f->fd, &namelen, sizeof(namelen));
    if (r == -1) {
        twarn("read");
        warnpos(f, 0, "error");
        *err = 1;
        return 0;
    }
    if (r != sizeof(namelen)) {
        return 0;
    }
    sz += r;
    if (namelen >= MAX_TUBE_NAME_LEN) {
        warnpos(f, -r, "namelen %zu exceeds maximum of %d", namelen, MAX_TUBE_NAME_LEN - 1);
        *err = 1;
        return 0;
    }

    if (namelen) {
        r = readfull(f, tubename, namelen, err, "v5 tube name");
        if (!r) {
            return 0;
        }
        sz += r;
    }
    tubename[namelen] = '\0';

    r = readfull(f, &jr, Jobrec5size, err, "v5 job struct");
    if (!r) {
        return 0;
    }
    sz += r;

    // are we reading trailing zeroes?
    if (!jr.id) return 0;

    j = job_find(jr.id);
    if (!(j || namelen)) {
        // We read a short record without having seen a
        // full record for this job, so the full record
        // was in an eariler file that has been deleted.
        // Therefore the job itself has either been
        // deleted or migrated; either way, this record
        // should be ignored.
        return 1;
    }

    switch (jr.state) {
    case Reserved:
        jr.state = Ready;
    case Ready:
    case Buried:
    case Delayed:
        if (!j) {
            if (jr.body_size > job_data_size_limit) {
                warnpos(f, -r, "job %"PRIu64" is too big (%"PRId32" > %zu)",
                        jr.id,
                        jr.body_size,
                        job_data_size_limit);
                goto Error;
            }
            t = tube_find_or_make(tubename);
            j = make_job_with_id(jr.pri, jr.delay, jr.ttr, jr.body_size,
                                 t, jr.id);
            j->next = j->prev = j;
            j->r.created_at = jr.created_at;
        }
        j->r.id = jr.id;
        j->r.pri = jr.pri;
        j->r.delay = jr.delay * 1000; // us => ns
        j->r.ttr = jr.ttr * 1000; // us => ns
        j->r.body_size = jr.body_size;
        j->r.created_at = jr.created_at * 1000; // us => ns
        j->r.deadline_at = jr.deadline_at * 1000; // us => ns
        j->r.reserve_ct = jr.reserve_ct;
        j->r.timeout_ct = jr.timeout_ct;
        j->r.release_ct = jr.release_ct;
        j->r.bury_ct = jr.bury_ct;
        j->r.kick_ct = jr.kick_ct;
        j->r.state = jr.state;
        job_insert(l, j);

        // full record; read the job body
        if (namelen) {
            if (jr.body_size != j->r.body_size) {
                warnpos(f, -r, "job %"PRIu64" size changed", j->r.id);
                warnpos(f, -r, "was %"PRId32", now %"PRId32, j->r.body_size, jr.body_size);
                goto Error;
            }
            r = readfull(f, j->body, j->r.body_size, err, "v5 job body");
            if (!r) {
                goto Error;
            }
            sz += r;

            // since this is a full record, we can move
            // the file pointer and decref the old
            // file, if any
            filermjob(j->file, j);
            fileaddjob(f, j);
        }
        j->walused += sz;
        f->w->alive += sz;

        return 1;
    case Invalid:
        if (j) {
            job_remove(j);
            filermjob(j->file, j);
            job_free(j);
        }
        return 1;
    }

Error:
    *err = 1;
    if (j) {
        job_remove(j);
        filermjob(j->file, j);
        job_free(j);
    }
    return 0;
}