implement graceful shutdown to webhook mode
ensures all pending tasks are finished before quit.
This commit is contained in:
parent
a10a04e1ce
commit
6bae5d11c2
3 changed files with 172 additions and 133 deletions
159
lib/git.go
Normal file
159
lib/git.go
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package lib
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type repoSpec struct {
|
||||
user string
|
||||
name string
|
||||
}
|
||||
|
||||
type GitRunner struct {
|
||||
ch <-chan repoSpec
|
||||
cfg *WebhookCFG
|
||||
}
|
||||
|
||||
func (g *GitRunner) Run(ctx context.Context) error {
|
||||
// graceful: exit only if all tasks are done
|
||||
for {
|
||||
select {
|
||||
case repo := <-g.ch:
|
||||
err := g.cfg.download(repo.user, repo.name)
|
||||
if err != nil {
|
||||
fmt.Println("Failed to download repo: ", err)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *WebhookCFG) pageDir(user, repo string) string {
|
||||
return filepath.Join(c.PageDir, user, repo)
|
||||
}
|
||||
|
||||
func (c *WebhookCFG) gitDir(user, repo string) string {
|
||||
return filepath.Join(c.GitDir, user, repo)
|
||||
}
|
||||
|
||||
func (c *WebhookCFG) checkout(user, repo string) (err error) {
|
||||
pageDir := c.pageDir(user, repo)
|
||||
gitDir := c.gitDir(user, repo)
|
||||
|
||||
git := exec.Command(
|
||||
"git",
|
||||
"--git-dir", gitDir,
|
||||
"--work-tree", pageDir,
|
||||
"checkout", "origin/"+c.Branch,
|
||||
)
|
||||
output, err := git.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Println("git checkout failed: ", err)
|
||||
fmt.Println("=========== Dump output: ============")
|
||||
fmt.Println(string(output))
|
||||
fmt.Println("=====================================")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *WebhookCFG) fetch(user, repo string) (err error) {
|
||||
gitDir := c.gitDir(user, repo)
|
||||
git := exec.Command(
|
||||
"git",
|
||||
"--git-dir", gitDir,
|
||||
"fetch", "origin", c.Branch,
|
||||
)
|
||||
output, err := git.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Println("git fetch failed: ", err)
|
||||
fmt.Println("=========== Dump output: ============")
|
||||
fmt.Println(string(output))
|
||||
fmt.Println("=====================================")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *WebhookCFG) clone(user, repo string) (err error) {
|
||||
pageDir := c.pageDir(user, repo)
|
||||
err = os.MkdirAll(filepath.Dir(pageDir), 0755)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
gitDir := c.gitDir(user, repo)
|
||||
err = os.MkdirAll(filepath.Dir(gitDir), 0700)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uri := c.Server
|
||||
uri.Path = "/" + path.Join(user, repo) + ".git"
|
||||
uri.User = url.UserPassword(c.GitUser, c.GitPass)
|
||||
|
||||
git := exec.Command(
|
||||
"git",
|
||||
"clone",
|
||||
"-b", c.Branch,
|
||||
"--single-branch",
|
||||
"--no-tags",
|
||||
"--separate-git-dir", gitDir,
|
||||
uri.String(),
|
||||
pageDir,
|
||||
)
|
||||
output, err := git.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Println("git clone failed: ", err)
|
||||
fmt.Println("=========== Dump output: ============")
|
||||
fmt.Println(string(output))
|
||||
fmt.Println("=====================================")
|
||||
}
|
||||
|
||||
os.Remove(filepath.Join(pageDir, ".git"))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var repoLock = &sync.Map{}
|
||||
|
||||
func (c *WebhookCFG) lock(user, repo string) func() {
|
||||
key := user + "/" + repo
|
||||
|
||||
lock, _ := repoLock.LoadOrStore(key, &sync.Mutex{})
|
||||
m := lock.(*sync.Mutex)
|
||||
m.Lock()
|
||||
return m.Unlock
|
||||
}
|
||||
|
||||
func (c *WebhookCFG) download(user, repo string) (err error) {
|
||||
unlock := c.lock(user, repo)
|
||||
defer unlock()
|
||||
fmt.Println("Pulling ", user, repo)
|
||||
|
||||
gitDir := c.gitDir(user, repo)
|
||||
if _, err = os.Stat(gitDir); os.IsNotExist(err) {
|
||||
err = c.clone(user, repo)
|
||||
} else {
|
||||
err = c.fetch(user, repo)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = c.checkout(user, repo)
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue