-
Notifications
You must be signed in to change notification settings - Fork 1
Fix/18 auto continue on usage reset #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
irmorteza
wants to merge
3
commits into
Neokil:main
Choose a base branch
from
irmorteza:fix/18-auto-continue-on-usage-reset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| workflowstate "github.com/Neokil/AutoPR/internal/domain/workflowstate" | ||
| "github.com/Neokil/AutoPR/internal/markdown" | ||
| ) | ||
|
|
||
| func (s *server) persistTicketScheduled(repoID, repoRoot, ticket string, repoRt *repoRuntime, cause error) error { | ||
| if strings.TrimSpace(ticket) == "" { | ||
| return nil | ||
| } | ||
|
|
||
| ticketState, err := repoRt.store.LoadState(ticket) | ||
| if err != nil { | ||
| if !os.IsNotExist(err) { | ||
| return fmt.Errorf("load ticket state: %w", err) | ||
| } | ||
| ticketState = workflowstate.New(ticket) | ||
| } | ||
|
|
||
| msg := strings.TrimSpace(cause.Error()) | ||
| ticketState.FlowStatus = workflowstate.FlowStatusRescheduled | ||
| ticketState.LastError = msg | ||
| saveErr := repoRt.store.SaveState(ticket, ticketState) | ||
| if saveErr != nil { | ||
| return fmt.Errorf("save ticket state: %w", saveErr) | ||
| } | ||
|
|
||
| body := msg | ||
| if ticketState.WorktreePath != "" && ticketState.CurrentState != "" { | ||
| logPath := ticketState.CurrentRunLogPath() | ||
| _ = markdown.AppendSection(logPath, "Job Rescheduled — Quota Exhausted", body) | ||
| } | ||
|
|
||
| return s.syncTicketFromRepo(repoID, repoRoot, ticket, repoRt, true) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "log/slog" | ||
| "time" | ||
|
|
||
| "github.com/Neokil/AutoPR/internal/providers" | ||
| ) | ||
|
|
||
| const ( | ||
| quotaMonitorInterval = 20 * time.Minute | ||
| ) | ||
|
|
||
| func (s *server) quotaMonitorLoop() { | ||
| ticker := time.NewTicker(quotaMonitorInterval) | ||
|
|
||
| defer ticker.Stop() | ||
| for range ticker.C { | ||
| s.checkQuotaStatus() | ||
| } | ||
| } | ||
|
|
||
| func (s *server) isQuotaReached() bool { | ||
| s.quotaMu.RLock() | ||
| defer s.quotaMu.RUnlock() | ||
| return s.quotaReached | ||
| } | ||
|
|
||
| func (s *server) setQuotaReached(reached bool) { | ||
| s.quotaMu.Lock() | ||
| defer s.quotaMu.Unlock() | ||
| if reached && !s.quotaReached { | ||
| // Create a fresh channel that workers will block on | ||
| s.quotaResetCh = make(chan struct{}) | ||
| } | ||
| if !reached && s.quotaReached { | ||
| // Signal all waiting workers to wake up | ||
| close(s.quotaResetCh) | ||
| } | ||
| s.quotaReached = reached | ||
|
|
||
| } | ||
|
|
||
| func (s *server) checkQuotaStatus() { | ||
|
|
||
| if !s.isQuotaReached() { | ||
| return | ||
| } | ||
| slog.Info("quota monitor: probing provider to check if quota has reset") | ||
|
|
||
| repos := s.meta.ListRepos() | ||
| if len(repos) == 0 { | ||
| slog.Warn("quota monitor: no repos available for probe, skipping") | ||
| return | ||
| } | ||
| rt, err := s.runtimeForRepo(repos[0].Path) | ||
| if err != nil { | ||
| slog.Error("quota monitor: failed to get runtime for probe", "err", err) | ||
| return | ||
| } | ||
|
|
||
| probeErr := rt.svc.ProbeProvider(context.Background()) | ||
| if errors.Is(probeErr, providers.ErrTokensExhausted) { | ||
| slog.Info("quota monitor: quota still reached, will check again later") | ||
| return | ||
| } | ||
|
|
||
| s.setQuotaReached(false) | ||
| slog.Info("quota monitor: quota has reset, resuming operations") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is already handled inside of the
StartFlow -> runStateorMoveToState -> transitionTo -> runStateso the ticket is updated twice