diff --git a/.gitignore b/.gitignore index 9649294..dfbd02d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -deployment/Dockerfile.dev \ No newline at end of file +deployment/compose-dev.yml \ No newline at end of file diff --git a/README.md b/README.md index 99412d7..3a63491 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,22 @@ go build ```bash # docker build -docker build -f deployment/Dockerfile -t dcronapp:latest . +docker build -f deployment/Dockerfile -t dcron_app:v1 . +``` + +## Run in docker compose + +You can change the app.yaml volume to change the configuration. +```yaml +version: '3.0' +services: + app: + image: dcron_app:v1 + scale: 3 + volumes: + - /path/to/yourself-app.yaml:/app/etc/app.yaml + ports: + - 10010-10012:8080 ``` ## Features @@ -33,7 +48,7 @@ docker build -f deployment/Dockerfile -t dcronapp:latest . ---|--- Done|MySQL TODO|PostgreSQL -TODO|Monitor +Done|Monitor TODO|Use ETCD to run dcron TODO|Use service discovery to implement inner call. diff --git a/app/config/config.go b/app/config/config.go index ee96494..66d4c84 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -7,11 +7,12 @@ import ( ) type ConfigType struct { - Port uint `yaml:"port"` - Redis RedisType `yaml:"redis"` - MySQL MySQLType `yaml:"mysql"` - Dcron DcronType `yaml:"dcron"` - InnerCall InnerCallType `yaml:"innerCall"` + Port uint `yaml:"port"` + Redis RedisType `yaml:"redis"` + MySQL MySQLType `yaml:"mysql"` + Dcron DcronType `yaml:"dcron"` + InnerCall InnerCallType `yaml:"innerCall"` + EnableReporter bool `yaml:"enableReporter"` } type RedisType struct { @@ -30,6 +31,9 @@ type DcronType struct { ServiceName string `yaml:"serviceName"` } +// channel name and channel redis configuration, +// TODO: +// remove redis dependency in inner call. type InnerCallType struct { Channel string `yaml:"channel"` Redis RedisType `yaml:"redis"` diff --git a/app/etc/app.yaml b/app/etc/app.yaml index fe825d1..40a38c1 100644 --- a/app/etc/app.yaml +++ b/app/etc/app.yaml @@ -16,4 +16,6 @@ innerCall: channel: dcronapp-channel redis: addr: redis:6379 - db: 2 \ No newline at end of file + db: 2 + +enableReporter: false \ No newline at end of file diff --git a/app/internal/crontasks/crontask.go b/app/internal/crontasks/crontask.go index 686b552..8f1d720 100644 --- a/app/internal/crontasks/crontask.go +++ b/app/internal/crontasks/crontask.go @@ -22,6 +22,11 @@ func (cronTask *CronTask) Run() { "name=%s|dbname=%s|sql-str=`%s`", cronTask.Name, cronTask.DBCustomerName, cronTask.SQLStr) + dbi := db.SelfStoreUtil{}.I() + go dbi.ReportTaskMetric(&db.TaskMetric{ + TaskName: cronTask.Name, + NodeID: CronTasksContainerUtil{}.I().dcronInstance.NodeID(), + }) if err := cronTask.dbconn.Exec(cronTask.SQLStr).Error; err != nil { logrus.Error(err) } diff --git a/app/internal/crontasks/crontaskscontainer.go b/app/internal/crontasks/crontaskscontainer.go index 21dce74..9ff611a 100644 --- a/app/internal/crontasks/crontaskscontainer.go +++ b/app/internal/crontasks/crontaskscontainer.go @@ -57,6 +57,9 @@ func (du CronTasksContainerUtil) Initial() { logrus.Error(err) continue } + if len(tasks) == 0 { + break + } for _, task := range tasks { cronTask := &CronTask{} cronTask.FromDBTask(&task) diff --git a/app/internal/db/model.go b/app/internal/db/model.go index ff3a993..10f746a 100644 --- a/app/internal/db/model.go +++ b/app/internal/db/model.go @@ -5,6 +5,8 @@ import ( "app/internal/common" "fmt" "strings" + + "gorm.io/gorm" ) type Task struct { @@ -24,6 +26,13 @@ type Database struct { Addr string `gorm:"type:VARCHAR(128)"` } +type TaskMetric struct { + gorm.Model + + TaskName string + NodeID string +} + func (db Database) DSN() string { if db.DBTypeEnum() == common.DBTypeMYSQL { return fmt.Sprintf(common.MySQLDSNFormat, db.User, db.Password, db.Addr, db.DatabaseName) diff --git a/app/internal/db/selfstore.go b/app/internal/db/selfstore.go index d3a82d2..9c4e2b6 100644 --- a/app/internal/db/selfstore.go +++ b/app/internal/db/selfstore.go @@ -3,6 +3,7 @@ package db import ( "app/config" + "github.com/sirupsen/logrus" "gorm.io/driver/mysql" "gorm.io/gorm" ) @@ -28,9 +29,20 @@ func newSelfStore() (ss *SelfStore, err error) { if err != nil { return nil, err } + + if config.I().EnableReporter { + err = ss.db.AutoMigrate(&TaskMetric{}) + if err != nil { + logrus.Error(err) + } + } return } +func (ss *SelfStore) ReportTaskMetric(tm *TaskMetric) { + _ = ss.db.Save(tm) +} + func (ss *SelfStore) UpsertDataBase(db *Database) (err error) { return ss.db.Save(db).Error } @@ -70,6 +82,7 @@ func (ss *SelfStore) GetTaskID(taskName string) (ID uint, err error) { } func (ss *SelfStore) GetTasksByIDLimit(ID uint, limit int) (tasks []Task, err error) { + tasks = make([]Task, 0) err = ss.db.Where("id >= ?", ID).Order("id").Limit(limit).Find(&tasks).Error return } diff --git a/dcron b/dcron index 6c06804..ff86fbc 160000 --- a/dcron +++ b/dcron @@ -1 +1 @@ -Subproject commit 6c06804a20dcfa0b24090f03914ff9807794fb4d +Subproject commit ff86fbc24931c3e4312d17ad255aed04e9e7e87d