Welcome! Log In Create A New Profile

Advanced

Writing a timer event (how to execute an event from time to time)

Rogério Schneider
May 25, 2010 01:18AM
Would you be kind enough to help me understanding how to write an
event which executes itself from time to time, using Nginx event
handler?

I would like to register for each request a timer to execute a given
task each 10 seconds.

I am trying this in my module handler:

static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
{
ngx_buf_t *b;
ngx_chain_t out;
int rc;
ngx_event_t *wev;

wev = ngx_pcalloc(r->pool, sizeof(ngx_event_t));

if (wev == NULL) {
ngx_log_stderr(0, "wev null");
return NGX_ERROR;
}

wev->handler = ngx_http_hello_world_delay_handler;
wev->data = r;
wev->log = r->connection->log;

ngx_add_timer(wev, 10000);

r->headers_out.content_type.len = sizeof("text/plain") - 1;
r->headers_out.content_type.data = (u_char *) "text/plain";

b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

out.buf = b;
out.next = NULL;

b->pos = ngx_hello_world;
b->last = ngx_hello_world + sizeof(ngx_hello_world);
b->memory = 1;
b->flush = 1;
b->last_buf = 0;

r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = -1;

ngx_http_clear_content_length(r);
ngx_http_clear_accept_ranges(r);

ngx_http_send_header(r);

rc = ngx_http_output_filter(r, &out);

return rc; // never got reached due to b->last_buf=0 and chunked response
}

and the delay handler:

static void ngx_http_hello_world_delay_handler(ngx_event_t *ev)
{
ngx_buf_t *b;
ngx_chain_t out;
int rc;
ngx_connection_t *c;
ngx_http_request_t *r;

r = ev->data;
c = r->connection;

if (c->destroyed) {
return;
}

out.buf = b;
out.next = NULL;
b->pos = ngx_hello_world;
b->last = ngx_hello_world + sizeof(ngx_hello_world);
b->memory = 1;
b->flush = 1;
b->last_buf = 0;

rc = ngx_http_output_filter(r, &out);

return;
}

Do you think I am missing something? I still getting segfaults just as
soon as my delay handler is called.

Regards,
--
Rogério Schneider
http://stockrt.github.com

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

Writing a timer event (how to execute an event from time to time)

Rogério Schneider 4170 May 25, 2010 01:18AM

Re: Writing a timer event (how to execute an event from time to time)

Weibin Yao 1336 May 25, 2010 01:34AM

Re: Writing a timer event (how to execute an event from time to time)

Rogério Schneider 1287 May 25, 2010 07:36AM

Re: Writing a timer event (how to execute an event from time to time)

Maxim Dounin 1273 May 25, 2010 09:14AM

Re: Writing a timer event (how to execute an event from time to time)

Rogério Schneider 1130 May 25, 2010 10:06AM

Re: Writing a timer event (how to execute an event from time to time)

Maxim Dounin 1656 May 25, 2010 11:12AM

Re: Writing a timer event (how to execute an event from time to time)

Piotr Sikora 1115 May 25, 2010 05:14AM

Re: Writing a timer event (how to execute an event from time to time)

Rogério Schneider 1152 May 25, 2010 07:42AM

Re: Writing a timer event (how to execute an event from time to time)

Peter Leonov 1223 May 25, 2010 07:08AM

Re: Writing a timer event (how to execute an event from time to time)

Rogério Schneider 1163 May 25, 2010 07:48AM

Re: Writing a timer event (how to execute an event from time to time)

Peter Leonov 1080 May 25, 2010 08:56AM

Re: Writing a timer event (how to execute an event from time to time)

Rogério Schneider 1156 May 25, 2010 10:08AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 233
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready