From e8722e6df504849723bb3e040ae48b1a9d8dec74 Mon Sep 17 00:00:00 2001 From: "suguo.yao" Date: Mon, 17 Oct 2022 13:59:10 +0800 Subject: [PATCH] init --- main.go | 5 ++++- model/domain-model.go | 13 +++++++++++++ model/site-model.go | 11 ++++++----- service/domain-service.go | 31 +++++++++++++++++++++++++++++++ service/probe-service.go | 34 ++++++++++++++++++++++++++++++++++ service/site-service.go | 5 +++++ 6 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 model/domain-model.go create mode 100644 service/domain-service.go create mode 100644 service/probe-service.go create mode 100644 service/site-service.go diff --git a/main.go b/main.go index a08e655..ab0b76f 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/model/domain-model.go b/model/domain-model.go new file mode 100644 index 0000000..e3cd18a --- /dev/null +++ b/model/domain-model.go @@ -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 +} diff --git a/model/site-model.go b/model/site-model.go index 466ad13..a90186c 100644 --- a/model/site-model.go +++ b/model/site-model.go @@ -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 } diff --git a/service/domain-service.go b/service/domain-service.go new file mode 100644 index 0000000..1953148 --- /dev/null +++ b/service/domain-service.go @@ -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 + } + +} diff --git a/service/probe-service.go b/service/probe-service.go new file mode 100644 index 0000000..cf9dfae --- /dev/null +++ b/service/probe-service.go @@ -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) + } + } +} diff --git a/service/site-service.go b/service/site-service.go new file mode 100644 index 0000000..0f21f2f --- /dev/null +++ b/service/site-service.go @@ -0,0 +1,5 @@ +package service + +func siteAccess() { + +}