# 概要

- pub/sub を用いて Config の値のアップデート時に他の App にキャッシュの更新を促す
    - Nchan, Redis の2種をサポートする
    - v4.1.0 向けにはとりあえず Nchan のみ実装を行う

# Nchan

https://nchan.io/

## 概要

- nginx のサードパーティーモジュール
- GROWI は WebSocket で subscribe、HTTP POST で publish する方針

## Nchan 専用 nginx イメージのビルド

### Why?
- [nginx 公式 docker image](https://hub.docker.com/_/nginx) は debian ベースだが、debian 公式パッケージを使わず、nginx 独自の apt repos のバイナリをインストールしている
- そのため、Nchan module は自前ビルドする必要がある

### コンパイル参考
- https://www.nginx.com/blog/compiling-dynamic-modules-nginx-plus/
- https://gist.github.com/hermanbanken/96f0ff298c162a522ddbba44cad31081

### Dockerfile の種

`NGINX_VERSION` と `NCHAN_VERSION` は ENV で用意する

#### FROM debian:buster-slim

```bash
apt update
apt install --no-install-recommends --no-install-suggests -y gnupg1 ca-certificates wget
apt install -y dpkg-dev libpcre3-dev zlib1g-dev

NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \
KEY_SERVER=hkp://keyserver.ubuntu.com:80; \
apt-key adv --keyserver "$KEY_SERVER" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY"

echo "deb-src https://nginx.org/packages/mainline/debian/ buster nginx" >> /etc/apt/sources.list.d/nginx.list

apt update

cd
apt source nginx=1.19.1-1~buster

wget "https://github.com/slact/nchan/archive/v1.2.7.tar.gz" -O nchan.tar.gz
tar zxf nchan.tar.gz

export NCHANDIR="$(pwd)/nchan-${NCHAN_VERSION}"

cd nginx-$NGINX_VERSION
./configure --with-compat --add-dynamic-module=$NCHANDIR
# make modules
make -f objs/Makefile objs/ngx_nchan_module.so
```

#### FROM nginx:1.19.1
```bash
mkdir -p /usr/local/nginx/modules
cp /root/nchan-1.2.7/objs/ngx_nchan_module.so /usr/local/nginx/modules/

cd /etc/nginx
echo -e "# load Nchan module" > /tmp/nginx.conf
echo -e "load_module /usr/local/nginx/modules/ngx_nchan_module.so;" > /tmp/nginx.conf
cat nginx.conf >> /tmp/nginx.conf

mv nginx.conf nginx.conf.org
mv /tmp/nginx.conf nginx.conf
```

#### `/etc/nginx/conf.d/default.conf`

```nginx
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # Pub/sub
    location /pubsub {
        nchan_pubsub;
        nchan_channel_id common;
    }
    location ~ /pubsub/(\w+) {
        nchan_pubsub;
        nchan_channel_id "$1";
    }
}

```


# Redis

https://redis.io/topics/pubsub

(TBD)


---

## Page Navigation

- Canonical URL: https://dev.growi.org/資料/内部仕様/ConfigManager/マルチApp対応
- Permalink: https://dev.growi.org/5f0ee2c18c252f0048289de4
- Parent: [ConfigManager](/5cc9975f30890b003fe90935.md)
- Children: 0 total
- Total descendants: 0
- Siblings: 0 total
- Last updated: 2020-07-17T12:32:21.135Z by yuki
- Full page listing (all children regardless of count): https://dev.growi.org/_api/v3/page-listing/children?id=5f0ee2c18c252f0048289de4
