Class: Couchbase::Timer
- Inherits:
-
Object
- Object
- Couchbase::Timer
- Defined in:
- ext/couchbase_ext/couchbase_ext.c
Instance Method Summary collapse
-
#cancel ⇒ String
Cancel the timer.
-
#initialize(*args) {|timer| ... } ⇒ Couchbase::Timer
constructor
Initialize new Timer.
-
#inspect ⇒ String
Returns a string containing a human-readable representation of the Timer.
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.
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
#cancel ⇒ String
Cancel the timer.
This operation makes sense for periodic timers or if one need to cancel regular timer before it will be triggered.
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;
}
|
#inspect ⇒ String
Returns a string containing a human-readable representation of the Timer.
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;
}
|