Skip to content

Commit

Permalink
feat: add github workflow (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinzhong authored Oct 9, 2023
1 parent dd15cf6 commit 3ddc321
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: for pull request
on:
pull_request:
types: [opened, reopened]
push:

jobs:
build:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:5.7
env:
MYSQL_DATABASE: my_db
MYSQL_ROOT_PASSWORD: 123456
ports:
- 3306:3306
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.20.x'
- name: Install dependencies
run: go get .
- name: Build
run: go build -v ./...
- name: Test with the Go CLI
run: go test -v ./...
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,6 @@ DDD Firework is a framework that supports the implementation of DDD (Domain-Driv
))
}

# More Example

ref: example/main.go
3 changes: 3 additions & 0 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,6 @@ dddfirework 是一个支持 DDD (领域驱动设计)实现的引擎框架,
))
}

# 更多示例

参考: example/main.go
31 changes: 21 additions & 10 deletions eventbus/mysql/eventbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const txCheckTimeout = time.Minute

var ErrInvalidDB = fmt.Errorf("invalid db")
var ErrNoTransaction = fmt.Errorf("no transaction")
var ErrServiceNotCreate = fmt.Errorf("service not create")

var defaultLogger = stdr.New(stdlog.New(os.Stderr, "", stdlog.LstdFlags|stdlog.Lshortfile)).WithName("mysql_eventbus")

Expand Down Expand Up @@ -189,7 +190,8 @@ func NewEventBus(serviceName string, db *gorm.DB, options ...Option) *EventBus {
} else {
strategy = &IntervalRetry{Interval: retryInterval, Limit: retryLimit}
}
return &EventBus{

eb := &EventBus{
serviceName: serviceName,
db: db,
logger: defaultLogger,
Expand All @@ -198,12 +200,13 @@ func NewEventBus(serviceName string, db *gorm.DB, options ...Option) *EventBus {
txKey: contextKey(fmt.Sprintf("eventbus_tx_%d", time.Now().Unix())),
cleanCron: cron.New(),
}
_ = eb.initService()
return eb
}

func (e *EventBus) Options() []dddfirework.Option {
return []dddfirework.Option{
dddfirework.WithEventBus(e),
//dddfirework.WithEventPersist(eventPersist),
dddfirework.WithPostSave(e.onPostSave),
}
}
Expand Down Expand Up @@ -313,17 +316,22 @@ func (e *EventBus) RegisterEventHandler(cb dddfirework.DomainEventHandler) {
e.cb = cb
}

// 初次创建无记录情况下锁是无效的,因此采用插入后再重新竞争 update 锁的方式解决
func (e *EventBus) getOrCreateService(tx *gorm.DB) (*ServicePO, error) {
service := &ServicePO{Name: e.serviceName}
func (e *EventBus) initService() error {
service := &ServicePO{}
err := e.db.Where(ServicePO{Name: e.serviceName}).FirstOrCreate(service).Error
if err != nil {
return err
}
return nil
}

func (e *EventBus) lockService(tx *gorm.DB) (*ServicePO, error) {
service := &ServicePO{}
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
Where("name = ?", e.serviceName).
First(service).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if err := tx.Create(service).Error; err != nil {
return nil, err
}
return e.getOrCreateService(tx)
return nil, ErrServiceNotCreate
}
return nil, err
}
Expand Down Expand Up @@ -433,7 +441,7 @@ func (e *EventBus) handleEvents() error {

return e.db.Transaction(func(tx *gorm.DB) error {
ctx := context.Background()
service, err := e.getOrCreateService(tx)
service, err := e.lockService(tx)
if err != nil {
return err
}
Expand Down Expand Up @@ -592,6 +600,9 @@ func (e *EventBus) Start(ctx context.Context) {

if e.cb != nil {
if err := e.handleEvents(); err != nil {
if errors.Is(err, ErrServiceNotCreate) {
_ = e.initService()
}
e.logger.Error(err, "handler events err")
}
}
Expand Down
Binary file modified img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions testsuit/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type MySQLOption struct {
// InitMysql 启动测试数据库
// 前提: 在根目录执行 docker-compose up 命令
func InitMysql(opts ...MySQLOption) *gorm.DB {
dsn := "root:@tcp(mysql:3306)/my_db?parseTime=true&loc=Local"
dsn := "root:123456@tcp(127.0.0.1:3306)/my_db?parseTime=true&loc=Local"
if os.Getenv("LOCAL_TEST") == "true" {
dsn = "root:@tcp(localhost:3308)/my_db?parseTime=true&loc=Local"
}
Expand All @@ -47,7 +47,7 @@ func InitMysqlWithDatabase(db *gorm.DB, database string) *gorm.DB {
if err != nil {
panic(err)
}
dsn := fmt.Sprintf("root:@tcp(mysql:3306)/%s?parseTime=true&loc=Local", database)
dsn := fmt.Sprintf("root:123456@tcp(127.0.0.1:3306)/%s?parseTime=true&loc=Local", database)
if os.Getenv("LOCAL_TEST") == "true" {
dsn = fmt.Sprintf("root:@tcp(localhost:3308)/%s?parseTime=true&loc=Local", database)
}
Expand Down

0 comments on commit 3ddc321

Please sign in to comment.