-
Notifications
You must be signed in to change notification settings - Fork 80
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
[For discussion] Add args from #392
Open
PyExplorer
wants to merge
3
commits into
scrapinghub:master
Choose a base branch
from
PyExplorer:add_agrs_from
base: master
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
This file contains 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 | ||||
---|---|---|---|---|---|---|
|
@@ -34,6 +34,13 @@ | |||||
Similarly, job-specific settings can be supplied through the -s option: | ||||||
|
||||||
shub schedule myspider -s SETTING=VALUE -s LOG_LEVEL=DEBUG | ||||||
|
||||||
Also, the spider can be run with all arguments are taken from another job with -f option: | ||||||
|
||||||
shub schedule myspider -f 123/21/134 | ||||||
|
||||||
But arguments with -a will replace these arguments | ||||||
|
||||||
""" | ||||||
|
||||||
SHORT_HELP = "Schedule a spider to run on Scrapy Cloud" | ||||||
|
@@ -54,15 +61,17 @@ | |||||
help='Amount of Scrapy Cloud units (-u number)') | ||||||
@click.option('-t', '--tag', | ||||||
help='Job tags (-t tag)', multiple=True) | ||||||
def cli(spider, argument, set, environment, priority, units, tag): | ||||||
@click.option('-f', '--args_from', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency I'd suggest to use a hyphen, it'll be converted to
Suggested change
|
||||||
help='project/spider/job for copying arguments (-f 123/321/456)') | ||||||
def cli(spider, argument, set, environment, priority, units, tag, args_from): | ||||||
try: | ||||||
target, spider = spider.rsplit('/', 1) | ||||||
except ValueError: | ||||||
target = 'default' | ||||||
targetconf = get_target_conf(target) | ||||||
job_key = schedule_spider(targetconf.project_id, targetconf.endpoint, | ||||||
targetconf.apikey, spider, argument, set, | ||||||
priority, units, tag, environment) | ||||||
priority, units, tag, environment, args_from) | ||||||
watch_url = urljoin( | ||||||
targetconf.endpoint, | ||||||
'../p/{}/{}/{}'.format(*job_key.split('/')), | ||||||
|
@@ -78,11 +87,13 @@ def cli(spider, argument, set, environment, priority, units, tag): | |||||
|
||||||
|
||||||
def schedule_spider(project, endpoint, apikey, spider, arguments=(), settings=(), | ||||||
priority=DEFAULT_PRIORITY, units=None, tag=(), environment=()): | ||||||
priority=DEFAULT_PRIORITY, units=None, tag=(), environment=(), | ||||||
args_from=None): | ||||||
client = ScrapinghubClient(apikey, dash_endpoint=endpoint) | ||||||
try: | ||||||
project = client.get_project(project) | ||||||
args = dict(x.split('=', 1) for x in arguments) | ||||||
args = add_args_from_job(client, args, args_from) | ||||||
cmd_args = args.pop('cmd_args', None) | ||||||
meta = args.pop('meta', None) | ||||||
job = project.jobs.run( | ||||||
|
@@ -99,3 +110,14 @@ def schedule_spider(project, endpoint, apikey, spider, arguments=(), settings=() | |||||
return job.key | ||||||
except ScrapinghubAPIError as e: | ||||||
raise RemoteErrorException(str(e)) | ||||||
|
||||||
def add_args_from_job(client, base_args, args_from): | ||||||
if not args_from: | ||||||
return base_args | ||||||
job_args = get_args_from_parent_job(client, args_from).copy() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use |
||||||
job_args.update(base_args) | ||||||
return job_args | ||||||
|
||||||
def get_args_from_parent_job(client, args_from): | ||||||
job = client.get_job(args_from) | ||||||
return job.metadata.get("spider_args") or {} |
This file contains 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.
Should we extend the logic to optionally inherit job-level settings as well (with a different option ofc)? Could be done separately, here I'm more worried about consistent API, say we reserve
-f
/--args-from
for arguments, and-s
/--settings-from
for settings. Wdyt?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.
I think this makes totally sense, thank you :). For settings we already have "-s", we can use "-g/--settings-from" for example.
Apart from the settings, there are also parameters that we can take from the "parent" job
--environment (-n, --env-from)
--priority
--units
--tag
Not sure about the priority, units, tag, but what do you think about the environment?
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.
Tbh the short flag
-g
here-g/--settings-from
looks a bit misleading 🤔 Also inheriting different parameters from different jobs (say get tags from job A, get settings from job B etc) doesn't look useful in real life, you'd usually inherit parameters from a single job.My suggestion here would be
-i
/--inherit-from
--inherit-args
,--inherit-settings
,--inherit-environment
,--inherit-tags
--inherit-from
without specifiying what exactly to inherit should fail with a warningYour thoughts?
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.
Although the argument's names are a bit long (--inherit-environment) but clear and no problem to use this I hope. And the approach with --inherit argument looks good and quite agile.
About
using --inherit-from without specifiying what exactly to inherit should fail with a warning
- what do you think if instead of raising a warning it will inherit all parameters (args, settings, environment, tags, priority, units) at once? In most cases, we need exactly this behavior. Or just add a new argument like to --inherit-all (in addition to --inherit-args, etc).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.
Looks good to me, let's go this way 👍🏻