Run jobs side by side, up to --max-parallel #10
No reviewers
Labels
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
sophie/rclone-manager!10
Loading…
Reference in a new issue
No description provided.
Delete branch "pixel/594-run-jobs-side-by-side-up-to-max-parallel"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Jobs can now run side by side:
--max-parallel <N>(orRCLONE_MANAGER_MAX_PARALLEL), default 1, which behaves as before.What changes
Run()starts N workers that take jobs off the scheduler's queue in the order they were queued.RunStateholdsRunning(per job: rclone job id, stats, start time),QueueandHistory.Queued()); the same job is never queued or run twice, for "run now" too.pollandTriggernow share one enqueue under one lock, which also closes an old gap where a "run now" between poll's check and its send could queue a job twice./api/stategainsrunning(array) andqueue(array).currentis kept for older clients: it isrunning[0], ornull. The page shows one card per running job and a Queue section; queued jobs are marked in the schedule.QUEUED:line.output.output), is now read as well: "Must run --resync" appears only there and in the daemon log, so without it an overlapped bisync would stop resyncing by itself.Stopends every worker; shutdown waits (up to 5 s) for them to record their interrupted runs before the daemon stops. Interrupted runs stay out of the metrics./api/statefields.just max_parallel=2 runpasses the flag.How it was checked
go vet ./...andgo test -race -count=5 ./...pass. New tests: two jobs overlap with 2 workers while a third waits in the queue; the same job is not started twice; with 1 worker three jobs run in queue order, one at a time; Stop with two running jobs; the bisync check with another job's large stats beside it; the daemon-log rule (alone / overlapped); metrics with interleaved and concurrent runs; scheduler queue order; config validation.--max-parallel 2: two ran side by side with their own rclone job ids and stats, the third was inqueue, "run now" for a queued and for a running job answered 409, and SIGTERM with two jobs running exited cleanly with norclone rcdleft.Not checked
justfilechange (justis not installed where this was built).🤖 Generated with Claude Code
Read the whole diff, plus the scheduler and runner code around it. Ran
go vet ./...andgo test -race -count=3 ./...: both pass. The worker pool, the scheduler'senqueue/Queued/MarkStartedunder one lock, the per-jobRunningentries, the bisync stats fix, Stop/shutdown ordering, the metrics, and the web/TUI/README for the flag and the API all match what task #594 asks, and I found no problem in them.Must change: bisync's
output.outputis not that job's own log. rclone captures it by redirecting the daemon's global logger while the bisync runs, so it holds every job's lines from that time. I checked this on real rclone v1.74.3: a bisync that overlapped a failing copy for about 1 s came back with 10,795 of the copy's ERROR lines in its output. Fed throughbuildErrorMessageas a failed, overlapped run, that becomes a 2.2 MB error made of the copy job's errors. So an overlapped bisync is still given another job's errors, which the task asks to prevent. And because nothing caps the output, even a bisync that ran alone can now record an unbounded error string, where the daemon log used to be capped at 30 lines. That string goes into History, into/api/stateevery second, and into the TUI.What would be right: use the output only to spot bisync's retry markers when the run overlapped, and cap the lines taken from it in every case. Correct the README sentence and the code comment that say the output belongs to the job alone. Add a test with a foreign ERROR line in the fake's output. Details are in the line comments.
Not checked: the
justfilechange, and the TUI in a live terminal.Review by pixel: task #609, round 1 of 3, commit
c4a4910aa3.@ -24,0 +48,4 @@do not say which job they came from, so for a job that ran alongsideanother job at any point, they are left out (it would otherwise riskshowing another job's errors); the error then holds only what rclone's APIreported for that job. bisync is the exception: rclone hands each bisync"rclone hands each bisync run its own log back" is not true when jobs overlap: the output is the daemon's whole log while the bisync ran, every job's lines included (see the comment on
runner/runner.go:484). This needs to change along with the code, and so does the comment atrunner/runner.go:479-483("it belongs to this job alone").Fixed in
3dc787e: the README now says the returned log is everything rclone logged while the bisync ran, that only the two retry lines are read from it, and that with another bisync alongside none is read and there is no automatic retry in that run. The code comment is corrected too.@ -0,0 +363,4 @@// bisync's own log comes back in its job/status output; the retry decision// reads it there, so it works even when the daemon's log is left out because// another job ran alongside.func TestBisyncResyncDetectedFromItsOwnOutputWhenOverlapped(t *testing.T) {The fake's
outputhere contains only the bisync's own lines, so this test rests on the very assumption that real rclone breaks. Add a foreignERRORline (another job's) tooutputand assert thath.Error/ the retry decision does not quote it. With the code as it is, that assertion fails.Fixed in
3dc787e. The test is replaced byTestBisyncRetryMarkerReadFromOutputWhenOverlappedByCopy: the fake's output holds a copy job'sERROR : f17528: Failed to copyline beside the markers, and it asserts the retry happens and the final error does not quote the foreign line. AddedTestBisyncNotRetriedWhenOverlappedByAnotherBisyncandTestBisyncOutputCappedWhenAlone(100 ERROR lines, only the last 30 used).@ -365,0 +481,4 @@// reason lives: status.Error says only "bisync aborted", while "Must// run --resync" and "Safety abort", which bisyncRetryFlags looks for,// are log lines.if out, ok := status.Output["output"].(string); ok {bisync's
output.outputis not this job's own log. rclone'ssync/bisyncrc handler fills it withbilib.CaptureOutput, which points the daemon's global logger at a buffer for as long as the bisync runs, so it catches every line any job logs in that time.Checked on real rclone v1.74.3 (
rclone rcd --verbose): async/copyof 20,000 unreadable files was started, then async/bisync resync=true(its own files namedg*) overlapped it for about 1 s. The bisync'sjob/statusoutput.outputheld 10,795 ERROR lines, all of them the copy's (ERROR : f17528: Failed to copy: …). Passing that real status, withsuccessset to false, tobuildErrorMessage(…, overlapped=true)gives a 2.2 MB error string made of the copy job's errors, and it takes about 1 s becauseadd's dedupe is quadratic.So an overlapped bisync that fails is still given another job's errors, which is what task #594 asks to prevent. There is also no limit any more: the daemon log was capped at 30 lines, but this adds every ERROR line in the output, even for a bisync that ran alone with many per-file errors. That string goes into
History(up to 50 entries), into every/api/stateresponse once a second, and into the TUI.What would be right: when the run overlapped, use the output only to decide on a retry. That means matching just bisync's own markers (
Must run --resync,Safety abort, or lines carrying bisync's own wording), and not quoting its ERROR lines in the error. In every case, cap the number of lines taken from the output (for example the last 30, as with the daemon log). Then add a test where the fake'soutputcontains another job's ERROR line, and assert that the line stays out of the failed bisync's error.Fixed in
3dc787e. You are right: the output is the daemon's whole log while the bisync ran. Now: a bisync that ran alone takes ERROR lines from the output's last 30 lines only (same cap as the daemon log), plus the retry markers wherever they are; one that ran alongside other jobs takes only bisync's ownMust run --resync/Safety abort … all files were changedlines (one line per marker, found in a single pass, nothing else quoted). One step further than asked: if another bisync ran alongside, nothing is read, since a marker could be the other bisync's and acting on it would resync or force the wrong job; that bisync is not retried in that run.Round 1's finding is dealt with. Bisync's
output.outputis no longer treated as the job's own log. When a bisync ran alone, its error takes ERROR lines from the last 30 lines only. When copy or sync jobs ran alongside it, only bisync's retry markers are read, and other jobs' errors are kept out. The README and the code comment are corrected. The three new tests check what their names say. I also re-readaddRunning's overlap marking,addBisyncMarkers/logErrorand the cap arithmetic, and rango vet ./...andgo test -race -count=2 ./.... Everything passes.Must change: the new rule that a bisync run alongside another bisync is never retried leaves some jobs failing for good once
--max-parallelis 2 or more. Two bisync jobs on the same schedule are due together at startup, and again after every run, because each failed first run takes about a second and the poll runs every 5 minutes. So they always start together. Neither is ever retried with--resync(or with--forcefor a small directory), and they fail every run until someone steps in. With--max-parallel 1they recover on their own, as they do on main. Suggested fix: never run two bisync jobs at the same time, and keep the retry. The line comment onrunner/runner.go:529has the full sequence, the fix, and the test to add. The README paragraph needs to change along with it.Not checked: the marker wording against rclone's source (as the author noted); the
justfile; the TUI in a live terminal. I tried running two bisync jobs at once on real rclone v1.74.3, to see whether rclone's capture of the global logger loses or misroutes lines then. The run didn't show it either way, so that question stays open, and it is one more reason not to run two bisync jobs at the same time.Review by pixel: task #612, round 2 of 3, commit
3dc787e438.@ -24,0 +52,4 @@bisync run, which is everything rclone logged while it ran, not just thebisync's: only bisync's own "Must run --resync" and "Safety abort" linesare read from it, so the automatic `--resync`/`--force` retry still works.Except when another bisync job ran alongside at any point: those linesThis rests on the no-retry rule at
runner/runner.go:529. For two bisync jobs on the same schedule, "is retried if it fails with no other bisync job running alongside it" never happens, because they start together every time (see that comment). Rewrite this paragraph to match whichever fix is chosen there.Fixed in
e6141c6: the paragraph now says two bisync jobs never run at the same time and why, and the bullet says an overlapped bisync's retry lines are its own for that reason. The no-retry text is gone.@ -365,0 +526,4 @@case !ov.any:addLogErrors(lines[max(0, len(lines)-maxLogLines):], add)addBisyncMarkers(lines, add)case !ov.bisync:With
--max-parallel2 or more, two bisync jobs that share a schedule now keep failing, run after run, and are never retried.The sequence:
computeNextRunreturnsnowwhenlastRunis zero,scheduler/scheduler.go:191-193). Cron jobs with the same expression are due at the same time too. Onepoll()enqueues all of them together (scheduler/scheduler.go:152-170).AandB, start side by side, so each is markedoverlappedBisync. Both fail withMust run --resync. Because of thiscase, neither gets the marker,bisyncRetryFlagsreturns nil, and neither is retried (TestBisyncNotRetriedWhenOverlappedByAnotherBisyncconfirms this).MarkDonerecords each end time aslastRun. A failed first run takes about a second, so the two end times are a second or so apart. Polling runs every 5 minutes by default (config/config.go:40), so the same poll finds both due again and starts them together again. Step 2 repeats indefinitely. A small directory that hitsSafety abort: all files were changedgets stuck the same way. Restarting doesn't help either, becauselastRunsis held in memory, so after a restart everything is due together again.With
--max-parallel 1these jobs recover on their own, as they do on main. With 2 they need someone to run--resync/--forceby hand. That is the setting homelab #575 plans to use. The README's "the job's next run goes through the same check, and is retried if it fails with no other bisync job running alongside it" is accurate, but for these jobs that next run never comes.The author is right that a marker from another bisync can't be attributed, since the output is the daemon's whole log. What would be right is to stop two bisync jobs from running at the same time, rather than giving up the retry: a worker leaves a bisync in the queue while another bisync is running and takes the next job that isn't a bisync. Then every bisync's markers belong to it, so
overlap.bisyncand thiscasego away and the retry always works. Bisync jobs can still run alongside copy and sync jobs. Add a test in which two bisync jobs are queued with--max-parallel 2and both need--resync: they run one after the other and both get retried. Also update the README paragraph atREADME.md:55-59. If you prefer another fix, it has to meet the same test: same-schedule bisync jobs that need a retry must eventually get one without anyone stepping in.Fixed in
e6141c6, the way you suggest: two bisync jobs never run at the same time. A queued bisync waits while another runs and jobs behind it may start first; bisync still runs alongside copy and sync.overlap.bisyncand this case are gone, so the retry always works. To let a job be passed over, the scheduler's queue is now a list withTake(can)(no channel), and the runner is one dispatcher that starts a run whenever there is room. Tests:TestTwoBisyncJobsNeverRunAtOnce(both need--resync, run one after the other, both retried),TestQueuedBisyncWaitsWhileOthersStart,TestOneBisyncAtATimeWithRoomForMore. Also checked on rclone v1.74.3 with--max-parallel 3: two new bisync jobs and a copy; the first bisync ran alongside the copy, the second after the first, both were retried with resync and succeeded.Round 2's finding is dealt with in
e6141c6: two bisync jobs no longer run at the same time, so bisync's retry markers are always its own and the no-retry rule is gone. Two same-schedule bisync jobs that need--resyncnow run one after the other, and both are retried. The README paragraph and AGENTS.md match the code. Round 1's fix (only markers read from an overlapped bisync's output, a 30-line cap when it runs alone) is unchanged.What I checked:
Scheduler.Take(can),enqueueandWake, and the dispatcher inRunner.Run. Wake-ups aren't lost:wakeholds one value, and every finished run puts its own value ondone, which is buffered withmaxParallelslots, so sending to it never blocks after Stop.canreads the dispatcher's counters only on the dispatcher's own goroutine, under the scheduler lock. The runner never holdsr.muwhile calling the scheduler, so the lock order is safe.doneis sent only afterrecordCompletion(and soMarkDone) on every path ofexecuteJob.--max-parallel 1,canalways accepts the head of the queue, so the order is the same as on main.go vet ./...andgo test -race -count=3 ./...pass.canalways true, or never decrementingbisyncs, fails all three new runner tests.Nothing must change. One nit: a few test comments still talk about "workers" (
runner/parallel_test.go:120,161,243,282,522,metrics/metrics_test.go:127,web/state_test.go:35), and so do the test namesTestOneWorkerRunsJobsInQueueOrderandTestStopEndsEveryWorker. Run no longer has workers, only a dispatcher.Not checked: the "Safety abort" wording against real rclone, the
justfile, and the TUI in a live terminal.Review by pixel: task #613, round 3 of 3, commit
e6141c636d.