This commit is contained in:
suguo.yao 2022-10-17 13:59:10 +08:00
parent 5ff5a33222
commit e8722e6df5
6 changed files with 93 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import (
"myschools.me/suguo/search/exceptionless"
"myschools.me/suguo/search/model"
"myschools.me/suguo/search/mysql"
"myschools.me/suguo/search/service"
)
func main() {
@ -29,10 +30,12 @@ func main() {
if err != nil {
panic(err)
}
if err := db.AutoMigrate(&model.Site{}); err != nil {
if err := db.AutoMigrate(&model.Site{}, &model.Domain{}); err != nil {
panic(err)
}
go service.Probe()
// 服务停止相应
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

13
model/domain-model.go Normal file
View File

@ -0,0 +1,13 @@
package model
import "time"
type Domain struct {
Root string `gorm:"type:varchar(10);primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
Success uint
Failure uint
Current uint8
Status uint8
}

View File

@ -3,9 +3,10 @@ package model
import "time"
type Site struct {
URL string `gorm:"type:varchar(255);primarykey"`
Title string `gorm:"type:varchar(30);not null;"`
Description string `gorm:"type:varchar(255);not null;"`
UpdatedAt time.Time `gorm:"index"`
CreatedAt time.Time
URL string `gorm:"type:varchar(255);primarykey"`
Title string `gorm:"type:varchar(30);not null;"`
Description string `gorm:"type:varchar(255);not null;"`
AccessDuration uint `gorm:"index"`
UpdatedAt time.Time `gorm:"index"`
CreatedAt time.Time
}

31
service/domain-service.go Normal file
View File

@ -0,0 +1,31 @@
package service
import (
"time"
"myschools.me/suguo/search/exceptionless"
"myschools.me/suguo/search/model"
"myschools.me/suguo/search/mysql"
)
func domainAnalyze(domain *model.Domain) {
if domain.Status != 0 && domain.UpdatedAt.After(time.Now().Add(10*time.Minute)) {
return
}
db, err := mysql.New()
if err != nil {
exceptionless.SubmitAppError("domainAnalyze", "mysql.New", nil, err)
return
}
if domain.Current >= uint8(250-len(domain.Root)) {
domain.Current = 1
}
domain.Status = 1 //置状态处理中
if err := db.Updates(domain).Error; err != nil {
exceptionless.SubmitAppError("domainAnalyze", "mysql.Updates", nil, err)
return
}
}

34
service/probe-service.go Normal file
View File

@ -0,0 +1,34 @@
package service
import (
"time"
"gorm.io/gorm"
"myschools.me/suguo/search/exceptionless"
"myschools.me/suguo/search/model"
"myschools.me/suguo/search/mysql"
)
// 探针
func Probe() {
for {
time.Sleep(time.Minute)
db, err := mysql.New()
if err != nil {
exceptionless.SubmitAppError("Probe", "mysql.New", nil, err)
continue
}
var domains []*model.Domain
if err := db.Find(&domains).Error; err != nil {
if err != gorm.ErrRecordNotFound {
exceptionless.SubmitAppError("Probe", "mysql.Find", nil, err)
continue
}
}
for _, d := range domains {
domainAnalyze(d)
}
}
}

5
service/site-service.go Normal file
View File

@ -0,0 +1,5 @@
package service
func siteAccess() {
}