From 56d6793d5fd30293c82374abe764342532e3ef4a Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Fri, 9 Jul 2021 05:30:52 +0900 Subject: [PATCH 1/4] Use Filesys::Notify::Simple instead of -R option. --- mt/mt-watcher.pl | 24 ++++++++++++++++++++++++ mt/mt-watcher/Dockerfile | 18 ++++++++++++++++++ mt/plackup-mt | 18 ++++-------------- mt/psgi.yml | 13 ++++++++++++- 4 files changed, 58 insertions(+), 15 deletions(-) create mode 100755 mt/mt-watcher.pl create mode 100644 mt/mt-watcher/Dockerfile diff --git a/mt/mt-watcher.pl b/mt/mt-watcher.pl new file mode 100755 index 0000000..3cb1839 --- /dev/null +++ b/mt/mt-watcher.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Filesys::Notify::Simple; + +# wait for `make me` to complete +sleep(5); + +my @files = ( qw(addons extlib lib plugins), glob('*.cgi') ); +my $watcher = Filesys::Notify::Simple->new( \@files ); +while (1) { + $watcher->wait( + sub { + my @events = @_; + print( join( "\n", map { $_->{path} } @events ) . "\n" ); + system('docker kill -s HUP mt_mt_1'); + + # throttling + sleep(1); + } + ); +} diff --git a/mt/mt-watcher/Dockerfile b/mt/mt-watcher/Dockerfile new file mode 100644 index 0000000..455ef24 --- /dev/null +++ b/mt/mt-watcher/Dockerfile @@ -0,0 +1,18 @@ +FROM perl:5.34 + +ENV DOCKER_VERSION=20.10.7 + +## docker +RUN set -ex \ + \ + && mkdir /tmp/docker \ + && cd /tmp/docker \ + && curl -sL https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKER_VERSION}.tgz -o docker.tgz \ + && tar zxf docker.tgz \ + && mv docker/docker /usr/bin/docker \ + && rm -fr /tmp/docker + +## cpan libraries +RUN set -ex \ + \ + && cpanm Filesys::Notify::Simple Linux::Inotify2 diff --git a/mt/plackup-mt b/mt/plackup-mt index 0bc5f09..ee4e50c 100755 --- a/mt/plackup-mt +++ b/mt/plackup-mt @@ -1,7 +1,5 @@ #!/bin/bash -reload_options="`ls *.cgi | sed -e 's/^/-R /'` `ls mt-static | grep -v support | sed -e 's/^/-R mt-static\//'`" - exec starman \ -MCGI \ -MFile::Spec \ @@ -12,16 +10,8 @@ exec starman \ -MDBI \ -MDBD::mysql \ -MImage::Magick \ - -R addons \ - -R alt-tmpl \ - -R default_templates \ - -R extlib \ - -R lib \ - -R php \ - -R plugins \ - -R search_templates \ - -R themes \ - -R tmpl \ - -R tools \ - $reload_options \ + -E production \ + --port=80 \ + --workers=2 \ + --pid=$MT_PID_FILE_PATH \ "$@" diff --git a/mt/psgi.yml b/mt/psgi.yml index d2b4904..dbc77c9 100644 --- a/mt/psgi.yml +++ b/mt/psgi.yml @@ -2,6 +2,17 @@ version: "3" services: mt: restart: always - command: "${PLACKUP:-/usr/local/lib/mt/bin/plackup-mt -E production -L Shotgun --port=80 --workers=2 --pid=/tmp/mt.psgi.pid} /var/www/cgi-bin/mt/mt.psgi" + command: "${PLACKUP:-/usr/local/lib/mt/bin/plackup-mt} /var/www/cgi-bin/mt/mt.psgi" + environment: + MT_PID_FILE_PATH: ${MT_PID_FILE_PATH:-/tmp/mt.psgi.pid} volumes: - "./plackup-mt:/usr/local/lib/mt/bin/plackup-mt" + mt-watcher: + build: + context: mt-watcher + working_dir: /var/www/cgi-bin/mt + command: /usr/local/lib/mt/bin/mt-watcher.pl + volumes: + - "${MT_HOME_PATH:-../../movabletype}:/var/www/cgi-bin/mt" + - "./mt-watcher.pl:/usr/local/lib/mt/bin/mt-watcher.pl" + - /var/run/docker.sock:/var/run/docker.sock From b8bbb754a7e9c3998f61f4b18dc5c25fc4c1c6cf Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Fri, 9 Jul 2021 08:28:31 +0900 Subject: [PATCH 2/4] Introduce DISABLE_MT_WATCHER environment variable. --- mt/mt-watcher.pl | 5 +++++ mt/psgi.yml | 2 ++ 2 files changed, 7 insertions(+) diff --git a/mt/mt-watcher.pl b/mt/mt-watcher.pl index 3cb1839..865009d 100755 --- a/mt/mt-watcher.pl +++ b/mt/mt-watcher.pl @@ -5,6 +5,11 @@ use Filesys::Notify::Simple; +if ( $ENV{DISABLE_MT_WATCHER} ) { + warn('mt-watcher container is disbled.'); + exit; +} + # wait for `make me` to complete sleep(5); diff --git a/mt/psgi.yml b/mt/psgi.yml index dbc77c9..9b00f4b 100644 --- a/mt/psgi.yml +++ b/mt/psgi.yml @@ -12,6 +12,8 @@ services: context: mt-watcher working_dir: /var/www/cgi-bin/mt command: /usr/local/lib/mt/bin/mt-watcher.pl + environment: + DISABLE_MT_WATCHER: ${DISABLE_MT_WATCHER:-0} volumes: - "${MT_HOME_PATH:-../../movabletype}:/var/www/cgi-bin/mt" - "./mt-watcher.pl:/usr/local/lib/mt/bin/mt-watcher.pl" From 26210c6e902b2154bb0589651863b119a9ebda0e Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Fri, 9 Jul 2021 13:54:32 +0900 Subject: [PATCH 3/4] Exclude files other than "*.cgi" at the top level --- mt/mt-watcher.pl | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mt/mt-watcher.pl b/mt/mt-watcher.pl index 865009d..7b63f24 100755 --- a/mt/mt-watcher.pl +++ b/mt/mt-watcher.pl @@ -3,6 +3,7 @@ use strict; use warnings; +use Cwd qw(getcwd); use Filesys::Notify::Simple; if ( $ENV{DISABLE_MT_WATCHER} ) { @@ -13,13 +14,23 @@ # wait for `make me` to complete sleep(5); -my @files = ( qw(addons extlib lib plugins), glob('*.cgi') ); +my $mt_home = $ENV{MT_HOME} || getcwd(); +$mt_home =~ s{/+$}{}; # remove trailing slash + +my @files = ( map( {"$mt_home/$_"} qw(addons extlib lib plugins) ), glob("$mt_home/*.cgi") ); my $watcher = Filesys::Notify::Simple->new( \@files ); while (1) { $watcher->wait( sub { - my @events = @_; - print( join( "\n", map { $_->{path} } @events ) . "\n" ); + my @paths = grep { + + # exclude files other than "*.cgi" at the top level + $_ =~ m{$mt_home/(?:[^/]+\.cgi|.*?/)}; + } map { $_->{path} } @_; + + return unless @paths; + + print( join( "\n", @paths ) . "\n" ); system('docker kill -s HUP mt_mt_1'); # throttling From aa349a9f0dccce3ce5e094a1f47ca17a3bf87de5 Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Fri, 9 Jul 2021 15:17:47 +0900 Subject: [PATCH 4/4] Enable to specify PERL_FNS_NO_OPT. --- Makefile | 3 +++ mt/psgi.yml | 1 + 2 files changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 9cc2ff2..03ccce2 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,9 @@ export HTTPD_EXPOSE_PORT export PLACKUP export CMD +# mt-watcher container +export PERL_FNS_NO_OPT + # override variables ENV_FILE=.env diff --git a/mt/psgi.yml b/mt/psgi.yml index 9b00f4b..a538afa 100644 --- a/mt/psgi.yml +++ b/mt/psgi.yml @@ -13,6 +13,7 @@ services: working_dir: /var/www/cgi-bin/mt command: /usr/local/lib/mt/bin/mt-watcher.pl environment: + PERL_FNS_NO_OPT: ${PERL_FNS_NO_OPT:-0} DISABLE_MT_WATCHER: ${DISABLE_MT_WATCHER:-0} volumes: - "${MT_HOME_PATH:-../../movabletype}:/var/www/cgi-bin/mt"