Class: Couchbase::Timer

Inherits:
Object
  • Object
show all
Defined in:
ext/couchbase_ext/couchbase_ext.c

Instance Method Summary (collapse)

Constructor Details

- (Couchbase::Timer) initialize {|timer| ... }

Initialize new Timer

The timers could used to trigger reccuring events or implement timeouts. The library will call given block after time interval pass.

Examples:

Create regular timer for 0.5 second

c.run do
  Couchbase::Timer.new(c, 500000) do
    puts "ding-dong"
  end
end

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

Parameters:

  • bucket (Bucket)

    the connection object

  • interval (Fixnum)

    the interval in microseconds

  • options (Hash)

Yield Parameters:

  • timer (Timer)

    the current timer

Since:

  • 1.2.0



4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
# File 'ext/couchbase_ext/couchbase_ext.c', line 4250

static VALUE
cb_timer_init(int argc, VALUE *argv, VALUE self)
{
    struct timer_st *tm = DATA_PTR(self);
    VALUE bucket, opts, timeout, exc, cb;
    libcouchbase_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 = libcouchbase_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;
}

Instance Method Details

- (String) cancel

Cancel the timer.

This operation makes sense for periodic timers or if one need to cancel regular timer before it will be triggered.

Examples:

Cancel periodic timer

n = 1
c.run do
  tm = c.create_periodic_timer(500000) do
    c.incr("foo") do
      if n == 5
        tm.cancel
      else
        n += 1
      end
    end
  end
end

Returns:

  • (String)

Since:

  • 1.2.0.dp6



4185
4186
4187
4188
4189
4190
4191
# File 'ext/couchbase_ext/couchbase_ext.c', line 4185

static VALUE
cb_timer_cancel(VALUE self)
{
    struct timer_st *tm = DATA_PTR(self);
    libcouchbase_timer_destroy(tm->bucket->handle, tm->timer);
    return self;
}

- (String) inspect

Returns a string containing a human-readable representation of the Timer.

Returns:

  • (String)

Since:

  • 1.2.0.dp6



4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
# File 'ext/couchbase_ext/couchbase_ext.c', line 4143

static VALUE
cb_timer_inspect(VALUE self)
{
    VALUE str;
    struct timer_st *tm = DATA_PTR(self);
    char buf[200];

    str = rb_str_buf_new2("#<");
    rb_str_buf_cat2(str, rb_obj_classname(self));
    snprintf(buf, 20, ":%p", (void *)self);
    rb_str_buf_cat2(str, buf);
    snprintf(buf, 100, " timeout:%u periodic:%s>",
            tm->usec, tm->periodic ? "true" : "false");
    rb_str_buf_cat2(str, buf);

    return str;
}