Skip to content

Commit

Permalink
remove unnecessary retries to get config using a loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishjh-bst committed Jun 28, 2024
1 parent 8b8ebbc commit 6c2cdf1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 52 deletions.
31 changes: 7 additions & 24 deletions moderation/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,33 +118,16 @@ func cacheKey(guildID int64) string {
}

func GetConfig(guildID int64) (*Config, error) {
const maxRetries = 1000

currentRetries := 0
for {
conf, err := models.FindModerationConfigG(context.Background(), guildID)
if err == nil {
if currentRetries > 1 {
logger.Info("Fetched config after ", currentRetries, " retries")
}
return configFromModel(conf), nil
}

if err == sql.ErrNoRows {
return nil, err
}

if strings.Contains(err.Error(), "sorry, too many clients already") {
time.Sleep(time.Millisecond * 10 * time.Duration(rand.Intn(10)))
currentRetries++
if currentRetries > maxRetries {
return nil, err
}
continue
}
conf, err := models.FindModerationConfigG(context.Background(), guildID)
if err == nil {
return configFromModel(conf), nil
}

if err == sql.ErrNoRows {
return nil, err
}

return nil, err
}

type ScheduledUnmuteData struct {
Expand Down
17 changes: 17 additions & 0 deletions notifications/assets/notifications_general.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ <h2>General</h2>
}
}

$(document).on('click', '.btn-add', function (e) {
e.preventDefault();

var currentEntry = $(this).parent().parent(),
newEntry = $(currentEntry.clone()).insertAfter(currentEntry);

newEntry.find('input, textarea').val('');
newEntry.parent().find('.entry:not(:last-of-type) .btn-add')
.removeClass('btn-add').addClass('btn-remove')
.removeClass('btn-success').addClass('btn-danger')
.html('<i class="fas fa-minus"></i>');
}).on('click', '.btn-remove', function (e) {
$(this).parents('.entry:first').remove();

e.preventDefault();
return false;
});
</script>

{{template "cp_footer" .}}
Expand Down
31 changes: 7 additions & 24 deletions notifications/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,16 @@ func cacheKey(guildID int64) string {
}

func GetConfig(guildID int64) (*Config, error) {
const maxRetries = 1000

currentRetries := 0
for {
conf, err := models.FindGeneralNotificationConfigG(context.Background(), guildID)
if err == nil {
if currentRetries > 1 {
logger.Info("Fetched config after ", currentRetries, " retries")
}
return configFromModel(conf), nil
}

if err == sql.ErrNoRows {
return nil, err
}

if strings.Contains(err.Error(), "sorry, too many clients already") {
time.Sleep(time.Millisecond * 10 * time.Duration(rand.Intn(10)))
currentRetries++
if currentRetries > maxRetries {
return nil, err
}
continue
}
conf, err := models.FindGeneralNotificationConfigG(context.Background(), guildID)
if err == nil {
return configFromModel(conf), nil
}

if err == sql.ErrNoRows {
return nil, err
}

return nil, err
}

func HandleGuildMemberAdd(evtData *eventsystem.EventData) (retry bool, err error) {
Expand Down
11 changes: 7 additions & 4 deletions notifications/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ CREATE TABLE IF NOT EXISTS general_notification_configs (
-- This column should be a TEXT[]. But for legacy reasons, it is instead a single
-- TEXT column containing all template responses joined together and delimited by
-- the character U+001E (INFORMATION SEPARATOR TWO.)
join_server_msgs TEXT,
join_dm_enabled BOOLEAN,
join_dm_msg TEXT,
leave_enabled BOOLEAN,
leave_channel TEXT,
-- Same deal as join_server_msgs.
leave_msgs TEXT,
topic_enabled BOOLEAN,
topic_channel TEXT,
Expand All @@ -42,6 +39,11 @@ CREATE TABLE IF NOT EXISTS general_notification_configs (
ALTER TABLE general_notification_configs ALTER COLUMN created_at SET NOT NULL;
`, `
ALTER TABLE general_notification_configs ALTER COLUMN updated_at SET NOT NULL;
`,
`
ALTER TABLE general_notification_configs ADD COLUMN IF NOT EXISTS join_server_msgs TEXT;
`, `
ALTER TABLE general_notification_configs ADD COLUMN IF NOT EXISTS leave_msgs TEXT;
`, `
-- Now the more complicated migration. For legacy reasons, the general_notification_configs
Expand All @@ -64,7 +66,8 @@ DO $$
BEGIN
-- only run if general_notifcation_configs.join_server_msg (indicative of legacy table) exists
IF EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name='general_notification_configs' AND column_name='join_server_msg') THEN
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='general_notification_configs' AND column_name='join_server_msgs')
AND EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name='general_notification_configs' AND column_name='join_server_msg') THEN
UPDATE general_notification_configs SET join_server_msgs_ = join_server_msg WHERE join_server_msg != '';
UPDATE general_notification_configs SET leave_msgs_ = leave_msg WHERE leave_msg != '';
Expand Down

0 comments on commit 6c2cdf1

Please sign in to comment.