Skip to content

Commit

Permalink
update documentation and monitor (#3)
Browse files Browse the repository at this point in the history
* update documentation

* update documentation

* update task_metrics table to save the running node

* update documentation
  • Loading branch information
dxyinme authored Oct 10, 2023
1 parent eb22dda commit 508d5bc
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
deployment/Dockerfile.dev
deployment/compose-dev.yml
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
14 changes: 9 additions & 5 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"`
Expand Down
4 changes: 3 additions & 1 deletion app/etc/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ innerCall:
channel: dcronapp-channel
redis:
addr: redis:6379
db: 2
db: 2

enableReporter: false
5 changes: 5 additions & 0 deletions app/internal/crontasks/crontask.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions app/internal/crontasks/crontaskscontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions app/internal/db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"app/internal/common"
"fmt"
"strings"

"gorm.io/gorm"
)

type Task struct {
Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions app/internal/db/selfstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"app/config"

"github.com/sirupsen/logrus"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion dcron
Submodule dcron updated 3 files
+5 −1 README.md
+6 −1 README_CN.md
+4 −0 dcron.go

0 comments on commit 508d5bc

Please sign in to comment.