fix: monitor timeout task SQL#617
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a significant performance bottleneck identified in the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a performance issue by adding a database index and modifying the query to filter timed-out tasks at the database level. The changes are logical and well-implemented. I've included a couple of suggestions to further improve the code by using more idiomatic GORM features, which will enhance maintainability and readability.
| err := DifyPluginDB.Exec( | ||
| "CREATE INDEX " + installTasksStatusCreatedAtIndexName + " ON install_tasks (status, created_at)", | ||
| ).Error |
There was a problem hiding this comment.
To improve database portability and avoid raw SQL, it's better to use GORM's CreateIndex method. This will also make the code cleaner and less prone to typos in table or column names. GORM will correctly generate the index name idx_install_tasks_status_created_at for the composite index on the status and created_at columns.
err := DifyPluginDB.Migrator().CreateIndex(&models.InstallTask{}, "status", "created_at")| tasks, err := db.GetAll[models.InstallTask]( | ||
| db.InArray("status", []any{ | ||
| string(models.InstallTaskStatusPending), | ||
| string(models.InstallTaskStatusRunning), | ||
| }), | ||
| db.WhereSQL("created_at < ?", deadline), | ||
| ) | ||
| if err != nil { | ||
| log.Error("failed to get all tasks", "error", err) | ||
| continue | ||
| } | ||
| tasksToProcess := make([]*models.InstallTask, 0, len(tasks)) | ||
| for i := range tasks { | ||
| task := &tasks[i] | ||
| if time.Since(task.CreatedAt) > timeout { | ||
| log.Info("task timed out", "task_id", task.ID) | ||
| tasksToProcess = append(tasksToProcess, task) | ||
| } | ||
| log.Info("task timed out", "task_id", task.ID) | ||
| tasksToProcess = append(tasksToProcess, task) | ||
| } | ||
| markTasksAsTimeout(tasksToProcess) |
There was a problem hiding this comment.
This block can be simplified by fetching a slice of pointers directly from the database. By changing db.GetAll[models.InstallTask] to db.GetAll[*models.InstallTask], you can avoid creating and populating the tasksToProcess slice. This makes the code cleaner and slightly more efficient.
tasks, err := db.GetAll[*models.InstallTask](
db.InArray("status", []any{
string(models.InstallTaskStatusPending),
string(models.InstallTaskStatusRunning),
}),
db.WhereSQL("created_at < ?", deadline),
)
if err != nil {
log.Error("failed to get all tasks", "error", err)
continue
}
for _, task := range tasks {
log.Info("task timed out", "task_id", task.ID)
}
markTasksAsTimeout(tasks)954937a to
3a94422
Compare
Description
internal/tasks/recycle.go:86func MonitorTimeoutTasksexecutes such query:SELECT * FROM "install_tasks" WHERE status IN ($1,$2)that produces significant performance issues on DB.Dify SaaS fixes this by adding index
idx_install_tasks_status ON install_tasks (status)This PR adds this index to
internal/types/models/task.go, allowing community instances to have this optimization.Type of Change
Essential Checklist
Testing