Basic process manager
This commit is contained in:
@ -32,7 +32,7 @@ func newLogger(logPath string) {
|
||||
|
||||
f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm)
|
||||
if err != nil {
|
||||
qlog.Fatal(err)
|
||||
qlog.Fatal("Fail to open log file(%s): %v", logPath, err)
|
||||
}
|
||||
|
||||
qlog.SetOutput(f)
|
||||
@ -185,8 +185,8 @@ func runServ(k *cli.Context) {
|
||||
gitcmd.Stdout = os.Stdout
|
||||
gitcmd.Stdin = os.Stdin
|
||||
gitcmd.Stderr = os.Stderr
|
||||
|
||||
if err = gitcmd.Run(); err != nil {
|
||||
err = gitcmd.Run()
|
||||
if err != nil {
|
||||
println("Gogs: internal error:", err)
|
||||
qlog.Fatalf("Fail to execute git command: %v", err)
|
||||
}
|
||||
|
2
gogs.go
2
gogs.go
@ -17,7 +17,7 @@ import (
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
)
|
||||
|
||||
const APP_VER = "0.4.4.0613 Alpha"
|
||||
const APP_VER = "0.4.4.0619 Alpha"
|
||||
|
||||
func init() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
|
@ -1,6 +0,0 @@
|
||||
package models
|
||||
|
||||
func Fix() error {
|
||||
_, err := orm.Exec("alter table repository drop column num_releases")
|
||||
return err
|
||||
}
|
@ -6,6 +6,7 @@ package models
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
@ -15,6 +16,7 @@ import (
|
||||
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
"github.com/gogits/gogs/modules/process"
|
||||
)
|
||||
|
||||
// Diff line types.
|
||||
@ -67,7 +69,7 @@ func (diff *Diff) NumFiles() int {
|
||||
|
||||
const DIFF_HEAD = "diff --git "
|
||||
|
||||
func ParsePatch(cmd *exec.Cmd, reader io.Reader) (*Diff, error) {
|
||||
func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
var (
|
||||
curFile *DiffFile
|
||||
@ -169,11 +171,8 @@ func ParsePatch(cmd *exec.Cmd, reader io.Reader) (*Diff, error) {
|
||||
}
|
||||
|
||||
// In case process became zombie.
|
||||
if !cmd.ProcessState.Exited() {
|
||||
log.Debug("git_diff.ParsePatch: process doesn't exit and now will be killed")
|
||||
if err := cmd.Process.Kill(); err != nil {
|
||||
log.Error("git_diff.ParsePatch: fail to kill zombie process: %v", err)
|
||||
}
|
||||
if err := process.Kill(pid); err != nil {
|
||||
log.Error("git_diff.ParsePatch(Kill): %v", err)
|
||||
}
|
||||
return diff, nil
|
||||
}
|
||||
@ -207,5 +206,5 @@ func GetDiff(repoPath, commitid string) (*Diff, error) {
|
||||
wr.Close()
|
||||
}()
|
||||
defer rd.Close()
|
||||
return ParsePatch(cmd, rd)
|
||||
return ParsePatch(process.Add(fmt.Sprintf("GetDiff(%s)", repoPath), cmd), cmd, rd)
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
qlog "github.com/qiniu/log"
|
||||
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
"github.com/gogits/gogs/modules/process"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -121,7 +122,7 @@ func AddPublicKey(key *PublicKey) (err error) {
|
||||
if err = ioutil.WriteFile(tmpPath, []byte(key.Content), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
stdout, stderr, err := com.ExecCmd("ssh-keygen", "-l", "-f", tmpPath)
|
||||
stdout, stderr, err := process.Exec("AddPublicKey", "ssh-keygen", "-l", "-f", tmpPath)
|
||||
if err != nil {
|
||||
return errors.New("ssh-keygen -l -f: " + stderr)
|
||||
} else if len(stdout) < 2 {
|
||||
|
342
models/repo.go
342
models/repo.go
File diff suppressed because it is too large
Load Diff
89
modules/process/manager.go
Normal file
89
modules/process/manager.go
Normal file
@ -0,0 +1,89 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package process
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
)
|
||||
|
||||
// Process represents a working process inherit from Gogs.
|
||||
type Process struct {
|
||||
Pid int64 // Process ID, not system one.
|
||||
Description string
|
||||
Start time.Time
|
||||
Cmd *exec.Cmd
|
||||
}
|
||||
|
||||
// List of existing processes.
|
||||
var (
|
||||
curPid int64 = 1
|
||||
Processes []*Process
|
||||
)
|
||||
|
||||
// Add adds a existing process and returns its PID.
|
||||
func Add(desc string, cmd *exec.Cmd) int64 {
|
||||
pid := curPid
|
||||
Processes = append(Processes, &Process{
|
||||
Pid: pid,
|
||||
Description: desc,
|
||||
Start: time.Now(),
|
||||
Cmd: cmd,
|
||||
})
|
||||
curPid++
|
||||
return pid
|
||||
}
|
||||
|
||||
func ExecDir(dir, desc, cmdName string, args ...string) (string, string, error) {
|
||||
bufOut := new(bytes.Buffer)
|
||||
bufErr := new(bytes.Buffer)
|
||||
|
||||
cmd := exec.Command(cmdName, args...)
|
||||
cmd.Dir = dir
|
||||
cmd.Stdout = bufOut
|
||||
cmd.Stderr = bufErr
|
||||
|
||||
pid := Add(desc, cmd)
|
||||
err := cmd.Run()
|
||||
if errKill := Kill(pid); errKill != nil {
|
||||
log.Error("Exec: %v", pid, desc, errKill)
|
||||
}
|
||||
return bufOut.String(), bufErr.String(), err
|
||||
}
|
||||
|
||||
// Exec starts executing a command and record its process.
|
||||
func Exec(desc, cmdName string, args ...string) (string, string, error) {
|
||||
return ExecDir("", desc, cmdName, args...)
|
||||
}
|
||||
|
||||
// Remove removes a process from list.
|
||||
func Remove(pid int64) {
|
||||
for i, proc := range Processes {
|
||||
if proc.Pid == pid {
|
||||
Processes = append(Processes[:i], Processes[i+1:]...)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Kill kills and removes a process from list.
|
||||
func Kill(pid int64) error {
|
||||
for i, proc := range Processes {
|
||||
if proc.Pid == pid {
|
||||
if proc.Cmd.Process != nil && proc.Cmd.ProcessState != nil && !proc.Cmd.ProcessState.Exited() {
|
||||
if err := proc.Cmd.Process.Kill(); err != nil {
|
||||
return fmt.Errorf("fail to kill process(%d/%s): %v", proc.Pid, proc.Description, err)
|
||||
}
|
||||
}
|
||||
Processes = append(Processes[:i], Processes[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -16,6 +16,7 @@ import (
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/cron"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
"github.com/gogits/gogs/modules/process"
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
)
|
||||
|
||||
@ -238,10 +239,12 @@ func Monitor(ctx *middleware.Context) {
|
||||
switch tab {
|
||||
case "process":
|
||||
ctx.Data["PageIsMonitorProcess"] = true
|
||||
ctx.Data["Processes"] = process.Processes
|
||||
ctx.HTML(200, "admin/monitor/process")
|
||||
default:
|
||||
ctx.Data["PageIsMonitorCron"] = true
|
||||
ctx.Data["Entries"] = cron.ListEntries()
|
||||
ctx.HTML(200, "admin/monitor/cron")
|
||||
}
|
||||
|
||||
ctx.HTML(200, "admin/monitor/cron")
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
0.4.4.0613 Alpha
|
||||
0.4.4.0619 Alpha
|
38
templates/admin/monitor/process.tmpl
Normal file
38
templates/admin/monitor/process.tmpl
Normal file
@ -0,0 +1,38 @@
|
||||
{{template "base/head" .}}
|
||||
{{template "base/navbar" .}}
|
||||
<div id="body" class="container" data-page="admin">
|
||||
{{template "admin/nav" .}}
|
||||
<div id="admin-container" class="col-md-10">
|
||||
<ul class="nav nav-tabs">
|
||||
<li{{if .PageIsMonitorCron}} class="active"{{end}}><a href="/admin/monitor">Cron Tasks</a></li>
|
||||
<li{{if .PageIsMonitorProcess}} class="active"{{end}}><a href="/admin/monitor?tab=process">Processes</a></li>
|
||||
</ul>
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
{{if .PageIsMonitorProcess}}
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Pid</th>
|
||||
<th>Description</th>
|
||||
<th>Start Time</th>
|
||||
<th>Execution Time</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .Processes}}
|
||||
<tr>
|
||||
<td>{{.Pid}}</td>
|
||||
<td>{{.Description}}</td>
|
||||
<td>{{.Start}}</td>
|
||||
<td>{{TimeSince .Start}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{template "base/footer" .}}
|
Reference in New Issue
Block a user