Last active
March 5, 2022 08:53
-
-
Save mamemomonga/ceeb834bd64ca1fc6b4c89d537c6b29d to your computer and use it in GitHub Desktop.
docker-compose up -d でMySQL(5.7.37) db の初期化を待つ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# ------------------------------------------ | |
# docker-compose up -d でMySQL(5.7.37) db の初期化を待つ | |
# Goで作り直しました https://github.com/mamemomonga/mysql-docker-wait-initalize | |
# ------------------------------------------ | |
use strict; | |
use warnings; | |
use feature 'say'; | |
use Symbol 'gensym'; | |
use IPC::Open3; | |
use IO::Select; | |
my $cid=`docker-compose ps -q db`; | |
chomp $cid; | |
say "ContainerID: $cid"; | |
my $pid=open3(undef,my $fhout = gensym() ,my $fherr = gensym(), | |
'docker','logs','-f',$cid) || die $!; | |
my $reader=IO::Select->new(); | |
$reader->add($fhout); | |
$reader->add($fherr); | |
my $flag=0; | |
my $line=""; | |
while(my @ready=$reader->can_read()) { | |
foreach my $fh (@ready) { | |
sysread($fh, my $char,1); | |
if($char ne "\n") { | |
$line.=$char; | |
next; | |
} | |
say "[初期化中] $line"; | |
if($line=~m#\Qready for connections.\E#s ){ | |
if($flag) { | |
kill 2,$pid; | |
exit(0); | |
} | |
$flag=1; | |
} | |
if($line=~m#\Q[ERROR]\E#s ){ | |
kill 2,$pid; | |
print "MySQL起動に失敗しました\n"; | |
exit(1); | |
} | |
$line=""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment