Exemple #1
0
/*
 * create a recycle item and return
 */
mx_recycle_item_t *mx_recycle_push()
{
    mx_recycle_item_t *item = malloc(sizeof(*item));
    
    if (item) {
        item->id = mx_daemon->recycle->last_id++;
        item->expire = mx_current_time + mx_daemon->recycle_expire;
        if (mx_skiplist_insert(mx_daemon->recycle->recycle, item->id, item) != SKL_STATUS_OK) {
            free(item);
            return NULL;
        }
        mx_recycle_inc();
    }
    return item;
}
Exemple #2
0
int mx_load_queues()
{
    struct mx_job_header header;
    mx_queue_t *queue;
    mx_job_t *job;
    time_t current_time = time(NULL);
    int count = 0;
    char tbuf[128];
    FILE *fp;
    int retval;

    if (!mx_global->bgsave_filepath || 
        !(fp = fopen(mx_global->bgsave_filepath, "rb")))
    {
        return 0;
    }

    if (fread(tbuf, sizeof(MX_BGSAVE_HEADER) - 1, 1, fp) != 1) {
        goto failed;
    }

    if (strncmp(tbuf, MX_BGSAVE_HEADER, sizeof(MX_BGSAVE_HEADER) - 1) != 0) {
        mx_write_log(mx_log_debug, "(%s) was a invaild database file", mx_global->bgsave_filepath);
        fclose(fp);
        return -1;
    }

    while (1)
    {
        if (fread(&header, sizeof(header), 1, fp) != 1) {
            goto failed;
        }

        /* finish and break */
        if (header.qlen == 0 || header.jlen == 0) {
            break;
        }

        if (fread(tbuf, header.qlen, 1, fp) != 1) {
            goto failed;
        }

        tbuf[header.qlen] = 0;

        /* find the queue from queue table */
        if (hash_lookup(mx_global->queue_table, tbuf, (void **)&queue) == -1)
        {
            /* not found and create it */
            if (!(queue = mx_queue_create(tbuf, header.qlen))) {
                goto failed;
            }

            if (hash_insert(mx_global->queue_table, tbuf, queue) != 0) {
                goto failed;
            }
        }

        job = mx_job_create(queue, header.prival, header.timeout, header.jlen);
        if (!job) {
            goto failed;
        }

        if (fread(job->body, job->length, 1, fp) != 1) {
            goto failed;
        }

        job->body[job->length] = CR_CHR;
        job->body[job->length+1] = LF_CHR;

        if (job->timeout > 0 && job->timeout > current_time) {
            retval = mx_skiplist_insert(mx_global->delay_queue, job->timeout, job);

        } else {
            job->timeout = 0;
            retval = mx_skiplist_insert(queue->list, job->prival, job);
        }

        if (retval != 0) {
            goto failed;
        }

        count++;
    }

    mx_write_log(mx_log_debug, "finish load (%d)jobs from disk", count);
    fclose(fp);
    return 0;

failed:
    mx_write_log(mx_log_error, "failed to read jobs from disk, message(%s)", strerror(errno));
    fclose(fp);
    return -1;
}