Skip to content

Commit

Permalink
feat(zentao): check db url when testing connections (apache#6258)
Browse files Browse the repository at this point in the history
* feat(zentao): check db url when testing connections

* fix(zentao): remove test codes

* fix(zentao): remove test codes
  • Loading branch information
d4x1 authored and sandesvitor committed Oct 19, 2023
1 parent 2c408a3 commit fc44e53
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
35 changes: 26 additions & 9 deletions backend/core/runner/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,32 @@ func getDbConnection(dbUrl string, conf *gorm.Config) (*gorm.DB, error) {
}

func CheckDbConnection(dbUrl string, d time.Duration) errors.Error {
db, err := getDbConnection(dbUrl, &gorm.Config{})
if err != nil {
return errors.Convert(err)
}
ctx := context.Background()
if d > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), d)
defer cancel()

result := make(chan errors.Error, 1)
done := make(chan struct{}, 1)
go func() {
db, err := getDbConnection(dbUrl, &gorm.Config{})
if err != nil {
result <- errors.Convert(err)
}
if d > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), d)
defer cancel()
}
if err := db.WithContext(ctx).Exec("SELECT 1").Error; err != nil {
done <- struct{}{}
} else {
result <- errors.Convert(err)
}
}()
select {
case <-time.After(d):
return errors.Default.New("timeout")
case <-done:
return nil
case err := <-result:
return err
}
return errors.Convert(db.WithContext(ctx).Exec("SELECT 1").Error)
}
2 changes: 1 addition & 1 deletion config-ui/src/api/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ export const test = (
plugin: string,
payload: Pick<
T.ConnectionForm,
'endpoint' | 'authMethod' | 'username' | 'password' | 'token' | 'appId' | 'secretKey' | 'proxy'
'endpoint' | 'authMethod' | 'username' | 'password' | 'token' | 'appId' | 'secretKey' | 'proxy' | 'dbUrl'
>,
): Promise<T.ConnectionTest> => request(`/plugins/${plugin}/test`, { method: 'post', data: payload });
2 changes: 2 additions & 0 deletions config-ui/src/api/connection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type Connection = {
password?: string;
proxy: string;
apiKey?: string;
dbUrl?: string;
};

export type ConnectionForm = {
Expand All @@ -40,6 +41,7 @@ export type ConnectionForm = {
enableGraphql?: boolean;
proxy: string;
rateLimitPerHour?: number;
dbUrl?: string;
};

export type ConnectionTest = {
Expand Down
1 change: 1 addition & 0 deletions config-ui/src/plugins/components/connection-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const ConnectionForm = ({ plugin, connectionId, onSuccess }: Props) => {
'secretKey',
'tenantId',
'tenantType',
"dbUrl",
]),
),
{
Expand Down
3 changes: 3 additions & 0 deletions config-ui/src/store/connections/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const ConnectionContextProvider = ({ children, ...props }: Props) => {
authMethod,
secretKey,
appId,
dbUrl,
}: ConnectionItemType) => {
try {
const res = await API.connection.test(plugin, {
Expand All @@ -83,6 +84,7 @@ export const ConnectionContextProvider = ({ children, ...props }: Props) => {
authMethod,
secretKey,
appId,
dbUrl,
});
return res.success ? ConnectionStatusEnum.ONLINE : ConnectionStatusEnum.OFFLINE;
} catch {
Expand All @@ -109,6 +111,7 @@ export const ConnectionContextProvider = ({ children, ...props }: Props) => {
authMethod: it.authMethod,
secretKey: it.secretKey,
appId: it.appId,
dbUrl: it.dbUrl,
}));
};

Expand Down
1 change: 1 addition & 0 deletions config-ui/src/store/connections/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ export type ConnectionItemType = {
authMethod?: string;
appId?: string;
secretKey?: string;
dbUrl?: string;
};

0 comments on commit fc44e53

Please sign in to comment.