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

[WIP] Use the datapusher #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 43 additions & 9 deletions ckanext/datastorer/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class Datastorer(CkanCommand):

Usage:

paster datastorer [update|queue] [package-id]
paster datastorer [update|queue|push] [package-id]
- Update all resources or just those belonging to a specific
package if a package id is provided. Use 'update' to update
the resource synchronously and log to stdout, or 'queue'
to queue the update to run asynchronously in celery
(output goes to celery's logs).
(output goes to celery's logs). Use 'push' to use the datapusher.

"""
summary = __doc__.split('\n')[0]
Expand Down Expand Up @@ -84,7 +84,7 @@ def command(self):
raise Exception('You have to set the "ckan.site_url" property in your ini file.')
api_url = urlparse.urljoin(config['ckan.site_url'], 'api/action')

if cmd in ('update', 'queue'):
if cmd in ('update', 'queue', 'push'):
headers = {
'content-type:': 'application/json'
}
Expand Down Expand Up @@ -124,6 +124,11 @@ def command(self):
'package %s' % (resource['url'],
package['name']))

datastorer_task_context = {
'model': model,
'user': user.get('name')
}

if cmd == "update":
logger.setLevel(0)
tasks._datastorer_upload(context, resource, logger)
Expand All @@ -137,16 +142,45 @@ def command(self):
'value': task_id,
'last_updated': datetime.now().isoformat()
}
datastorer_task_context = {
'model': model,
'user': user.get('name')
}

get_action('task_status_update')(datastorer_task_context,
datastorer_task_status)
celery.send_task("datastorer.upload",
args=[json.dumps(context), data],
task_id=task_id)
args=[json.dumps(context), data],
task_id=task_id)
elif cmd == "push":
data_dict = {
'api_key': context['apikey'],
'job_type': 'push_to_datastore',
#'result_url': 'https://www.ckan.org/datapusher/callback',
'metadata': {
'ckan_url': context['site_url'],
'resource_id': resource['id']
}
}
url = urlparse.urljoin(config['datapusher.url'], '/job')
r = requests.post(url,
data=json.dumps(data_dict),
headers=headers)
if r.status_code not in (200, 201):
raise Exception('Error when pushing to the datapusher. Message: %s' % r.content)
response = r.json()
job_data = {
'job_id': response['job_id'],
'job_key': response['job_key']
}
logger.warn('Submitted job. Got id %s.' % response['job_id'])
datastorer_task_status = {
'entity_id': resource['id'],
'entity_type': u'resource',
'task_type': u'datapusher',
'key': u'datapusher_job_data',
'value': json.dumps(job_data),
'last_updated': datetime.now().isoformat(),
'state': 'pending'
}
get_action('task_status_update')(datastorer_task_context,
datastorer_task_status)
else:
logger.error('Command %s not recognized' % (cmd,))

Expand Down
2 changes: 1 addition & 1 deletion pip-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-e git+https://github.com/okfn/messytables.git#egg=messytables
messytables==0.10
-e git+https://github.com/okfn/ckanext-archiver.git#egg=ckanext-archiver
celery==2.4.2
kombu==2.1.3
Expand Down