Skip to content

Commit

Permalink
[Fixes GeoNode#10024] Wrong default style when creating layer
Browse files Browse the repository at this point in the history
(cherry picked from commit 40fae5b)
  • Loading branch information
etj committed Sep 19, 2022
1 parent 5636f6a commit 4bc7f66
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
52 changes: 30 additions & 22 deletions geonode/geoserver/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ def set_attributes_from_geoserver(layer, overwrite=False):
)


def get_dataset(layer, gs_catalog):
def get_dataset(layer, gs_catalog: Catalog):
gs_catalog.reset()
gs_dataset = None
try:
Expand All @@ -1151,24 +1151,27 @@ def get_dataset(layer, gs_catalog):
return gs_dataset


def clean_styles(layer, gs_catalog):
def clean_styles(layer, gs_catalog: Catalog):
try:
# Cleanup Styles without a Workspace
gs_catalog.reset()
gs_dataset = get_dataset(layer, gs_catalog)
gs_catalog.delete(
gs_catalog.get_style(
name=gs_dataset.default_style.name,
workspace=None,
recursive=True),
purge=True,
recurse=False)
except Exception:
tb = traceback.format_exc()
logger.debug(tb)
logger.debug(f'clean_styles: Retrieving style "{gs_dataset.default_style.name}" for cleanup')
style = gs_catalog.get_style(
name=gs_dataset.default_style.name,
workspace=None,
recursive=True)
if style:
gs_catalog.delete(style, purge=True, recurse=False)
logger.debug(f'clean_styles: Style removed: {gs_dataset.default_style.name}')
else:
logger.debug(f'clean_styles: Style does not exist: {gs_dataset.default_style.name}')
except Exception as e:
logger.warning(f'Could not clean style for layer {layer.name}', exc_info=e)
logger.debug(f'Could not clean style for layer {layer.name} - STACK INFO', stack_info=True)


def set_styles(layer, gs_catalog):
def set_styles(layer, gs_catalog: Catalog):
style_set = []
gs_dataset = get_dataset(layer, gs_catalog)
if gs_dataset:
Expand All @@ -1178,18 +1181,21 @@ def set_styles(layer, gs_catalog):
layer.default_style, _gs_default_style = save_style(default_style, layer)
try:
if default_style.name != _gs_default_style.name or default_style.workspace != _gs_default_style.workspace:
logger.debug(f'set_style: Setting default style "{_gs_default_style.name}" for layer "{layer.name}')

gs_dataset.default_style = _gs_default_style
gs_catalog.save(gs_dataset)
if default_style.name not in DEFAULT_STYLE_NAME:
gs_catalog.delete(
gs_catalog.get_style(
name=default_style.name,
workspace=None,
recursive=True),
purge=True,
recurse=False)
logger.debug(f'set_style: Retrieving no-workspace default style "{default_style.name}" for deletion')
style_to_delete = gs_catalog.get_style(name=default_style.name, workspace=None, recursive=True)
if style_to_delete:
gs_catalog.delete(style_to_delete, purge=True, recurse=False)
logger.debug(f'set_style: No-ws default style deleted: {default_style.name}')
else:
logger.debug(f'set_style: No-ws default style does not exist: {default_style.name}')
except Exception as e:
logger.exception(e)
logger.error(f'Error setting default style "{_gs_default_style.name}" for layer "{layer.name}', exc_info=e)

style_set.append(layer.default_style)

try:
Expand Down Expand Up @@ -1218,7 +1224,7 @@ def set_styles(layer, gs_catalog):
layer.refresh_from_db()

# Legend links
logger.debug(" -- Resource Links[Legend link]...")
logger.debug(f" -- Resource Links[Legend link] for layer {layer.name}...")
try:
from geonode.base.models import Link
dataset_legends = Link.objects.filter(resource=layer.resourcebase_ptr, name='Legend')
Expand Down Expand Up @@ -1257,11 +1263,13 @@ def save_style(gs_style, layer):
sld_body = copy.copy(gs_style.sld_body)
_gs_style = None
if not gs_style.workspace:
logger.debug(f'save_style: Copying style "{sld_name}" to "{layer.workspace}:{layer.name}')
_gs_style = gs_catalog.create_style(
layer.name, sld_body,
raw=True, overwrite=True,
workspace=layer.workspace)
else:
logger.debug(f'save_style: Retrieving style "{layer.workspace}:{sld_name}" for layer "{layer.workspace}:{layer.name}')
_gs_style = gs_catalog.get_style(
name=sld_name,
workspace=layer.workspace
Expand Down
40 changes: 35 additions & 5 deletions geonode/geoserver/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
import logging
import os

from django.conf import settings
Expand Down Expand Up @@ -46,6 +47,8 @@

logger = get_task_logger(__name__)

log_lock = logging.getLogger("geonode_lock_handler")


@app.task(
bind=True,
Expand All @@ -65,12 +68,16 @@ def geoserver_update_datasets(self, *args, **kwargs):
Runs update layers.
"""
lock_id = f'{self.request.id}'
log_lock.debug(f"geoserver_update_datasets: Creating lock {lock_id}")
with AcquireLock(lock_id) as lock:
log_lock.debug(f"geoserver_update_datasets: Acquiring lock {lock_id}")
if lock.acquire() is True:
log_lock.debug(f"geoserver_update_datasets: Acquired lock {lock_id}")
try:
return gs_slurp(*args, **kwargs)
finally:
lock.release()
log_lock.debug(f"geoserver_update_datasets: Released lock {lock_id}")


@app.task(
Expand Down Expand Up @@ -100,9 +107,12 @@ def geoserver_set_style(
logger.debug(f"Dataset id {instance_id} does not exist yet!")
raise

lock_id = f'{self.request.id}'
lock_id = f'{self.request.id}' if self.request.id else instance.name
log_lock.debug(f"geoserver_set_style: Creating lock {lock_id} for {instance.name}")
with AcquireLock(lock_id) as lock:
log_lock.debug(f"geoserver_set_style: Acquiring lock {lock_id} for {instance.name}")
if lock.acquire() is True:
log_lock.debug(f"geoserver_set_style: Acquired lock {lock_id} for {instance.name}")
try:
sld = open(base_file, "rb").read()
set_dataset_style(
Expand All @@ -114,6 +124,7 @@ def geoserver_set_style(
logger.exception(e)
finally:
lock.release()
log_lock.debug(f"geoserver_set_style: Released lock {lock_id} for {instance.name}")


@app.task(
Expand Down Expand Up @@ -146,9 +157,12 @@ def geoserver_create_style(
logger.debug(f"Dataset id {instance_id} does not exist yet!")
raise

lock_id = f'{self.request.id}'
lock_id = f'{self.request.id}' if self.request.id else instance.name
log_lock.debug(f"geoserver_create_style: Creating lock {lock_id} for {instance.name}")
with AcquireLock(lock_id) as lock:
if lock.acquire() is True and instance:
log_lock.debug(f"geoserver_create_style: Acquiring lock {lock_id} for {instance.name}")
if lock.acquire() is True:
log_lock.debug(f"geoserver_create_style: Acquired lock {lock_id} for {instance.name}")
try:
f = None
if sld_file and os.path.exists(sld_file) and os.access(sld_file, os.R_OK):
Expand Down Expand Up @@ -194,6 +208,7 @@ def geoserver_create_style(
geoserver_automatic_default_style_set.send_robust(sender=instance, instance=instance)
finally:
lock.release()
log_lock.debug(f"geoserver_create_style: Released lock {lock_id} for {instance.name}")


@app.task(
Expand All @@ -216,9 +231,19 @@ def geoserver_post_save_datasets(
"""
Runs update layers.
"""
lock_id = f'{self.request.id}'
instance = None
try:
instance = Dataset.objects.get(id=instance_id)
except Dataset.DoesNotExist:
logger.debug(f"Dataset id {instance_id} does not exist yet!")
raise

lock_id = f'{self.request.id}' if self.request.id else instance.name
log_lock.debug(f"geoserver_post_save_datasets: Creating lock {lock_id} for {instance_id}")
with AcquireLock(lock_id) as lock:
log_lock.debug(f"geoserver_post_save_datasets: Acquiring lock {lock_id} for {instance_id}")
if lock.acquire() is True:
log_lock.debug(f"geoserver_post_save_datasets: Acquired lock {lock_id} for {instance_id}")
try:
sync_instance_with_geoserver(instance_id, *args, **kwargs)

Expand All @@ -227,6 +252,7 @@ def geoserver_post_save_datasets(
call_command('update_index')
finally:
lock.release()
log_lock.debug(f"geoserver_post_save_datasets: Releasing lock {lock_id} for {instance_id}")


@app.task(
Expand All @@ -253,9 +279,12 @@ def geoserver_create_thumbnail(self, instance_id, overwrite=True, check_bbox=Tru
logger.error(f"Resource id {instance_id} does not exist yet!")
raise

lock_id = f'{self.request.id}'
lock_id = f'{self.request.id}' if self.request.id else instance.name
log_lock.debug(f"geoserver_create_thumbnail: Creating lock {lock_id} for {instance.name}")
with AcquireLock(lock_id) as lock:
log_lock.debug(f"geoserver_create_thumbnail: Acquiring lock {lock_id} for {instance.name}")
if lock.acquire() is True:
log_lock.debug(f"geoserver_create_thumbnail: Acquired lock {lock_id} for {instance.name}")
try:
instance.set_processing_state(enumerations.STATE_RUNNING)
try:
Expand All @@ -268,6 +297,7 @@ def geoserver_create_thumbnail(self, instance_id, overwrite=True, check_bbox=Tru
instance.set_processing_state(enumerations.STATE_PROCESSED)
finally:
lock.release()
log_lock.debug(f"geoserver_create_thumbnail: Released lock {lock_id} for {instance.name}")


@app.task(
Expand Down

0 comments on commit 4bc7f66

Please sign in to comment.