fix(scrapy): stop the crawl gracefully when the Actor run is migrated or aborted - #1104
Draft
vdusek wants to merge 8 commits into
Draft
fix(scrapy): stop the crawl gracefully when the Actor run is migrated or aborted#1104vdusek wants to merge 8 commits into
vdusek wants to merge 8 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1104 +/- ##
==========================================
+ Coverage 92.65% 92.82% +0.16%
==========================================
Files 51 52 +1
Lines 3445 3510 +65
==========================================
+ Hits 3192 3258 +66
+ Misses 253 252 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…plicate start page
…whether they were registered
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #1103.
The Scrapy integration ignored the
MIGRATINGandABORTINGevents, so the process was killed with requests in flight. Those stay pending and get downloaded again by the next run, and every item their callbacks had already pushed lands in the dataset twice.The issue proposed
crawler.stop_async()for both events, but that is wrong for a migration: the platform finishes a run whose container exits with code 0 as SUCCEEDED and doesn't restart it (apify-workeract2_run_job.ts, only a non-zero exit withwasMigrationInitiatedre-schedules the run). A crawl that stopped onMIGRATINGwould end the run as successful with work still pending. A graceful abort is different: the run is already DONE whenabortingis emitted, so exiting with 0 yields ABORTED.Migration
ApifySchedulerlistens forMIGRATING. From then onnext_request()hands out nothing, and the requests Scrapy is still working on are marked as handled as they finish. The crawl stays alive and idle until the platform kills it, so the next run continues with the pending requests instead of downloading the finished ones again.Actor.reboot()dispatches the same listener and awaits it, so a reboot now waits for the in-flight requests to settle first.Abort
The new
ApifyGracefulStopExtension, registered byapply_apify_settings, callscrawler.stop_async()onABORTING: the in-flight requests finish, the scheduler marks them as handled, and the run ends as ABORTED within the grace period.Docs and tests
The migration section of the Scrapy guide describes the behavior and the start-URL caveat: the default
Spider.start()yields withdont_filter=True(mapped toalways_enqueue=True), so a restarted run crawls the start URLs again. The example spider overridesstart()with plain requests. As a side effect the start page now deduplicates against the same link found on subpages, so the title-spider e2e test asserts unique items instead of a count that included that duplicate.Two e2e tests exercise the paths on the platform: a graceful abort with a request in flight (run ABORTED, spider closed gracefully, dataset items match the handled requests, the rest of the chain stays pending) and an
Actor.reboot()mid-crawl (the in-flight request is settled before the reboot, the rebooted run finishes the chain with no duplicates).✍️ Drafted by Claude Code