Skip to content

Commit a72a2cd

Browse files
committed
feat(AdminUI): add setNextCronTime functionality for global transactions
1 parent 18146ee commit a72a2cd

10 files changed

Lines changed: 99 additions & 1 deletion

File tree

admin/src/api/api_dtm.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ export function resetNextCronTime(gid: string): Promise<AxiosResponse> {
100100
})
101101
}
102102

103+
export function setNextCronTime(gid: string, time: Date): Promise<AxiosResponse> {
104+
return request({
105+
url: '/api/dtmsvr/setNextCronTime',
106+
method: 'post',
107+
data: { gid, next_cron_time: time}
108+
})
109+
}
110+
103111
export function getDtmVersion(): Promise<AxiosResponse<any>> {
104112
return request({
105113
url: '/api/dtmsvr/version',

admin/src/components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare module 'vue' {
1111
ABreadcrumb: typeof import('ant-design-vue/es')['Breadcrumb']
1212
ABreadcrumbItem: typeof import('ant-design-vue/es')['BreadcrumbItem']
1313
AButton: typeof import('ant-design-vue/es')['Button']
14+
ADatePicker: typeof import('ant-design-vue/es')['DatePicker']
1415
ADescriptions: typeof import('ant-design-vue/es')['Descriptions']
1516
ADescriptionsItem: typeof import('ant-design-vue/es')['DescriptionsItem']
1617
ADivider: typeof import('ant-design-vue/es')['Divider']

admin/src/views/Dashboard/GlobalTransactions/DialogTransactionDetail.vue

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@
2626
class="action-button"
2727
@confirm="handleSetNextCronTimeToNow(<string>transaction?.gid)" >
2828
<a-button type="default">Reset next cron time</a-button>
29+
</a-popconfirm>
30+
<a-date-picker
31+
v-model:value="nextCronTimeInput"
32+
format="YYYY-MM-DD HH:mm:ss"
33+
placeholder="Set NextCronTime"
34+
show-time
35+
/>
36+
<a-popconfirm
37+
title="Set next cron time?"
38+
ok-text="Yes, reset"
39+
cancel-text="No"
40+
class="action-button"
41+
:disabled="!nextCronTimeInput"
42+
@confirm="handleSetNextCronTime(<string>transaction?.gid)" >
43+
<a-button type="default" :disabled="!nextCronTimeInput">Set next cron time</a-button>
2944
</a-popconfirm>
3045
<a-descriptions bordered size="small" :column="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }">
3146
<a-descriptions-item label="Status">
@@ -61,7 +76,7 @@ import { getTransaction } from '/@/api/api_dtm'
6176
import screenfull from '/@/components/Screenfull/index.vue'
6277
import { useRoute } from 'vue-router';
6378
import { string } from 'vue-types';
64-
import { forceStopTransaction, resetNextCronTime } from '/@/api/api_dtm'
79+
import { forceStopTransaction, resetNextCronTime, setNextCronTime } from '/@/api/api_dtm'
6580
// import VueJsonPretty from 'vue-json-pretty';
6681
// import 'vue-json-pretty/lib/styles.css'
6782
const route = useRoute();
@@ -72,6 +87,7 @@ const transaction = ref<Transaction>()
7287
const visible = ref(false)
7388
const textVal = ref('')
7489
const closeable = ref(true)
90+
const nextCronTimeInput = ref()
7591
7692
7793
let _gid = <string>route.params.gid;
@@ -137,6 +153,11 @@ const handleSetNextCronTimeToNow = async(gid: string) => {
137153
refresh();
138154
}
139155
156+
const handleSetNextCronTime = async(gid: string) => {
157+
await setNextCronTime(gid, nextCronTimeInput.value);
158+
refresh();
159+
}
160+
140161
type Data = {
141162
branches: {
142163
gid: string

dtmsvr/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ func svcResetNextCronTime(t *TransGlobal) error {
9393
return dbt.resetNextCronTime()
9494
}
9595

96+
func svcSetNextCronTime(t *TransGlobal, dt *time.Time) error {
97+
dbt := GetTransGlobal(t.Gid)
98+
dbt.NextCronTime = dt
99+
return dbt.setNextCronTime()
100+
}
101+
96102
func svcRegisterBranch(transType string, branch *TransBranch, data map[string]string) error {
97103
branches := []TransBranch{*branch, *branch}
98104
if transType == "tcc" {

dtmsvr/api_http.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func addRoute(engine *gin.Engine) {
4141
engine.GET("/api/dtmsvr/scanKV", dtmutil.WrapHandler2(scanKV))
4242
engine.GET("/api/dtmsvr/queryKV", dtmutil.WrapHandler2(queryKV))
4343
engine.POST("/api/dtmsvr/resetNextCronTime", dtmutil.WrapHandler2(resetNextCronTime)) // one global trans only
44+
engine.POST("/api/dtmsvr/setNextCronTime", dtmutil.WrapHandler2(setNextCronTime)) // one global trans only
4445

4546
// add prometheus exporter
4647
h := promhttp.Handler()
@@ -74,6 +75,11 @@ func resetNextCronTime(c *gin.Context) interface{} {
7475
return svcResetNextCronTime(TransFromContext(c))
7576
}
7677

78+
func setNextCronTime(c *gin.Context) interface{} {
79+
trans := TransFromContext(c)
80+
return svcSetNextCronTime(trans, trans.NextCronTime)
81+
}
82+
7783
func registerBranch(c *gin.Context) interface{} {
7884
data := map[string]string{}
7985
err := c.BindJSON(&data)

dtmsvr/storage/boltdb/boltdb.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,24 @@ func (s *Store) ResetTransGlobalCronTime(g *storage.TransGlobalStore) error {
501501
return err
502502
}
503503

504+
// SetTransGlobalCronTime set nextCronTime of one global trans.
505+
func (s *Store) SetTransGlobalCronTime(g *storage.TransGlobalStore) error {
506+
err := s.boltDb.Update(func(t *bolt.Tx) error {
507+
dt := g.NextCronTime
508+
g := tGetGlobal(t, g.Gid)
509+
if g == nil {
510+
return storage.ErrNotFound
511+
}
512+
now := dtmutil.GetNextTime(0)
513+
g.NextCronTime = dt
514+
g.UpdateTime = now
515+
tPutGlobal(t, g)
516+
return nil
517+
})
518+
dtmimp.E2P(err)
519+
return err
520+
}
521+
504522
// ScanKV lists KV pairs
505523
func (s *Store) ScanKV(cat string, position *string, limit int64) []storage.KVStore {
506524
kvs := []storage.KVStore{}

dtmsvr/storage/redis/redis.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,15 @@ func (s *Store) ResetTransGlobalCronTime(global *storage.TransGlobalStore) error
344344
return err
345345
}
346346

347+
// SetTransGlobalCronTime set nextCronTime of one global trans.
348+
func (s *Store) SetTransGlobalCronTime(global *storage.TransGlobalStore) error {
349+
now := dtmutil.GetNextTime(0)
350+
global.UpdateTime = now
351+
key := conf.Store.RedisPrefix + "_g_" + global.Gid
352+
_, err := redisGet().Set(ctx, key, dtmimp.MustMarshalString(global), time.Duration(conf.Store.DataExpire)*time.Second).Result()
353+
return err
354+
}
355+
347356
// TouchCronTime updates cronTime
348357
func (s *Store) TouchCronTime(global *storage.TransGlobalStore, nextCronInterval int64, nextCronTime *time.Time) {
349358
global.UpdateTime = dtmutil.GetNextTime(0)

dtmsvr/storage/sql/sql.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ func (s *Store) ResetTransGlobalCronTime(global *storage.TransGlobalStore) error
227227
return err
228228
}
229229

230+
// ResetTransGlobalCronTime set nextCronTime of one global trans.
231+
func (s *Store) SetTransGlobalCronTime(global *storage.TransGlobalStore) error {
232+
timeStr := getTimeSqlStr(global.NextCronTime)
233+
now := getTimeStr(0)
234+
sql := fmt.Sprintf(`UPDATE trans_global SET update_time='%s',next_cron_time='%s' WHERE gid = '%s'`,
235+
now,
236+
timeStr,
237+
global.Gid)
238+
_, err := dtmimp.DBExec(conf.Store.Driver, dbGet().ToSQLDB(), sql)
239+
return err
240+
}
241+
230242
// ScanKV lists KV pairs
231243
func (s *Store) ScanKV(cat string, position *string, limit int64) []storage.KVStore {
232244
kvs := []storage.KVStore{}
@@ -328,3 +340,10 @@ func getTimeStr(afterSecond int64) string {
328340
}
329341
return dtmutil.GetNextTime(afterSecond).Format("2006-01-02 15:04:05")
330342
}
343+
344+
func getTimeSqlStr(dt *time.Time) string {
345+
if conf.Store.Driver == config.SQLServer {
346+
return dt.Format(time.RFC3339)
347+
}
348+
return dt.Format("2006-01-02 15:04:05")
349+
}

dtmsvr/storage/store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Store interface {
3232
LockOneGlobalTrans(expireIn time.Duration) *TransGlobalStore
3333
ResetCronTime(after time.Duration, limit int64) (succeedCount int64, hasRemaining bool, err error)
3434
ResetTransGlobalCronTime(global *TransGlobalStore) error
35+
SetTransGlobalCronTime(global *TransGlobalStore) error
3536
ScanKV(cat string, position *string, limit int64) []KVStore
3637
FindKV(cat, key string) []KVStore
3738
UpdateKV(kv *KVStore) error

dtmsvr/trans_status.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ func (t *TransGlobal) resetNextCronTime() error {
9595
return nil
9696
}
9797

98+
func (t *TransGlobal) setNextCronTime() error {
99+
err := GetStore().SetTransGlobalCronTime(&t.TransGlobalStore)
100+
if err != nil {
101+
return err
102+
}
103+
logger.Infof("SetTransGlobalCronTime to %s for %s", t.NextCronTime, t.TransGlobalStore.String())
104+
return nil
105+
}
106+
98107
func (t *TransGlobal) changeBranchStatus(b *TransBranch, status string, branchPos int) {
99108
now := time.Now()
100109
b.Status = status

0 commit comments

Comments
 (0)