summaryrefslogtreecommitdiff
path: root/tests/events.wsgi
blob: 0f7e7780b131d46f7c65e959fcdf0f3255a139d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from __future__ import print_function

import mod_wsgi
import traceback
import time
import os
import threading
import atexit

try:
    mod_wsgi.request_data()
except RuntimeError:
    print('INACTIVE')

def wrapper(application):
    def _application(environ, start_response):
        print('WRAPPER', application)
        return application(environ, start_response)
    return _application

def event_handler(name, **kwargs):
    print('EVENT-HANDLER', name, kwargs, os.getpid(), mod_wsgi.application_group)
    if name == 'request_started':
        thread = threading.current_thread()
        request_data = kwargs['request_data']
        request_data['thread_name'] = thread.name
        request_data['thread_id'] = thread.ident
        return dict(application_object=wrapper(kwargs['application_object']))
    elif name == 'response_started':
        print('REQUESTS', mod_wsgi.active_requests)
    elif name == 'request_finished':
        print('PROCESS', mod_wsgi.process_metrics()) 
    elif name == 'request_exception':
        exception_info = kwargs['exception_info']
        traceback.print_exception(*exception_info)
    elif name == 'process_stopping':
        print('SHUTDOWN', mod_wsgi.active_requests)

print('EVENTS#ALL', mod_wsgi.event_callbacks)

mod_wsgi.subscribe_events(event_handler)

def shutdown_handler(event, **kwargs):
    print('SHUTDOWN-HANDLER', event, kwargs)

print('EVENTS#SHUTDOWN', mod_wsgi.event_callbacks)

mod_wsgi.subscribe_shutdown(shutdown_handler)

print('CALLBACKS', mod_wsgi.event_callbacks)

def atexit_handler():
    print('ATEXIT-HANDLER')

atexit.register(atexit_handler)

def do_sleep(duration):
    time.sleep(duration)

def application(environ, start_response):
    failure_mode = environ.get('HTTP_X_FAILURE_MODE', '')
    failure_mode = failure_mode.split()

    sleep_duration = environ.get('HTTP_X_SLEEP_DURATION', 0)
    sleep_duration = float(sleep_duration or 0)

    if 'application' in failure_mode:
        raise RuntimeError('application')

    status = '200 OK'
    output = b'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    environ['wsgi.input'].read()

    if sleep_duration:
        do_sleep(sleep_duration)

    try:
        yield output

        if 'yield' in failure_mode:
            raise RuntimeError('yield')
    finally:
        if 'close' in failure_mode:
            raise RuntimeError('close')