Skip to content

Commit

Permalink
execute thumb extractor action in an different process
Browse files Browse the repository at this point in the history
  • Loading branch information
wandenberg committed Nov 11, 2015
1 parent 9cf6156 commit 31fd119
Show file tree
Hide file tree
Showing 5 changed files with 711 additions and 46 deletions.
22 changes: 22 additions & 0 deletions include/ngx_http_video_thumbextractor_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,30 @@ typedef struct {
ngx_str_t filename;
} ngx_http_video_thumbextractor_thumb_ctx_t;

typedef enum {
NGX_HTTP_VIDEO_THUMBEXTRACTOR_TRANSFER_RC = 1,
NGX_HTTP_VIDEO_THUMBEXTRACTOR_TRANSFER_IMAGE_LEN,
NGX_HTTP_VIDEO_THUMBEXTRACTOR_TRANSFER_IMAGE_DATA,
NGX_HTTP_VIDEO_THUMBEXTRACTOR_TRANSFER_FINISHED
} ngx_http_video_thumbextractor_transfer_step;

typedef struct {
ngx_http_video_thumbextractor_transfer_step step;
ngx_buf_t buffer;
ngx_http_video_thumbextractor_thumb_ctx_t thumb_ctx;
caddr_t data;
size_t size;
ngx_int_t rc;
ngx_pool_t *pool;
ngx_connection_t *conn;
} ngx_http_video_thumbextractor_transfer_t;

typedef struct {
ngx_queue_t queue;
ngx_int_t slot;
ngx_http_request_t *request;
ngx_http_video_thumbextractor_thumb_ctx_t thumb_ctx;
ngx_http_video_thumbextractor_transfer_t transfer;
} ngx_http_video_thumbextractor_ctx_t;

ngx_int_t ngx_http_video_thumbextractor_access_handler(ngx_http_request_t *r);
Expand Down
47 changes: 47 additions & 0 deletions include/ngx_http_video_thumbextractor_module_ipc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2011 Wandenberg Peixoto <wandenberg@gmail.com>
*
* This file is part of Nginx Video Thumb Extractor Module.
*
* Nginx Video Thumb Extractor Module is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Nginx Video Thumb Extractor Module is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Nginx Video Thumb Extractor Module. If not, see <http://www.gnu.org/licenses/>.
*
*
* ngx_http_video_thumbextractor_module_utils.h
*
* Created: Nov 22, 2011
* Author: Wandenberg Peixoto <wandenberg@gmail.com>
*
*/
#ifndef NGX_HTTP_VIDEO_THUMBEXTRACTOR_MODULE_IPC_H_
#define NGX_HTTP_VIDEO_THUMBEXTRACTOR_MODULE_IPC_H_

#include <ngx_http_video_thumbextractor_module.h>

typedef struct {
ngx_socket_t pipefd[2];
ngx_connection_t *conn;
ngx_pid_t pid;
ngx_flag_t processing;
ngx_http_request_t *request;
ngx_int_t slot;
} ngx_http_video_thumbextractor_ipc_t;


ngx_queue_t *ngx_http_video_thumbextractor_module_extract_queue;

ngx_http_video_thumbextractor_ipc_t ngx_http_video_thumbextractor_module_ipc_ctxs[NGX_MAX_PROCESSES];

void ngx_http_video_thumbextractor_module_ensure_extractor_process(void);

#endif /* NGX_HTTP_VIDEO_THUMBEXTRACTOR_MODULE_IPC_H_ */
68 changes: 23 additions & 45 deletions src/ngx_http_video_thumbextractor_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <ngx_http_video_thumbextractor_module.h>
#include <ngx_http_video_thumbextractor_module_setup.c>
#include <ngx_http_video_thumbextractor_module_utils.c>
#include <ngx_http_video_thumbextractor_module_ipc.c>

ngx_http_output_header_filter_pt ngx_http_video_thumbextractor_next_header_filter;
ngx_http_output_body_filter_pt ngx_http_video_thumbextractor_next_body_filter;
Expand Down Expand Up @@ -108,15 +109,8 @@ ngx_http_video_thumbextractor_body_filter(ngx_http_request_t *r, ngx_chain_t *in
ngx_int_t
ngx_http_video_thumbextractor_extract_and_send_thumb(ngx_http_request_t *r)
{
ngx_http_video_thumbextractor_loc_conf_t *vtlcf;
ngx_http_video_thumbextractor_ctx_t *ctx;
caddr_t out_buffer = 0;
size_t out_len = 0;
ngx_buf_t *b;
ngx_int_t rc;
ngx_chain_t *out;
ngx_http_video_thumbextractor_ctx_t *ctx;

vtlcf = ngx_http_get_module_loc_conf(r, ngx_http_video_thumbextractor_module);
ctx = ngx_http_get_module_ctx(r, ngx_http_video_thumbextractor_module);

#if (NGX_HTTP_CACHE)
Expand All @@ -138,45 +132,13 @@ ngx_http_video_thumbextractor_extract_and_send_thumb(ngx_http_request_t *r)
}
#endif

rc = ngx_http_video_thumbextractor_get_thumb(vtlcf, &ctx->thumb_ctx, &out_buffer, &out_len, r->pool, r->connection->log);
r->main->count++;

if (rc == NGX_ERROR) {
return ngx_http_filter_finalize_request(r, &ngx_http_video_thumbextractor_module, NGX_HTTP_INTERNAL_SERVER_ERROR);
}
ngx_queue_insert_tail(ngx_http_video_thumbextractor_module_extract_queue, &ctx->queue);

if ((rc == NGX_HTTP_VIDEO_THUMBEXTRACTOR_FILE_NOT_FOUND) || (rc == NGX_HTTP_VIDEO_THUMBEXTRACTOR_SECOND_NOT_FOUND)) {
return ngx_http_filter_finalize_request(r, &ngx_http_video_thumbextractor_module, NGX_HTTP_NOT_FOUND);
}

/* write response */
r->headers_out.content_type = NGX_HTTP_VIDEO_THUMBEXTRACTOR_CONTENT_TYPE;
r->headers_out.content_type_len = NGX_HTTP_VIDEO_THUMBEXTRACTOR_CONTENT_TYPE.len;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = out_len;
ngx_http_video_thumbextractor_module_ensure_extractor_process();

out = (ngx_chain_t *) ngx_pcalloc(r->pool, sizeof(ngx_chain_t));
b = ngx_calloc_buf(r->pool);
if ((out == NULL) || (b == NULL)) {
return NGX_ERROR;
}

b->last_buf = 1;
b->last_in_chain = 1;
b->flush = 1;
b->memory = 1;
b->pos = (u_char *) out_buffer;
b->start = b->pos;
b->end = b->pos + out_len;
b->last = b->end;

out->buf = b;
out->next = NULL;

rc = ngx_http_video_thumbextractor_next_header_filter(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_video_thumbextractor_next_body_filter(r, out);
return NGX_DONE;
}


Expand All @@ -201,7 +163,7 @@ ngx_http_video_thumbextractor_set_request_context(ngx_http_request_t *r)
}

if ((cln = ngx_pool_cleanup_add(r->pool, 0)) == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "video thumb extractor module: unable to allocate memory for cleanup");
ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0, "video thumb extractor module: unable to allocate memory for cleanup");
return NGX_ERROR;
}

Expand All @@ -215,6 +177,10 @@ ngx_http_video_thumbextractor_set_request_context(ngx_http_request_t *r)

ngx_http_set_ctx(r, ctx, ngx_http_video_thumbextractor_module);

ctx->slot = -1;
ctx->request = r;
ngx_queue_init(&ctx->queue);

thumb_ctx = &ctx->thumb_ctx;
thumb_ctx->file_info.offset = 0;

Expand Down Expand Up @@ -281,5 +247,17 @@ ngx_http_video_thumbextractor_cleanup_request_context(ngx_http_request_t *r)
r->read_event_handler = ngx_http_request_empty_handler;

if (ctx != NULL) {
if (ctx->slot >= 0) {
ngx_http_video_thumbextractor_module_ipc_ctxs[ctx->slot].request = NULL;
}

if (!ngx_queue_empty(&ctx->queue)) {
ngx_queue_remove(&ctx->queue);
ngx_queue_init(&ctx->queue);
}

ngx_http_set_ctx(r, NULL, ngx_http_video_thumbextractor_module);
}

ngx_http_video_thumbextractor_module_ensure_extractor_process();
}
Loading

0 comments on commit 31fd119

Please sign in to comment.