Skip to content

Add configurable option for event scheduling.#1131

Merged
MR2011 merged 1 commit intomainfrom
mikrzyz/configurable-schedule
Mar 12, 2026
Merged

Add configurable option for event scheduling.#1131
MR2011 merged 1 commit intomainfrom
mikrzyz/configurable-schedule

Conversation

@michalkrzyz
Copy link
Collaborator

Default time set to 200 minutes, User can change
it using DB_MV_CALC_PERIOD_MINUTES env variable

On-behalf-of: SAP Michal Krzyz michal.krzyz@sap.com

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

What type of PR is this? (check all applicable)

  • 🍕 Feature
  • 🐛 Bug Fix
  • 📝 Documentation Update
  • 🎨 Style
  • 🧑‍💻 Code Refactor
  • 🔥 Performance Improvements
  • ✅ Test
  • 🤖 Build
  • 🔁 CI
  • 📦 Chore (Release)
  • ⏩ Revert

Related Tickets & Documents

  • Related Issue # (issue)
  • Closes # (issue)
  • Fixes # (issue)

Remove if not applicable

Added tests?

  • 👍 yes
  • 🙅 no, because they aren't needed
  • 🙋 no, because I need help
  • Separate ticket for tests # (issue/pr)

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Added to documentation?

  • 📜 README.md
  • 🤝 Documentation pages updated
  • 🙅 no documentation needed
  • (if applicable) generated OpenAPI docs for CRD changes

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copilot AI review requested due to automatic review settings March 12, 2026 14:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes the materialized view refresh event scheduling configurable via the DB_MV_CALC_PERIOD_MINUTES environment variable. Previously, the events were created in the SQL migration file with a hardcoded EVERY 2 HOUR schedule. Now, the events are registered programmatically in Go code after migrations run, using the configured period (default: 200 minutes).

Changes:

  • Added DBMvCalcPeriodMinutes config field with a default of 200 minutes (previously hardcoded at 2 hours/120 minutes)
  • Moved event creation from the SQL migration file into a new registerEvents Go function in migration.go
  • Removed the static event creation SQL from the init migration

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
internal/util/config.go Added DBMvCalcPeriodMinutes config field for configurable event scheduling period
internal/database/mariadb/migrations/20260311144708_init.up.sql Removed hardcoded event creation SQL statements
internal/database/mariadb/migration.go Added registerEvents function to programmatically create scheduled events with configurable period

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

db, err := GetSqlxConnection(cfg)
if err != nil {
return fmt.Errorf("Error while Creating Db: %w", err)
}
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The database connection opened by GetSqlxConnection is never closed in this function. Compare with enableScheduler (line 121) which properly calls defer db.Close(). This will leak database connections each time registerEvents is called.

Suggested change
}
}
defer db.Close()

Copilot uses AI. Check for mistakes.
DBMaxIdleConnections int `envconfig:"DB_MAX_IDLE_CONNECTIONS" default:"10" json:"dBMaxIdleConnections"`
DBMaxOpenConnections int `envconfig:"DB_MAX_OPEN_CONNECTIONS" default:"100" json:"dbMaxOpenConnections"`
DBTrace bool `envconfig:"DB_TRACE" default:"false" json:"-"`
DBMvCalcPeriodMinutes int `envconfig:"DB_MV_CALC_PERIOD_MINUTES" default:"200"`
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing json struct tag on this field. All other fields in the Config struct have a json tag (either for serialization or json:"-" to exclude). This field should follow the same convention for consistency.

Suggested change
DBMvCalcPeriodMinutes int `envconfig:"DB_MV_CALC_PERIOD_MINUTES" default:"200"`
DBMvCalcPeriodMinutes int `envconfig:"DB_MV_CALC_PERIOD_MINUTES" default:"200" json:"dbMvCalcPeriodMinutes"`

Copilot uses AI. Check for mistakes.
Comment on lines +150 to +154
var query string
for _, e := range events {
query = fmt.Sprintf(
"%s DROP EVENT IF EXISTS %s; CREATE EVENT %s ON SCHEDULE EVERY %d MINUTE DO CALL %s_proc();",
query, e, e, cfg.DBMvCalcPeriodMinutes, e)
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no validation that cfg.DBMvCalcPeriodMinutes is a positive value. If a user sets DB_MV_CALC_PERIOD_MINUTES to 0 or a negative number, this would generate invalid SQL (ON SCHEDULE EVERY 0 MINUTE). Consider adding a validation check before using this value, or at minimum ensuring it falls back to the default if the value is not positive.

Suggested change
var query string
for _, e := range events {
query = fmt.Sprintf(
"%s DROP EVENT IF EXISTS %s; CREATE EVENT %s ON SCHEDULE EVERY %d MINUTE DO CALL %s_proc();",
query, e, e, cfg.DBMvCalcPeriodMinutes, e)
// Ensure the materialized view calculation period is a positive value.
mvPeriod := cfg.DBMvCalcPeriodMinutes
if mvPeriod <= 0 {
logrus.WithField("DBMvCalcPeriodMinutes", cfg.DBMvCalcPeriodMinutes).
Warn("invalid DBMvCalcPeriodMinutes configuration; falling back to 1 minute")
mvPeriod = 1
}
var query string
for _, e := range events {
query = fmt.Sprintf(
"%s DROP EVENT IF EXISTS %s; CREATE EVENT %s ON SCHEDULE EVERY %d MINUTE DO CALL %s_proc();",
query, e, e, mvPeriod, e)

Copilot uses AI. Check for mistakes.
@michalkrzyz michalkrzyz force-pushed the mikrzyz/configurable-schedule branch from 5863e95 to b48b28e Compare March 12, 2026 14:36
Default time set to 200 minutes, User can change
it using DB_MV_CALC_PERIOD_MINUTES env variable

On-behalf-of: SAP Michal Krzyz <michal.krzyz@sap.com>
Signed-off-by: Michal Krzyz <michalkrzyz@gmail.com>
@michalkrzyz michalkrzyz force-pushed the mikrzyz/configurable-schedule branch 2 times, most recently from c090b2c to 71aac57 Compare March 12, 2026 14:44
@MR2011 MR2011 merged commit 5db8602 into main Mar 12, 2026
9 checks passed
@MR2011 MR2011 deleted the mikrzyz/configurable-schedule branch March 12, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants