Return error for RunSyncer()

This commit is contained in:
Yang Luo 2023-08-27 02:08:45 +08:00
parent 7e3c361ce7
commit 988b26b3c2
7 changed files with 74 additions and 51 deletions

View File

@ -160,7 +160,11 @@ func (c *ApiController) RunSyncer() {
return
}
object.RunSyncer(syncer)
err = object.RunSyncer(syncer)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk()
}

View File

@ -250,7 +250,7 @@ func (syncer *Syncer) getKey() string {
return key
}
func RunSyncer(syncer *Syncer) {
func RunSyncer(syncer *Syncer) error {
syncer.initAdapter()
syncer.syncUsers()
return syncer.syncUsers()
}

View File

@ -52,11 +52,14 @@ func addSyncerJob(syncer *Syncer) error {
syncer.initAdapter()
syncer.syncUsers()
err := syncer.syncUsers()
if err != nil {
return err
}
schedule := fmt.Sprintf("@every %ds", syncer.SyncInterval)
cron := getCronMap(syncer.Name)
_, err := cron.AddFunc(schedule, syncer.syncUsers)
_, err = cron.AddFunc(schedule, syncer.syncUsersNoError)
if err != nil {
return err
}

View File

@ -19,9 +19,9 @@ import (
"time"
)
func (syncer *Syncer) syncUsers() {
func (syncer *Syncer) syncUsers() error {
if len(syncer.TableColumns) == 0 {
return
return fmt.Errorf("The syncer table columns should not be empty")
}
fmt.Printf("Running syncUsers()..\n")
@ -35,10 +35,8 @@ func (syncer *Syncer) syncUsers() {
line := fmt.Sprintf("[%s] %s\n", timestamp, err.Error())
_, err = updateSyncerErrorText(syncer, line)
if err != nil {
panic(err)
return err
}
return
}
fmt.Printf("Users: %d, oUsers: %d\n", len(users), len(oUsers))
@ -71,28 +69,30 @@ func (syncer *Syncer) syncUsers() {
updatedUser := syncer.createUserFromOriginalUser(oUser, affiliationMap)
updatedUser.Hash = oHash
updatedUser.PreHash = oHash
fmt.Printf("Update from oUser to user: %v\n", updatedUser)
_, err = syncer.updateUserForOriginalByFields(updatedUser, key)
if err != nil {
panic(err)
return err
}
fmt.Printf("Update from oUser to user: %v\n", updatedUser)
}
} else {
if user.PreHash == oHash {
if !syncer.IsReadOnly {
updatedOUser := syncer.createOriginalUserFromUser(user)
fmt.Printf("Update from user to oUser: %v\n", updatedOUser)
_, err = syncer.updateUser(updatedOUser)
if err != nil {
panic(err)
return err
}
fmt.Printf("Update from user to oUser: %v\n", updatedOUser)
}
// update preHash
user.PreHash = user.Hash
_, err = SetUserField(user, "pre_hash", user.PreHash)
if err != nil {
panic(err)
return err
}
} else {
if user.Hash == oHash {
@ -100,17 +100,18 @@ func (syncer *Syncer) syncUsers() {
user.PreHash = user.Hash
_, err = SetUserField(user, "pre_hash", user.PreHash)
if err != nil {
panic(err)
return err
}
} else {
updatedUser := syncer.createUserFromOriginalUser(oUser, affiliationMap)
updatedUser.Hash = oHash
updatedUser.PreHash = oHash
fmt.Printf("Update from oUser to user (2nd condition): %v\n", updatedUser)
_, err = syncer.updateUserForOriginalByFields(updatedUser, key)
if err != nil {
panic(err)
return err
}
fmt.Printf("Update from oUser to user (2nd condition): %v\n", updatedUser)
}
}
}
@ -118,7 +119,7 @@ func (syncer *Syncer) syncUsers() {
}
_, err = AddUsersInBatch(newUsers)
if err != nil {
panic(err)
return err
}
if !syncer.IsReadOnly {
@ -126,12 +127,22 @@ func (syncer *Syncer) syncUsers() {
id := user.Id
if _, ok := oUserMap[id]; !ok {
newOUser := syncer.createOriginalUserFromUser(user)
fmt.Printf("New oUser: %v\n", newOUser)
_, err = syncer.addUser(newOUser)
if err != nil {
return err
}
}
}
}
return nil
}
func (syncer *Syncer) syncUsersNoError() {
err := syncer.syncUsers()
if err != nil {
panic(err)
}
fmt.Printf("New oUser: %v\n", newOUser)
}
}
}
}

View File

@ -234,12 +234,30 @@ class SyncerEditPage extends React.Component {
</Select>
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("syncer:Database type"), i18next.t("syncer:Database type - Tooltip"))} :
</Col>
<Col span={22} >
<Select virtual={false} style={{width: "100%"}} value={this.state.syncer.databaseType} onChange={(value => {this.updateSyncerField("databaseType", value);})}>
{
[
{id: "mysql", name: "MySQL"},
{id: "postgres", name: "PostgreSQL"},
{id: "mssql", name: "SQL Server"},
{id: "oracle", name: "Oracle"},
{id: "sqlite3", name: "Sqlite 3"},
].map((databaseType, index) => <Option key={index} value={databaseType.id}>{databaseType.name}</Option>)
}
</Select>
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("provider:Host"), i18next.t("provider:Host - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.syncer.host} onChange={e => {
<Input prefix={<LinkOutlined />} value={this.state.syncer.host} onChange={e => {
this.updateSyncerField("host", e.target.value);
}} />
</Col>
@ -274,24 +292,6 @@ class SyncerEditPage extends React.Component {
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("syncer:Database type"), i18next.t("syncer:Database type - Tooltip"))} :
</Col>
<Col span={22} >
<Select virtual={false} style={{width: "100%"}} value={this.state.syncer.databaseType} onChange={(value => {this.updateSyncerField("databaseType", value);})}>
{
[
{id: "mysql", name: "MySQL"},
{id: "postgres", name: "PostgreSQL"},
{id: "mssql", name: "SQL Server"},
{id: "oracle", name: "Oracle"},
{id: "sqlite3", name: "Sqlite 3"},
].map((databaseType, index) => <Option key={index} value={databaseType.id}>{databaseType.name}</Option>)
}
</Select>
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("syncer:Database"), i18next.t("syncer:Database - Tooltip"))} :

View File

@ -86,8 +86,13 @@ class SyncerListPage extends BaseListPage {
this.setState({loading: true});
SyncerBackend.runSyncer("admin", this.state.data[i].name)
.then((res) => {
if (res.status === "ok") {
this.setState({loading: false});
Setting.showMessage("success", "Syncer sync users successfully");
Setting.showMessage("success", i18next.t("general:Successfully synced"));
} else {
this.setState({loading: false});
Setting.showMessage("error", `${i18next.t("general:Failed to sync")}: ${res.msg}`);
}
}
)
.catch(error => {
@ -151,6 +156,13 @@ class SyncerListPage extends BaseListPage {
{text: "LDAP", value: "LDAP"},
],
},
{
title: i18next.t("syncer:Database type"),
dataIndex: "databaseType",
key: "databaseType",
width: "130px",
sorter: (a, b) => a.databaseType.localeCompare(b.databaseType),
},
{
title: i18next.t("provider:Host"),
dataIndex: "host",
@ -183,13 +195,6 @@ class SyncerListPage extends BaseListPage {
sorter: true,
...this.getColumnSearchProps("password"),
},
{
title: i18next.t("syncer:Database type"),
dataIndex: "databaseType",
key: "databaseType",
width: "120px",
sorter: (a, b) => a.databaseType.localeCompare(b.databaseType),
},
{
title: i18next.t("syncer:Database"),
dataIndex: "database",
@ -208,7 +213,7 @@ class SyncerListPage extends BaseListPage {
title: i18next.t("syncer:Sync interval"),
dataIndex: "syncInterval",
key: "syncInterval",
width: "130px",
width: "140px",
sorter: true,
...this.getColumnSearchProps("syncInterval"),
},

View File

@ -96,7 +96,7 @@ class SyncerTableColumnTable extends React.Component {
key: "casdoorName",
render: (text, record, index) => {
return (
<Select virtual={false} style={{width: "100%"}} value={text} onChange={(value => {this.updateField(table, index, "casdoorName", value);})}>
<Select virtual={false} showSearch style={{width: "100%"}} value={text} onChange={(value => {this.updateField(table, index, "casdoorName", value);})}>
{
["Name", "CreatedTime", "UpdatedTime", "Id", "Type", "Password", "PasswordSalt", "DisplayName", "FirstName", "LastName", "Avatar", "PermanentAvatar",
"Email", "EmailVerified", "Phone", "Location", "Address", "Affiliation", "Title", "IdCardType", "IdCard", "Homepage", "Bio", "Tag", "Region",