主程序移动到子线程运行,方便重启 #99

This commit is contained in:
johlanse 2022-10-03 11:05:57 +08:00
parent 673302ed85
commit 2e99776ed0
12 changed files with 80 additions and 43 deletions

1
go.mod
View File

@ -12,6 +12,7 @@ require (
github.com/gin-gonic/gin v1.8.1 github.com/gin-gonic/gin v1.8.1
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0-beta.0 github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0-beta.0
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/huoxue1/xdaemon v0.0.0-20221003021252-8ce95c07edb0
github.com/imroc/req/v3 v3.8.2 github.com/imroc/req/v3 v3.8.2
github.com/johlanse/wechat v0.0.0-20220909140933-49a96a4c0412 github.com/johlanse/wechat v0.0.0-20220909140933-49a96a4c0412
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0

2
go.sum
View File

@ -159,6 +159,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/huoxue1/xdaemon v0.0.0-20221003021252-8ce95c07edb0 h1:SePyM2Way5A1kaXfMfaES0uBrsiDEJqKGxmFCpbLcQc=
github.com/huoxue1/xdaemon v0.0.0-20221003021252-8ce95c07edb0/go.mod h1:2o/jLOwlNdu8qpZc0nYDbPGhPgKkpkG8JdcQn7HY1gA=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imroc/req/v3 v3.8.2 h1:wFZ7B0dclCQyjClP5GwXRboUGIek5l0mCpodrGgT01c= github.com/imroc/req/v3 v3.8.2 h1:wFZ7B0dclCQyjClP5GwXRboUGIek5l0mCpodrGgT01c=

View File

@ -389,6 +389,7 @@ func (c *Core) initNotWindows() {
func (c *Core) Quit() { func (c *Core) Quit() {
err := c.browser.Close() err := c.browser.Close()
if err != nil { if err != nil {
log.Errorln("关闭浏览器失败" + err.Error())
return return
} }
err = c.pw.Stop() err = c.pw.Stop()

75
main.go
View File

@ -7,8 +7,11 @@ import (
"math/rand" "math/rand"
"net/http" "net/http"
"os" "os"
"os/exec"
"os/signal"
"path" "path"
"strconv" "strconv"
"syscall"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -17,6 +20,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
nested "github.com/Lyrics-you/sail-logrus-formatter/sailor" nested "github.com/Lyrics-you/sail-logrus-formatter/sailor"
"github.com/huoxue1/xdaemon"
"github.com/johlanse/study_xxqg/conf" "github.com/johlanse/study_xxqg/conf"
"github.com/johlanse/study_xxqg/utils" "github.com/johlanse/study_xxqg/utils"
@ -37,7 +41,7 @@ var (
var VERSION = "unknown" var VERSION = "unknown"
func init() { func init() {
runBack()
fmt.Printf("\033[1;31;40m%s\033[0m\n\n", "******************************************************************") fmt.Printf("\033[1;31;40m%s\033[0m\n\n", "******************************************************************")
fmt.Printf("\033[1;31;40m%s\033[0m\n\n", "软件仅可用户学习和个人使用,禁止用于任何商业活动!!!!") fmt.Printf("\033[1;31;40m%s\033[0m\n\n", "软件仅可用户学习和个人使用,禁止用于任何商业活动!!!!")
@ -47,7 +51,7 @@ func init() {
fmt.Printf("\033[1;31;40m%s\033[0m\n\n", "软件仅可用户学习和个人使用,禁止用于任何商业活动!!!!") fmt.Printf("\033[1;31;40m%s\033[0m\n\n", "软件仅可用户学习和个人使用,禁止用于任何商业活动!!!!")
fmt.Printf("\033[1;31;40m%s\033[0m\n\n", "******************************************************************") fmt.Printf("\033[1;31;40m%s\033[0m\n\n", "******************************************************************")
time.Sleep(5 * time.Second) time.Sleep(3 * time.Second)
flag.BoolVar(&u, "u", false, "更新应用") flag.BoolVar(&u, "u", false, "更新应用")
flag.BoolVar(&i, "init", false, "init the app") flag.BoolVar(&i, "init", false, "init the app")
@ -235,10 +239,6 @@ func do(m string) {
} }
}() }()
log.Infoln(` 刷课模式默认为1
1只刷文章何视频
2只刷文章和视频和每日答题
3刷文章和视频和每日答题每周答题和专项答题`)
log.Infoln("检测到模式", config.Model) log.Infoln("检测到模式", config.Model)
getPush := push.GetPush(config) getPush := push.GetPush(config)
@ -284,20 +284,20 @@ func do(m string) {
core2.Push(u.PushId, "flush", message) core2.Push(u.PushId, "flush", message)
} }
c := make(chan *model.User, 1) //c := make(chan *model.User, 1)
//
go func() { //go func() {
for true { // for true {
u := <-c // u := <-c
if u.UID == "" { // if u.UID == "" {
break // break
} else { // } else {
l := &lib.Core{Push: getPush, ShowBrowser: config.ShowBrowser} // l := &lib.Core{Push: getPush, ShowBrowser: config.ShowBrowser}
l.Init() // l.Init()
study(l, u) // study(l, u)
} // }
} // }
}() //}()
failUser, _ := model.QueryFailUser() failUser, _ := model.QueryFailUser()
for _, user := range failUser { for _, user := range failUser {
@ -382,3 +382,38 @@ func do(m string) {
study(core, user) study(core, user)
core.Push(user.PushId, "flush", "") core.Push(user.PushId, "flush", "")
} }
func runBack() {
cmd, err := xdaemon.Background(os.Stdout, false)
if err != nil {
log.Fatalln(err.Error())
}
if xdaemon.IsParent() {
go onKill(cmd)
for true {
_ = cmd.Wait()
if cmd.ProcessState.Exited() {
if cmd.ProcessState.ExitCode() != 1001 {
break
}
}
cmd, err = xdaemon.Background(os.Stdout, false)
if err != nil {
return
}
}
os.Exit(0)
}
}
func onKill(cmd *exec.Cmd) {
c := make(chan os.Signal)
//监听指定信号 ctrl+c kill
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-c
if cmd.Process != nil {
cmd.Process.Kill()
}
os.Exit(1)
}

View File

@ -24,7 +24,7 @@ func (d *Ding) Send() func(id string, kind string, message string) {
if kind == "flush" { if kind == "flush" {
if strings.Contains(message, "login.xuexi.cn") { if strings.Contains(message, "login.xuexi.cn") {
message = fmt.Sprintf("[点我登录](%v)", "dtxuexi://appclient/page/study_feeds?url="+strings.Split(message, "?")[1]) message = fmt.Sprintf("[点我登录](%v)", message)
} }
err := s.SendMessage(map[string]interface{}{ err := s.SendMessage(map[string]interface{}{

View File

@ -6,9 +6,6 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"os/exec"
"strings"
"sync"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
@ -21,16 +18,17 @@ import (
/* @Description: /* @Description:
*/ */
func Restart() { func Restart() {
once := sync.Once{} //once := sync.Once{}
once.Do(func() { //once.Do(func() {
log.Infoln("程序启动命令: " + strings.Join(os.Args, " ")) // log.Infoln("程序启动命令: " + strings.Join(os.Args, " "))
cmd := exec.Command(strings.Join(os.Args, " ")) // cmd := exec.Command(strings.Join(os.Args, " "))
cmd.Stdout = os.Stdout // cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr // cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin // cmd.Stdin = os.Stdin
cmd.Start() // cmd.Start()
os.Exit(3) // os.Exit(3)
}) //})
os.Exit(1001)
} }

View File

@ -1,7 +1,7 @@
{ {
"files": { "files": {
"main.css": "/static/xxqg/build/static/css/main.fc31f0e5.css", "main.css": "/static/xxqg/build/static/css/main.fc31f0e5.css",
"main.js": "/static/xxqg/build/static/js/main.677fce24.js", "main.js": "/static/xxqg/build/static/js/main.bd279fde.js",
"static/js/312.600b1c1c.chunk.js": "/static/xxqg/build/static/js/312.600b1c1c.chunk.js", "static/js/312.600b1c1c.chunk.js": "/static/xxqg/build/static/js/312.600b1c1c.chunk.js",
"static/js/4877.63f2a077.chunk.js": "/static/xxqg/build/static/js/4877.63f2a077.chunk.js", "static/js/4877.63f2a077.chunk.js": "/static/xxqg/build/static/js/4877.63f2a077.chunk.js",
"static/js/1074.4c2739c5.chunk.js": "/static/xxqg/build/static/js/1074.4c2739c5.chunk.js", "static/js/1074.4c2739c5.chunk.js": "/static/xxqg/build/static/js/1074.4c2739c5.chunk.js",
@ -88,7 +88,7 @@
"static/media/codicon.ttf": "/static/xxqg/build/static/media/codicon.b797181c93b3755f4fa1.ttf", "static/media/codicon.ttf": "/static/xxqg/build/static/media/codicon.b797181c93b3755f4fa1.ttf",
"index.html": "/static/xxqg/build/index.html", "index.html": "/static/xxqg/build/index.html",
"main.fc31f0e5.css.map": "/static/xxqg/build/static/css/main.fc31f0e5.css.map", "main.fc31f0e5.css.map": "/static/xxqg/build/static/css/main.fc31f0e5.css.map",
"main.677fce24.js.map": "/static/xxqg/build/static/js/main.677fce24.js.map", "main.bd279fde.js.map": "/static/xxqg/build/static/js/main.bd279fde.js.map",
"312.600b1c1c.chunk.js.map": "/static/xxqg/build/static/js/312.600b1c1c.chunk.js.map", "312.600b1c1c.chunk.js.map": "/static/xxqg/build/static/js/312.600b1c1c.chunk.js.map",
"4877.63f2a077.chunk.js.map": "/static/xxqg/build/static/js/4877.63f2a077.chunk.js.map", "4877.63f2a077.chunk.js.map": "/static/xxqg/build/static/js/4877.63f2a077.chunk.js.map",
"1074.4c2739c5.chunk.js.map": "/static/xxqg/build/static/js/1074.4c2739c5.chunk.js.map", "1074.4c2739c5.chunk.js.map": "/static/xxqg/build/static/js/1074.4c2739c5.chunk.js.map",
@ -175,6 +175,6 @@
}, },
"entrypoints": [ "entrypoints": [
"static/css/main.fc31f0e5.css", "static/css/main.fc31f0e5.css",
"static/js/main.677fce24.js" "static/js/main.bd279fde.js"
] ]
} }

View File

@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/static/xxqg/build/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/static/xxqg/build/logo192.png"/><link rel="manifest" href="/static/xxqg/build/manifest.json"/><title>Study XXQG</title><script defer="defer" src="/static/xxqg/build/static/js/main.677fce24.js"></script><link href="/static/xxqg/build/static/css/main.fc31f0e5.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html> <!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/static/xxqg/build/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/static/xxqg/build/logo192.png"/><link rel="manifest" href="/static/xxqg/build/manifest.json"/><title>Study XXQG</title><script defer="defer" src="/static/xxqg/build/static/js/main.bd279fde.js"></script><link href="/static/xxqg/build/static/css/main.fc31f0e5.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

File diff suppressed because one or more lines are too long

View File

@ -45,7 +45,7 @@ class Home extends Component<any, any>{
</Routes> </Routes>
<div style={{position:"fixed","height":"60px",width:"100%",bottom:0,zIndex:9}}> <div style={{position:"fixed","height":"60px",width:"100%",bottom:0,zIndex:9,color: "#f0f", backgroundColor: "#5f6d6e"}}>
<TabBar activeKey={this.props.location.pathname} onChange={value => this.props.navigate("/home"+value)}> <TabBar activeKey={this.props.location.pathname} onChange={value => this.props.navigate("/home"+value)}>
{tableItems.map(item => ( {tableItems.map(item => (