-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathtask_runners.py
More file actions
756 lines (651 loc) · 32.5 KB
/
task_runners.py
File metadata and controls
756 lines (651 loc) · 32.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
# noqa
# -*- coding: utf-8 -*-
import logging
import os
from os.path import join, exists, basename
import json
import shutil
import zipfile
import traceback
import pickle
import django
from dramatiq.middleware import TimeLimitExceeded
from dramatiq import get_broker
from dramatiq_abort import Abortable, backends
import redis
from django.apps import apps
from django.conf import settings
from django import db
if not apps.ready and not settings.configured:
django.setup()
import dramatiq
from raven import Client
from django.utils import timezone
from django.utils.text import get_valid_filename
from jobs.models import Job, HDXExportRegion, PartnerExportRegion
from tasks.models import ExportRun, ExportTask
from hdx_exports.hdx_export_set import slugify, sync_region
import osm_export_tool
import osm_export_tool.tabular as tabular
import osm_export_tool.nontabular as nontabular
from osm_export_tool.mapping import Mapping
from osm_export_tool.geometry import load_geometry
from osm_export_tool.sources import Overpass, OsmiumTool, Galaxy
from osm_export_tool.package import create_package, create_posm_bundle
import shapely.geometry
from .email import (
send_completion_notification,
send_error_notification,
send_hdx_completion_notification,
send_hdx_error_notification,
)
from .pdc import run_pdc_task
client = Client()
LOG = logging.getLogger(__name__)
ZIP_README = """This thematic file was generated by the HOT Export Tool.
For more information, visit http://export.hotosm.org .
This theme includes features matching the filter:
{criteria}
clipped to the area defined by the included boundary.geojson.
This theme includes the following OpenStreetMap keys:
{columns}
(c) OpenStreetMap contributors.
This file is made available under the Open Database License: http://opendatacommons.org/licenses/odbl/1.0/. Any rights in individual contents of the database are licensed under the Database Contents License: http://opendatacommons.org/licenses/dbcl/1.0/
"""
redis_client = redis.Redis.from_url("redis://localhost:6379/0")
abortable = Abortable(
backend=backends.RedisBackend(client=redis_client)
)
dramatiq.get_broker().add_middleware(abortable)
class ExportTaskRunner(object):
def run_task(self, job_uid=None, user=None, ondemand=True): # noqa
LOG.debug('Running Job with id: {0}'.format(job_uid))
job = Job.objects.get(uid=job_uid)
if not user:
user = job.user
if job.last_run_status != 'SUBMITTED' or job.last_run_status != 'RUNNING':
run = ExportRun.objects.create(job=job, user=user, status='SUBMITTED')
run.save()
run_uid = str(run.uid)
LOG.debug('Saved run with id: {0}'.format(run_uid))
for format_name in job.export_formats:
ExportTask.objects.create(
run=run,
status='PENDING',
name=format_name
)
LOG.debug('Saved task: {0}'.format(format_name))
if HDXExportRegion.objects.filter(job=job).exists():
ondemand=False # move hdx jobs to scheduled even though triggered from run now , so that they won't block ondemand queue
if ondemand:
# run_task_remote(run_uid)
# db.close_old_connections()
send_task=run_task_async_ondemand.send(run_uid)
run.worker_message_id=send_task.message_id
run.save()
LOG.debug("Worker message saved with task_message_id:{0} ".format(run.worker_message_id))
else:
# run_task_remote(run_uid)
# db.close_old_connections()
send_task=run_task_async_scheduled.send(run_uid)
run.worker_message_id=send_task.message_id
run.save()
LOG.debug("Worker message saved with task_message_id:{0} ".format(run.worker_message_id))
return run
else :
LOG.warn('Previous run is on operation already for job: {0}'.format(job_uid))
return None
@dramatiq.actor(max_retries=0,queue_name='default',time_limit=1000*60*60*4) # 4 hour
def run_task_async_ondemand(run_uid):
try:
run_task_remote(run_uid)
except TimeLimitExceeded:
run = ExportRun.objects.get(uid=run_uid)
client.captureException(extra={'run_uid': run_uid})
LOG.warn('ExportRun {0} failed due to timeout'.format(run_uid))
run.status = 'FAILED'
run.finished_at = timezone.now()
run.save()
db.close_old_connections()
@dramatiq.actor(max_retries=0,queue_name='scheduled',time_limit=1000*60*60*12) # 12 hour
def run_task_async_scheduled(run_uid):
try :
run_task_remote(run_uid)
except TimeLimitExceeded:
run = ExportRun.objects.get(uid=run_uid)
client.captureException(extra={'run_uid': run_uid})
LOG.warn('ExportRun {0} failed due to timeout'.format(run_uid))
run.status = 'FAILED'
run.finished_at = timezone.now()
run.save()
db.close_old_connections()
def run_task_remote(run_uid):
stage_dir=None
try:
run = ExportRun.objects.get(uid=run_uid)
run.status = 'RUNNING'
run.started_at = timezone.now()
run.save()
stage_dir = join(settings.EXPORT_STAGING_ROOT, run_uid)
download_dir = join(settings.EXPORT_DOWNLOAD_ROOT,run_uid)
if not exists(stage_dir):
os.makedirs(stage_dir)
if not exists(download_dir):
os.makedirs(download_dir)
run_task(run_uid,run,stage_dir,download_dir)
except (Job.DoesNotExist,ExportRun.DoesNotExist,ExportTask.DoesNotExist):
LOG.warn('Job was deleted - exiting.')
except Exception as ex:
client.captureException(extra={'run_uid': run_uid})
run = ExportRun.objects.get(uid=run_uid)
run.status = 'FAILED'
run.finished_at = timezone.now()
run.save()
if HDXExportRegion.objects.filter(job_id=run.job_id).exists():
send_hdx_error_notification(run, run.job.hdx_export_region_set.first())
LOG.warn('ExportRun {0} failed: {1}'.format(run_uid, ex))
LOG.warn(traceback.format_exc())
finally:
if stage_dir:
shutil.rmtree(stage_dir)
def run_task(run_uid,run,stage_dir,download_dir):
LOG.debug('Running ExportRun with id: {0}'.format(run_uid))
job = run.job
valid_name = get_valid_filename(job.name)
geom = load_geometry(job.simplified_geom.json)
export_formats = job.export_formats
mapping = Mapping(job.feature_selection)
def start_task(name):
task = ExportTask.objects.get(run__uid=run_uid, name=name)
task.status = 'RUNNING'
task.started_at = timezone.now()
task.save()
def stop_task(name):
LOG.debug('Task Failed: {0} for run: {1}'.format(name, run_uid))
task = ExportTask.objects.get(run__uid=run_uid, name=name)
task.status = 'FAILED'
task.finished_at = timezone.now()
task.save()
def finish_task(name,created_files=None,response_back=None,planet_file=False):
LOG.debug('Task Finish: {0} for run: {1}'.format(name, run_uid))
task = ExportTask.objects.get(run__uid=run_uid, name=name)
task.status = 'SUCCESS'
task.finished_at = timezone.now()
# assumes each file only has one part (all are zips or PBFs)
if response_back :
task.filenames = [r['download_url'] for r in response_back]
else:
task.filenames = [basename(file.parts[0]) for file in created_files]
if planet_file is False:
if response_back:
total_bytes = 0
for r in response_back:
total_bytes += int(r['zip_file_size_bytes']) #getting filesize bytes
task.filesize_bytes = total_bytes
else:
total_bytes = 0
for file in created_files:
total_bytes += file.size()
task.filesize_bytes = total_bytes
task.save()
is_hdx_export = HDXExportRegion.objects.filter(job_id=run.job_id).exists()
is_partner_export = PartnerExportRegion.objects.filter(job_id=run.job_id).exists()
planet_file = False
polygon_centroid = False
use_only_galaxy = False
all_feature_filter_json = None
galaxy_supported_outputs = ['geojson','geopackage','kml','shp','fgb','csv','sql']
if galaxy_supported_outputs == list(export_formats) or set(export_formats).issubset(set(galaxy_supported_outputs)):
use_only_galaxy=True
LOG.debug("Using Only galaxy to Perform Request")
if is_hdx_export:
planet_file = HDXExportRegion.objects.get(job_id=run.job_id).planet_file
if is_partner_export:
export_region = PartnerExportRegion.objects.get(job_id=run.job_id)
planet_file = export_region.planet_file
polygon_centroid = export_region.polygon_centroid
# Run PDC special task.
if export_region.group.name == "PDC" and planet_file is True and polygon_centroid is True:
params = {
"PLANET_FILE": settings.PLANET_FILE,
"MAPPING": mapping,
"STAGE_DIR": stage_dir,
"DOWNLOAD_DIR": download_dir,
"VALID_NAME": valid_name
}
if "geopackage" not in export_formats:
raise ValueError("geopackage must be the export format")
paths = run_pdc_task(params)
start_task("geopackage")
target = join(download_dir, "{}.gpkg".format(valid_name))
shutil.move(paths["geopackage"], target)
os.chmod(target, 0o644)
finish_task("geopackage",[osm_export_tool.File("gpkg",[target],'')], planet_file)
send_completion_notification(run)
run.status = 'COMPLETED'
run.finished_at = timezone.now()
run.save()
LOG.debug('Finished ExportRun with id: {0}'.format(run_uid))
return
if is_hdx_export:
geopackage = None
shp = None
kml = None
geojson=None
csv=None
mapping_filter = mapping
if job.unfiltered:
mapping_filter = None
if settings.USE_RAW_DATA_API_FOR_HDX is False:
use_only_galaxy=False # run old format as it as so that galaxy won't interfere
hdx_supported_galaxy=['geojson','shp','kml','geopackage','csv']
if hdx_supported_galaxy == list(export_formats) or set(export_formats).issubset(set(hdx_supported_galaxy)):
use_only_galaxy=True # we don't want to run overpass
tabular_outputs = []
if 'geojson' in export_formats:
geojson = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping,file_name=valid_name)
start_task('geojson')
if 'geopackage' in export_formats:
if settings.USE_RAW_DATA_API_FOR_HDX:
geopackage = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping,file_name=valid_name)
else:
geopackage = tabular.MultiGeopackage(join(stage_dir,valid_name),mapping)
tabular_outputs.append(geopackage)
start_task('geopackage')
if 'shp' in export_formats:
if settings.USE_RAW_DATA_API_FOR_HDX:
shp = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping,file_name=valid_name)
else:
shp = tabular.Shapefile(join(stage_dir,valid_name),mapping)
tabular_outputs.append(shp)
start_task('shp')
if 'kml' in export_formats:
if settings.USE_RAW_DATA_API_FOR_HDX:
kml = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping,file_name=valid_name)
else:
kml = tabular.Kml(join(stage_dir,valid_name),mapping)
tabular_outputs.append(kml)
start_task('kml')
if 'csv' in export_formats:
csv = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping,file_name=valid_name)
start_task('csv')
if planet_file:
h = tabular.Handler(tabular_outputs,mapping,polygon_centroid=polygon_centroid)
source = OsmiumTool('osmium',settings.PLANET_FILE,geom,join(stage_dir,'extract.osm.pbf'),tempdir=stage_dir)
else:
if use_only_galaxy == False :
h = tabular.Handler(tabular_outputs,mapping,clipping_geom=geom,polygon_centroid=polygon_centroid)
source = Overpass(settings.OVERPASS_API_URL,geom,join(stage_dir,'overpass.osm.pbf'),tempdir=stage_dir,use_curl=True,mapping=mapping_filter)
if use_only_galaxy == False :
LOG.debug('Source start for run: {0}'.format(run_uid))
source_path = source.path()
LOG.debug('Source end for run: {0}'.format(run_uid))
h.apply_file(source_path, locations=True, idx='sparse_file_array')
all_zips = []
def add_metadata(z,theme):
columns = []
for key in theme.keys:
columns.append('{0} http://wiki.openstreetmap.org/wiki/Key:{0}'.format(key))
columns = '\n'.join(columns)
readme = ZIP_README.format(criteria=theme.matcher.to_sql(),columns=columns)
z.writestr("README.txt", readme)
if geojson :
try:
LOG.debug('Galaxy fetch started geojson for run: {0}'.format(run_uid))
response_back=geojson.fetch('geojson',is_hdx_export=True)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for geojson run: {0}'.format(run_uid))
finish_task('geojson',response_back=response_back)
all_zips += response_back
except Exception as ex :
stop_task('geojson')
raise ex
if csv :
try:
LOG.debug('Galaxy fetch started for csv run: {0}'.format(run_uid))
response_back=csv.fetch('csv',is_hdx_export=True)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for csv run: {0}'.format(run_uid))
finish_task('csv',response_back=response_back)
all_zips += response_back
except Exception as ex :
stop_task('csv')
raise ex
if geopackage:
try:
if settings.USE_RAW_DATA_API_FOR_HDX:
LOG.debug('Galaxy fetch started for geopackage run: {0}'.format(run_uid))
response_back=geopackage.fetch('gpkg',is_hdx_export=True)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for geopackage run: {0}'.format(run_uid))
finish_task('geopackage',response_back=response_back)
all_zips += response_back
else:
geopackage.finalize()
zips = []
for theme in mapping.themes:
destination = join(download_dir,valid_name + '_' + slugify(theme.name) + '_gpkg.zip')
matching_files = [f for f in geopackage.files if 'theme' in f.extra and f.extra['theme'] == theme.name]
with zipfile.ZipFile(destination, 'w', zipfile.ZIP_DEFLATED, True) as z:
add_metadata(z,theme)
for file in matching_files:
for part in file.parts:
z.write(part, os.path.basename(part))
zips.append(osm_export_tool.File('geopackage',[destination],{'theme':theme.name}))
finish_task('geopackage',zips)
all_zips += zips
except Exception as ex :
stop_task('geopackage')
raise ex
if shp:
try:
if settings.USE_RAW_DATA_API_FOR_HDX:
LOG.debug('Galaxy fetch started for shp run: {0}'.format(run_uid))
response_back=shp.fetch('shp',is_hdx_export=True)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for shp run: {0}'.format(run_uid))
finish_task('shp',response_back=response_back)
all_zips += response_back
else:
shp.finalize()
zips = []
for file in shp.files:
# for HDX geopreview to work
# each file (_polygons, _lines) is a separate zip resource
# the zipfile must end with only .zip (not .shp.zip)
destination = join(download_dir,os.path.basename(file.parts[0]).replace('.','_') + '.zip')
with zipfile.ZipFile(destination, 'w', zipfile.ZIP_DEFLATED, True) as z:
theme = [t for t in mapping.themes if t.name == file.extra['theme']][0]
add_metadata(z,theme)
for part in file.parts:
z.write(part, os.path.basename(part))
zips.append(osm_export_tool.File('shp',[destination],{'theme':file.extra['theme']}))
finish_task('shp',zips)
all_zips += zips
except Exception as ex:
stop_task('shp')
raise ex
if kml:
try:
if settings.USE_RAW_DATA_API_FOR_HDX:
LOG.debug('Galaxy fetch started for kml run: {0}'.format(run_uid))
response_back=kml.fetch('kml',is_hdx_export=True)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for kml run: {0}'.format(run_uid))
finish_task('kml',response_back=response_back)
all_zips += response_back
else: # use overpass
kml.finalize()
zips = []
for file in kml.files:
destination = join(download_dir,os.path.basename(file.parts[0]).replace('.','_') + '.zip')
with zipfile.ZipFile(destination, 'w', zipfile.ZIP_DEFLATED, True) as z:
theme = [t for t in mapping.themes if t.name == file.extra['theme']][0]
add_metadata(z,theme)
for part in file.parts:
z.write(part, os.path.basename(part))
zips.append(osm_export_tool.File('kml',[destination],{'theme':file.extra['theme']}))
finish_task('kml',zips)
all_zips += zips
except Exception as ex :
stop_task('kml')
raise ex
if 'garmin_img' in export_formats:
start_task('garmin_img')
try:
garmin_files = nontabular.garmin(source_path,settings.GARMIN_SPLITTER,settings.GARMIN_MKGMAP,tempdir=stage_dir)
zipped = create_package(join(download_dir,valid_name + '_gmapsupp_img.zip'),garmin_files,boundary_geom=geom,output_name='garmin_img')
all_zips.append(zipped)
finish_task('garmin_img',[zipped])
except Exception as ex :
stop_task('garmin_img')
raise ex
if settings.SYNC_TO_HDX:
LOG.debug('Syncing to HDX for run: {0}'.format(run_uid))
region = HDXExportRegion.objects.get(job_id=run.job_id)
all_zips_data = pickle.dumps(all_zips)
public_dir = settings.HOSTNAME + join(settings.EXPORT_MEDIA_ROOT, run_uid)
pickle_file_path = os.path.join(download_dir,'all_zips.pkl')
with open(pickle_file_path, 'wb') as file:
file.write(all_zips_data)
try:
sync_region(region,all_zips,public_dir)
run.hdx_sync_status = True
except Exception as ex:
run.sync_status = False
LOG.error(ex)
send_hdx_completion_notification(run, run.job.hdx_export_region_set.first())
else:
geopackage = None
shp = None
kml = None
geojson=None
fgb=None
csv=None
sql=None
tabular_outputs = []
mapping_filter = mapping
if job.unfiltered:
mapping_filter = None
if 'geojson' in export_formats:
preserved_geom=geom
if job.preserve_geom :
preserved_geom = load_geometry(job.the_geom.json)
geojson = Galaxy(settings.RAW_DATA_API_URL,preserved_geom,mapping=mapping_filter,file_name=valid_name)
start_task('geojson')
if 'fgb' in export_formats:
fgb = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping_filter,file_name=valid_name)
start_task('fgb')
if 'csv' in export_formats:
csv = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping_filter,file_name=valid_name)
start_task('csv')
if 'sql' in export_formats:
sql = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping_filter,file_name=valid_name)
start_task('sql')
if 'geopackage' in export_formats:
geopackage = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping_filter,file_name=valid_name)
# geopackage = tabular.Geopackage(join(stage_dir,valid_name),mapping)
# tabular_outputs.append(geopackage)
start_task('geopackage')
if 'shp' in export_formats:
shp = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping_filter,file_name=valid_name)
start_task('shp')
if 'kml' in export_formats:
kml = Galaxy(settings.RAW_DATA_API_URL,geom,mapping=mapping_filter,file_name=valid_name)
# kml = tabular.Kml(join(stage_dir,valid_name),mapping)
# tabular_outputs.append(kml)
start_task('kml')
if planet_file:
h = tabular.Handler(tabular_outputs,mapping,polygon_centroid=polygon_centroid)
source = OsmiumTool('osmium',settings.PLANET_FILE,geom,join(stage_dir,'extract.osm.pbf'),tempdir=stage_dir, mapping=mapping)
else:
if use_only_galaxy == False :
h = tabular.Handler(tabular_outputs,mapping,clipping_geom=geom,polygon_centroid=polygon_centroid)
source = Overpass(settings.OVERPASS_API_URL,geom,join(stage_dir,'overpass.osm.pbf'),tempdir=stage_dir,use_curl=True,mapping=mapping_filter)
if use_only_galaxy == False :
LOG.debug('Source start for run: {0}'.format(run_uid))
source_path = source.path()
LOG.debug('Source end for run: {0}'.format(run_uid))
h.apply_file(source_path, locations=True, idx='sparse_file_array')
bundle_files = []
if geojson :
try:
LOG.debug('Galaxy fetch started for geojson run: {0}'.format(run_uid))
all_feature_filter_json=join(os.getcwd(),'tasks/tests/fixtures/all_features_filters.json')
response_back=geojson.fetch('geojson',all_feature_filter_json=all_feature_filter_json)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for geojson run: {0}'.format(run_uid))
finish_task('geojson',response_back=response_back)
except Exception as ex :
stop_task('geojson')
raise ex
if fgb :
try:
LOG.debug('Galaxy fetch started for fgb run: {0}'.format(run_uid))
all_feature_filter_json=join(os.getcwd(),'tasks/tests/fixtures/all_features_filters.json')
response_back=fgb.fetch('fgb',all_feature_filter_json=all_feature_filter_json)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for fgb run: {0}'.format(run_uid))
finish_task('fgb',response_back=response_back)
except Exception as ex :
stop_task('fgb')
raise ex
if csv :
try:
LOG.debug('Galaxy fetch started for csv run: {0}'.format(run_uid))
all_feature_filter_json=join(os.getcwd(),'tasks/tests/fixtures/all_features_filters.json')
response_back=csv.fetch('csv',all_feature_filter_json=all_feature_filter_json)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for csv run: {0}'.format(run_uid))
finish_task('csv',response_back=response_back)
except Exception as ex :
stop_task('csv')
raise ex
if sql :
try:
LOG.debug('Galaxy fetch started for sql run: {0}'.format(run_uid))
all_feature_filter_json=join(os.getcwd(),'tasks/tests/fixtures/all_features_filters.json')
response_back=sql.fetch('sql',all_feature_filter_json=all_feature_filter_json)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for sql run: {0}'.format(run_uid))
finish_task('sql',response_back=response_back)
except Exception as ex :
stop_task('sql')
raise ex
if geopackage:
try :
LOG.debug('Galaxy fetch started for geopackage run: {0}'.format(run_uid))
all_feature_filter_json=join(os.getcwd(),'tasks/tests/fixtures/all_features_filters.json')
response_back=geopackage.fetch('gpkg',all_feature_filter_json=all_feature_filter_json)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for geopackage run: {0}'.format(run_uid))
finish_task('geopackage',response_back=response_back)
except Exception as ex :
stop_task('geopackage')
raise ex
if shp:
try :
LOG.debug('Galaxy fetch started for shp run: {0}'.format(run_uid))
response_back=shp.fetch('shp',all_feature_filter_json=all_feature_filter_json)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for shp run: {0}'.format(run_uid))
finish_task('shp',response_back=response_back)
except Exception as ex :
stop_task('shp')
raise ex
if kml:
try :
LOG.debug('Galaxy fetch started for kml run: {0}'.format(run_uid))
all_feature_filter_json=join(os.getcwd(),'tasks/tests/fixtures/all_features_filters.json')
response_back=kml.fetch('kml',all_feature_filter_json=all_feature_filter_json)
for r in response_back:
size_path=join(download_dir,f"{r['download_url'].split('/')[-1]}_size.txt")
with open(size_path, 'w') as f:
f.write(str(r['zip_file_size_bytes']))
LOG.debug('Galaxy fetch ended for kml run: {0}'.format(run_uid))
finish_task('kml',response_back=response_back)
except Exception as ex :
stop_task('kml')
raise ex
if 'garmin_img' in export_formats:
start_task('garmin_img')
try :
garmin_files = nontabular.garmin(source_path,settings.GARMIN_SPLITTER,settings.GARMIN_MKGMAP,tempdir=stage_dir)
bundle_files += garmin_files
zipped = create_package(join(download_dir,valid_name + '_gmapsupp_img.zip'),garmin_files,boundary_geom=geom)
finish_task('garmin_img',[zipped])
except Exception as ex :
stop_task('garmin_img')
raise ex
if 'mwm' in export_formats:
start_task('mwm')
try :
mwm_dir = join(stage_dir,'mwm')
if not exists(mwm_dir):
os.makedirs(mwm_dir)
mwm_files = nontabular.mwm(source_path,mwm_dir,settings.GENERATE_MWM,settings.GENERATOR_TOOL)
bundle_files += mwm_files
zipped = create_package(join(download_dir,valid_name + '_mwm.zip'),mwm_files,boundary_geom=geom)
finish_task('mwm',[zipped])
except Exception as ex :
stop_task('garmin_img')
raise ex
if 'osmand_obf' in export_formats:
start_task('osmand_obf')
try:
osmand_files = nontabular.osmand(source_path,settings.OSMAND_MAP_CREATOR_DIR,tempdir=stage_dir)
bundle_files += osmand_files
zipped = create_package(join(download_dir,valid_name + '_Osmand2_obf.zip'),osmand_files,boundary_geom=geom)
finish_task('osmand_obf',[zipped])
except Exception as ex :
stop_task('osmand_obf')
raise ex
if 'mbtiles' in export_formats:
start_task('mbtiles')
try:
mbtiles_files = nontabular.mbtiles(geom,join(stage_dir,valid_name + '.mbtiles'),job.mbtiles_source,job.mbtiles_minzoom,job.mbtiles_maxzoom)
bundle_files += mbtiles_files
zipped = create_package(join(download_dir,valid_name + '_mbtiles.zip'),mbtiles_files,boundary_geom=geom)
finish_task('mbtiles',[zipped])
except Exception as ex :
stop_task('mbtiles')
raise ex
if 'osm_pbf' in export_formats:
bundle_files += [osm_export_tool.File('osm_pbf',[source_path],'')]
if 'bundle' in export_formats:
start_task('bundle')
try:
zipped = create_posm_bundle(join(download_dir,valid_name + '-bundle.tar.gz'),bundle_files,job.name,valid_name,job.description,geom)
finish_task('bundle',[zipped])
except Exception as ex :
stop_task('bundle')
raise ex
# do this last so we can do a mv instead of a copy
if 'osm_pbf' in export_formats:
start_task('osm_pbf')
try:
target = join(download_dir,valid_name + '.osm.pbf')
shutil.move(source_path,target)
os.chmod(target, 0o644)
finish_task('osm_pbf',[osm_export_tool.File('pbf',[target],'')], planet_file)
except Exception as ex :
stop_task('osm_pbf')
raise ex
send_completion_notification(run)
run.status = 'COMPLETED'
run.finished_at = timezone.now()
run.save()
LOG.debug('Finished ExportRun with id: {0}'.format(run_uid))