Class: Couchbase::Timer

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|timer| ... } ⇒ Couchbase::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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'ext/couchbase_ext/timer.c', line 162

VALUE
cb_timer_init(int argc, VALUE *argv, VALUE self)
{
    struct cb_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 (!RTEST(rb_obj_is_kind_of(bucket, cb_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, cb_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;
}

Instance Method Details

#cancelString

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



97
98
99
100
101
102
103
# File 'ext/couchbase_ext/timer.c', line 97

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

#inspectString

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

Returns:

  • (String)

Since:

  • 1.2.0.dp6



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/couchbase_ext/timer.c', line 55

VALUE
cb_timer_inspect(VALUE self)
{
    VALUE str;
    struct cb_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;
}