Skip to content
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

[Draft] Proof of concept for xloader site url #234

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions ckanext/xloader/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,18 @@ def xloader_submit(context, data_dict):
qualified=True
)
data = {
'api_key': utils.get_xloader_user_apitoken(),
'job_type': 'xloader_to_datastore',
'result_url': callback_url,
'metadata': {
'ignore_hash': data_dict.get('ignore_hash', False),
'ckan_url': config['ckan.site_url'],
'resource_id': res_id,
'set_url_type': data_dict.get('set_url_type', False),
'task_created': task['last_updated'],
'original_url': resource_dict.get('url'),
}
"api_key": utils.get_xloader_user_apitoken(),
"job_type": "xloader_to_datastore",
"result_url": callback_url,
"metadata": {
"ignore_hash": data_dict.get("ignore_hash", False),
"ckan_url": config.get("ckanext.xloader.site_url")
or config["ckan.site_url"],
"resource_id": res_id,
"set_url_type": data_dict.get("set_url_type", False),
"task_created": task["last_updated"],
"original_url": resource_dict.get("url"),
},
}
if custom_queue != rq_jobs.DEFAULT_QUEUE_NAME:
# Don't automatically retry if it's a custom run
Expand Down
11 changes: 5 additions & 6 deletions ckanext/xloader/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,11 @@ def _submit_resource(self, resource, user, indent=0, sync=False, queue=None):
'ignore_hash': True,
}
if sync:
data_dict['ckan_url'] = tk.config.get('ckan.site_url')
input_dict = {
'metadata': data_dict,
'api_key': 'TODO'
}
logger = logging.getLogger('ckanext.xloader.cli')
data_dict["ckan_url"] = tk.config.get(
"ckanext.xloader.site_url"
) or tk.config.get("ckan.site_url")
input_dict = {"metadata": data_dict, "api_key": "TODO"}
logger = logging.getLogger("ckanext.xloader.cli")
xloader_data_into_datastore_(input_dict, None, logger)
else:
if queue:
Expand Down
8 changes: 6 additions & 2 deletions ckanext/xloader/config_declaration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ version: 1
groups:
- annotation: ckanext-xloader settings
options:
- key: ckanext.xloader.site_url
default:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need an empty string default, it's normal for optional settings to be not present if not provided

Suggested change
default:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of the toolkit.config.get("ckanext.xloader.site_url") or toolkit.config.get("ckan.site_url") logic below we should be able to use just toolkit.config.get("ckanext.xloader.site_url") along with:

       validators: configured_default("ckan.site_url",None)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wardi ok I see. so that default is just set in the config yaml, and then, any code can assume that fallback default for the ckanext.xloader.site_url setting?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this validator will set ckanext.xloader.site_url to the same as ckan.site_url when it's not given. Another one of @smotornyuk 's clever ideas.

description: |
Provide an alternate site URL for the xloader_submit action.
This is useful, for example, when the site is running within a docker network.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is useful, for example, when the site is running within a docker network.
This is useful, for example, when the site is running within a docker network
or where the job runner can't access ckan using its normal public site URL.

required: false
- key: ckanext.xloader.jobs_db.uri
default: sqlite:////tmp/xloader_jobs.db
description: |
Expand Down Expand Up @@ -152,5 +158,3 @@ groups:
they will also display "complete", "active", "inactive", and "unknown".
type: bool
required: false


12 changes: 5 additions & 7 deletions ckanext/xloader/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ def configure(self, config_):
else:
self.ignore_hash = False

for config_option in ("ckan.site_url",):
if not config_.get(config_option):
raise Exception(
"Config option `{0}` must be set to use ckanext-xloader.".format(
config_option
)
)
site_url_configs = ("ckan.site_url", "ckanext.xloader.site_url")
if not any(site_url_configs):
raise Exception(
f"One of config options {site_url_configs} must be set to use ckanext-xloader."
)
Comment on lines +64 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ckan will refuse to start if a site_url isn't provided, so this code would never get executed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wardi here, I'm just extending the existing check for that. I prefer not to remove existing behavior, but just to provide the minimal new behavior required for the PR

https://github.com/ckan/ckanext-xloader/blob/master/ckanext/xloader/plugin.py#L64

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you did not cleanup the error message, this would not have been commented upon ;)

I'm fine leaving this in as its a belts and braces approach, better to fail early (if it does occur which is very very remote)


# IDomainObjectModification

Expand Down
23 changes: 12 additions & 11 deletions ckanext/xloader/tests/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,18 @@ def data(create_with_upload, apikey):
"api.action", ver=3, logic_function="xloader_hook", qualified=True
)
return {
'api_key': apikey,
'job_type': 'xloader_to_datastore',
'result_url': callback_url,
'metadata': {
'ignore_hash': True,
'ckan_url': toolkit.config.get('ckan.site_url'),
'resource_id': resource["id"],
'set_url_type': False,
'task_created': datetime.utcnow().isoformat(),
'original_url': resource["url"],
}
"api_key": apikey,
"job_type": "xloader_to_datastore",
"result_url": callback_url,
"metadata": {
"ignore_hash": True,
"ckan_url": toolkit.config.get("ckanext.xloader.site_url")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going to need more tests created to set these two values for proper validation.
one for default site url, another for the xloader override value. updating the fixture is so/so.

or toolkit.config.get("ckan.site_url"),
"resource_id": resource["id"],
"set_url_type": False,
"task_created": datetime.utcnow().isoformat(),
"original_url": resource["url"],
},
}


Expand Down