Skip to content

The init and exit cycle hooks in application layer

Taymindis Woon edited this page Dec 24, 2018 · 7 revisions

There are 2 functions must have built into your application layer, there extending the function of worker cycle init and exit of your applications.

void ngx_link_func_init_cycle(ngx_link_func_cycle_t* cyc);
void ngx_link_func_exit_cycle(ngx_link_func_cycle_t* cyc);

ngx_link_func_init_cycle and ngx_link_func_exit_cycle will be triggered N times of your application. The N is the number of your worker process defined in your nginx.

This is very important when you are workaround with link function shared memory, link function shared memory is reusable no matter how many time you reload the nginx. Once the nginx stopped, the link function shared memory will be freed.

The Benefit of cycle hooking, you can prepare your config properties setup and etc, if you have nothing to do in this function, you may just create a empty block function.

Example when you need to init share memory variable to share multiple worker/apps

void
ngx_link_func_init_cycle(ngx_link_func_cycle_t* cycle) {
    ngx_link_func_cyc_log(info, cycle, "%s", "starting:: The stock transaction application");
    unsigned int *stockUpdateVer;
    void *shm = cycle->shared_mem;
    ngx_link_func_shmtx_lock(shm);

    if( (stockUpdateVer = ngx_link_func_cache_get(shm, "stockVer")) == NULL ) {
        stockUpdateVer = ngx_link_func_cache_new(shm, "stockVer", sizeof(unsigned int));
        *stockUpdateVer = 1;
    }
    ngx_link_func_cyc_log(info, cycle, "starting:: stock update is %u\n", *stockUpdateVer);

    ngx_link_func_shmtx_unlock(shm);
}



void
ngx_link_func_exit_cycle(ngx_link_func_cycle_t* cyc) {
    shm = cyc->shared_mem;
    ngx_link_func_shmtx_lock(shm);
    
/* TODO do whatever you want if nginx reload or stop */
    if( shouldResetStockVersion ) {
       ngx_link_func_cache_remove(shm, "stockVer");
    }
    ngx_link_func_shmtx_unlock(shm);
    
    ngx_link_func_cyc_log(info, cyc, "%s\n", "Shutting or reloading stock transaction application");
}