Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Golangで学ぶHot deployの仕組み

Golangで学ぶHot deployの仕組み

Server::Starterの仕組みを学び、Golangでミニマムで
 実装してみた

tsurubee

June 28, 2018
Tweet

More Decks by tsurubee

Other Decks in Programming

Transcript

  1. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ TUBSU@TFSWFS 1BSFOU start_server —-port 8888 —- ./server TFSWFS $IJME

    TPDLFU GPSLFYFD TPDLFUΛڞ༗ BDDFQU TUBSU@TFSWFSɿ(SBDFGVMSFTUBSUͷͨΊͷϓϩάϥϜ TFSWFSɿ࣮ߦ͍ͨ͠ϓϩάϥϜ
  2. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ TUBSU@TFSWFS 1BSFOU start_server —-port 8888 —- ./server TFSWFS $IJME

    TPDLFU GPSLFYFD TPDLFUΛڞ༗ BDDFQU TUBSU@TFSWFSɿ(SBDFGVMSFTUBSUͷͨΊͷϓϩάϥϜ TFSWFSɿ࣮ߦ͍ͨ͠ϓϩάϥϜ ᶃϓϩάϥϜʹมߋʂ
  3. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ kill -HUP <master pid> TPDLFU BDDFQU TUBSU@TFSWFS 1BSFOU TFSWFS

    $IJME TUBSU@TFSWFSɿ(SBDFGVMSFTUBSUͷͨΊͷϓϩάϥϜ TFSWFSɿ࣮ߦ͍ͨ͠ϓϩάϥϜ ᶄ4*()61 ᶃϓϩάϥϜʹมߋʂ
  4. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ kill -HUP <master pid> TPDLFU GPSLFYFD TFSWFS $IJME ᶄ4*()61

    BDDFQU BDDFQU TUBSU@TFSWFS 1BSFOU TFSWFS $IJME ᶅ৽ϓϩάϥϜͰىಈ ᶃϓϩάϥϜʹมߋʂ
  5. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ kill -HUP <master pid> TPDLFU ऴྃ͢Δલʹʜ ᶃ৽نϦΫΤεड෇ ɹͷఀࢭ ᶄݱࡏॲཧதͷ

    ϦΫΤετͷ׬ྃ GPSLFYFD (SBDFGVM4IVUEPXO BDDFQU BDDFQU TUBSU@TFSWFS 1BSFOU TFSWFS $IJME ᶅ৽ϓϩάϥϜͰىಈ ᶄ4*()61 ᶆ4*(5&3. TFSWFS $IJME ᶃϓϩάϥϜʹมߋʂ
  6. )PUEFQMPZͷ࢓૊Έʢ؆қ൛ʣ TUBSU@TFSWFS 1BSFOU kill -HUP <master pid> TPDLFU GPSLFYFD TFSWFS

    $IJME BDDFQU ᶆ4*(5&3. ᶅ৽ϓϩάϥϜͰىಈ ᶄ4*()61 TFSWFS $IJME ᶃϓϩάϥϜʹมߋʂ ऴྃ͢Δલʹʜ ᶃ৽نϦΫΤεड෇ ɹͷఀࢭ ᶄݱࡏॲཧதͷ ϦΫΤετͷ׬ྃ (SBDFGVM4IVUEPXO
  7. ࣮૷ϙΠϯτ̍ʙγάφϧϋϯυϥ func waitSignal(l net.Listener) { c := make(chan os.Signal, 1)

    signal.Notify(c, syscall.SIGTERM) go func() { sig := <-c switch sig { case syscall.SIGTERM: signal.Stop(c) l.Close() } }() } γάφϧͱ͸ϓϩηεؒ௨৴ͷҰछ ϓϩηεʹγάφϧΛૹ৴͢Δͱɺͦͷϓϩηεͷਖ਼ৗॲཧʹׂΓࠐΜͰɺ γάφϧݻ༗ͷॲཧΛ࣮ߦͰ͖Δ ͓खܰʹγάφϧͷϋϯυϦϯά͕ Ͱ͖Δ
  8. ࣮૷ϙΠϯτ̍ʙγάφϧϋϯυϥ func waitSignal(l net.Listener) { c := make(chan os.Signal, 1)

    signal.Notify(c, syscall.SIGTERM) go func() { sig := <-c switch sig { case syscall.SIGTERM: signal.Stop(c) l.Close() } }() } γάφϧͱ͸ϓϩηεؒ௨৴ͷҰछ ϓϩηεʹγάφϧΛૹ৴͢Δͱɺͦͷϓϩηεͷਖ਼ৗॲཧʹׂΓࠐΜͰɺ γάφϧݻ༗ͷॲཧΛ࣮ߦͰ͖Δ HPSPVUJOFͰγάφϧΛ଴ͪड͚Δ ͓खܰʹγάφϧͷϋϯυϦϯά͕ Ͱ͖Δ
  9. ࣮૷ϙΠϯτ̎ʙϑΝΠϧσΟεΫϦϓλ func forkExec(l *net.TCPListener) (*os.Process, error) { progName, _ :=

    exec.LookPath(os.Args[0]) pwd, _ := os.Getwd() f, _ := l.File() defer f.Close() files := []*os.File{os.Stdin, os.Stdout, os.Stderr, f} fdEnv := fmt.Sprintf("%s=%d", "FD_KEY", len(files)-1) return os.StartProcess(progName, os.Args, &os.ProcAttr{ Dir: pwd, Env: append(os.Environ(), fdEnv), Files: files, }) } ϑΝΠϧσΟεΫϓϦΫλ͸ɺϑΝΠϧ΍ιέοτͳͲΛந৅Խͨ͠࢓૊Έ ϓϩηεΛGPSLͨ͠ࡍ͸ɺ਌ϓϩηεͷϑΝΠϧσΟεΫϦϓλΛҾ͖ܧ͙