Ejemplo n.º 1
0
/*
 * Initialize new Timer
 *
 * @since 1.2.0
 *
 * The timers could used to trigger reccuring events or implement timeouts.
 * The library will call given block after time interval pass.
 *
 * @param bucket [Bucket] the connection object
 * @param interval [Fixnum] the interval in microseconds
 * @param options [Hash]
 * @option options [Boolean] :periodic (false) set it to +true+ if the timer
 *   should be triggered until it will be canceled.
 *
 * @yieldparam [Timer] timer the current timer
 *
 * @example Create regular timer for 0.5 second
 *   c.run do
 *     Couchbase::Timer.new(c, 500000) do
 *       puts "ding-dong"
 *     end
 *   end
 *
 * @example Create periodic timer
 *   n = 10
 *   c.run do
 *     Couchbase::Timer.new(c, 500000, :periodic => true) do |tm|
 *       puts "#{n}"
 *       n -= 1
 *       tm.cancel if n.zero?
 *     end
 *   end
 *
 *
 * @return [Couchbase::Timer]
 */
    VALUE
cb_timer_init(int argc, VALUE *argv, VALUE self)
{
    struct timer_st *tm = DATA_PTR(self);
    VALUE bucket, opts, timeout, exc, cb;
    lcb_error_t err;

    rb_need_block();
    rb_scan_args(argc, argv, "21&", &bucket, &timeout, &opts, &cb);

    if (CLASS_OF(bucket) != cBucket) {
        rb_raise(rb_eTypeError, "wrong argument type (expected Couchbase::Bucket)");
    }
    tm->self = self;
    tm->callback = cb;
    tm->usec = NUM2ULONG(timeout);
    tm->bucket = DATA_PTR(bucket);
    if (opts != Qnil) {
        Check_Type(opts, T_HASH);
        tm->periodic = RTEST(rb_hash_aref(opts, sym_periodic));
    }
    tm->timer = lcb_timer_create(tm->bucket->handle, tm, tm->usec,
            tm->periodic, timer_callback, &err);
    exc = cb_check_error(err, "failed to attach the timer", Qnil);
    if (exc != Qnil) {
        rb_exc_raise(exc);
    }

    return self;
}
Ejemplo n.º 2
0
static lcb_error_t bootstrap_common(lcb_t instance, int initial)
{
    struct lcb_bootstrap_st *bs = instance->bootstrap;

    if (bs && bs->active) {
        return LCB_SUCCESS;
    }

    if (!bs) {
        bs = calloc(1, sizeof(*instance->bootstrap));
        if (!bs) {
            return LCB_CLIENT_ENOMEM;
        }

        instance->bootstrap = bs;
        bs->parent = instance;
        lcb_confmon_add_listener(instance->confmon, &bs->listener);
    }

    bs->last_refresh = gethrtime();
    bs->active = 1;

    if (initial) {
        lcb_error_t err;
        bs->listener.callback = config_callback;
        bs->timer = lcb_timer_create(instance, NULL,
                                     instance->settings.config_timeout, 0,
                                     initial_timeout, &err);
        if (err != LCB_SUCCESS) {
            return err;
        }

    } else {
        /** No initial timer */
        bs->listener.callback = async_step_callback;
    }

    return lcb_confmon_start(instance->confmon);
}