-
Notifications
You must be signed in to change notification settings - Fork 54
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
base: master
Are you sure you want to change the base?
Changes from all commits
43203b8
da5c031
93c9e5f
deab5cb
729d5b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,14 @@ version: 1 | |||||||
groups: | ||||||||
- annotation: ckanext-xloader settings | ||||||||
options: | ||||||||
- key: ckanext.xloader.site_url | ||||||||
example: http://ckan-dev:5000 | ||||||||
default: | ||||||||
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. | ||||||||
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.
Suggested change
|
||||||||
validators: configured_default("ckan.site_url",None) | ||||||||
required: false | ||||||||
- key: ckanext.xloader.jobs_db.uri | ||||||||
default: sqlite:////tmp/xloader_jobs.db | ||||||||
description: | | ||||||||
|
@@ -152,5 +160,3 @@ groups: | |||||||
they will also display "complete", "active", "inactive", and "unknown". | ||||||||
type: bool | ||||||||
required: false | ||||||||
|
||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,14 @@ | |
from rq import get_current_job | ||
import sqlalchemy as sa | ||
|
||
from urllib.parse import urljoin, urlunsplit | ||
|
||
from ckan import model | ||
from ckan.plugins.toolkit import get_action, asbool, enqueue_job, ObjectNotFound, config | ||
|
||
from . import db, loader | ||
from .job_exceptions import JobError, HTTPError, DataTooBigError, FileCouldNotBeLoadedError | ||
from .utils import datastore_resource_exists, set_resource_metadata | ||
from .utils import datastore_resource_exists, set_resource_metadata, get_ckan_url | ||
|
||
try: | ||
from ckan.lib.api_token import get_user_from_token | ||
|
@@ -79,9 +81,14 @@ def xloader_data_into_datastore(input): | |
# First flag that this task is running, to indicate the job is not | ||
# stillborn, for when xloader_submit is deciding whether another job would | ||
# be a duplicate or not | ||
|
||
callback_url = get_ckan_url() | ||
callback_url = urljoin( | ||
callback_url.rstrip('/'), '/api/3/action/xloader_hook') | ||
|
||
job_dict = dict(metadata=input['metadata'], | ||
status='running') | ||
callback_xloader_hook(result_url=input['result_url'], | ||
callback_xloader_hook(result_url=callback_url, | ||
api_key=input['api_key'], | ||
job_dict=job_dict) | ||
|
||
|
@@ -143,7 +150,7 @@ def xloader_data_into_datastore(input): | |
errored = True | ||
finally: | ||
# job_dict is defined in xloader_hook's docstring | ||
is_saved_ok = callback_xloader_hook(result_url=input['result_url'], | ||
is_saved_ok = callback_xloader_hook(result_url=callback_url, | ||
api_key=input['api_key'], | ||
job_dict=job_dict) | ||
errored = errored or not is_saved_ok | ||
|
@@ -204,7 +211,10 @@ def direct_load(): | |
set_datastore_active(data, resource, logger) | ||
if 'result_url' in input: | ||
job_dict['status'] = 'running_but_viewable' | ||
callback_xloader_hook(result_url=input['result_url'], | ||
callback_url = get_ckan_url() | ||
callback_url = urljoin( | ||
callback_url.rstrip('/'), '/api/3/action/xloader_hook') | ||
callback_xloader_hook(result_url=callback_url, | ||
api_key=api_key, | ||
job_dict=job_dict) | ||
logger.info('Data now available to users: %s', resource_ckan_url) | ||
|
@@ -285,6 +295,13 @@ def _download_resource_data(resource, data, api_key, logger): | |
'Only http, https, and ftp resources may be fetched.' | ||
) | ||
|
||
resource_uri = urlunsplit(('', '', url_parts.path, url_parts.query, url_parts.fragment)) | ||
callback_url = get_ckan_url() | ||
url = urljoin( | ||
callback_url.rstrip('/'), resource_uri) | ||
|
||
url_parts = urlsplit(url) # reparse the url after the callback_url is set | ||
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. This will break all 'external' url's which are not internally referenced. We should be using a 'generic' function that takes a url and does the swap if the original url. Details below on my reasoning: I see in my jobs stats table items is
using this as an example. The input of the _download_resource_data function is looking at : https://www.data.qld.gov.au/api/action/resource_show?id=5d1e8368-7ec3-435a-92d0-280ad1e3db0d on line 292, it gets the url which would be: Which since you are 'force' changing the url, instead of checking its pointing to the CKAN site url first. it would then end up with http://localhost:5000/data-sets/water/gregory-environmental.csv?timestamp=2025-01-16T10:15:01+10:00 which would then be a 404 or hanging situation. 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. Thanks @duttonw for taking the time to help me with this. It is much appreaciated. Yes, I agree that a util/helper function is needed to swap out the CKAN URL With regards to your scenario, why would you set Update: actually if 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. Hi @kowh-ai , http://www.data.qld.gov.au/data-sets/water/gregory-environmental.csv?timestamp=2025-01-16T10:15:01+10:00 is a 404, since its not on www.data.qld.gov.au, but on apps.des.qld.gov.au (non ckan endpoint) With the new function This will still need at least another 5 unit tests for this new function to 'verify' that it does what we expect it to do. i.e. not break external data assets. somethings i've been thinking for a while is that if you have the 'archiver' plugin installed, like how there is a use case to 'wait' for the validation plugin to complete successfully. If the archiver has 'saved' the resource locally, then we could/should use that ingest into the datastore. The only negative is like these external csv files which are updated adhoc and the 'manual datastore upload' won't collect the latest like it does now. (harvester normally is set up to run on initial upload and then can be setup to run adhoc { for our instance monthly } , this is too slow and will cause frustration for authors who wish to have the latest from an external csv ) 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. This whole section should probably be moved down ten lines or so, to the block where 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. @duttonw - What about the other callbacks to CKAN ie: when these 2 functions call
The In your case could the |
||
|
||
# fetch the resource data | ||
logger.info('Fetching from: {0}'.format(url)) | ||
tmp_file = get_tmp_file(url) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
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. ckan will refuse to start if a site_url isn't provided, so this code would never get executed 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. @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 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. 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 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
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. please also add a test for 'external' url which is not using the site_url for the file. |
||
"job_type": "xloader_to_datastore", | ||
"result_url": callback_url, | ||
"metadata": { | ||
"ignore_hash": True, | ||
"ckan_url": toolkit.config.get("ckanext.xloader.site_url") | ||
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. going to need more tests created to set these two values for proper validation. |
||
or toolkit.config.get("ckan.site_url"), | ||
"resource_id": resource["id"], | ||
"set_url_type": False, | ||
"task_created": datetime.utcnow().isoformat(), | ||
"original_url": resource["url"], | ||
}, | ||
} | ||
|
||
|
||
|
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 we don't need an empty string default, it's normal for optional settings to be not present if not provided
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.
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 justtoolkit.config.get("ckanext.xloader.site_url")
along with: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.
@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?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.
yeah, this validator will set
ckanext.xloader.site_url
to the same asckan.site_url
when it's not given. Another one of @smotornyuk 's clever ideas.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.
Do we need to bother with that when
jobs.py
already has the fallbackconfig.get('ckanext.xloader.site_url') or config.get('ckan.site_url')
?IMO we should keep the fallback in
jobs.py
in case someone manually setsckanext.xloader.site_url
to a blank value (which could happen if eg the config is being automatically generated/populated from somewhere, perhaps usingckanext-ssm-config
), so the validator approach is redundant.