Skip to content

Fixing duplicate key error in comp_pipeline #1934

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

Merged
merged 4 commits into from
Nov 5, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,9 @@ async def stop_service(app: web.Application, node_uuid: str) -> None:
await response.text(),
)
except ClientConnectionError:
log.exception("service %s could not be contacted, state not saved")
log.exception(
"service %s could not be contacted, state not saved", service_host_name
)

# remove the services
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from celery import Celery
from celery.contrib.abortable import AbortableAsyncResult
from sqlalchemy import and_
from sqlalchemy.dialects.postgresql import insert

from models_library.projects import RunningState
from servicelib.application_keys import APP_DB_ENGINE_KEY
Expand Down Expand Up @@ -241,37 +242,23 @@ async def _parse_project_data(pipeline_data: Dict, app: web.Application):
async def _set_adjacency_in_pipeline_db(
db_engine: Engine, project_id: str, dag_adjacency_list: Dict
):
# pylint: disable=no-value-for-parameter
async with db_engine.acquire() as conn:
# READ
# get pipeline
query = sa.select([comp_pipeline]).where(
comp_pipeline.c.project_id == project_id
# Because race conditions always use an upsert operation
insert_stmt = insert(comp_pipeline).values(
project_id=project_id,
dag_adjacency_list=dag_adjacency_list,
state=StateType.NOT_STARTED,
)
upsert_stmt = insert_stmt.on_conflict_do_update(
index_elements=[
comp_pipeline.c.project_id,
],
set_=dict(
dag_adjacency_list=dag_adjacency_list, state=StateType.NOT_STARTED
),
)
result = await conn.execute(query)
pipeline = await result.first()

# WRITE
if pipeline is None:
# create pipeline
log.debug("No pipeline for project %s, creating one", project_id)
query = comp_pipeline.insert().values(
project_id=project_id,
dag_adjacency_list=dag_adjacency_list,
state=StateType.NOT_STARTED,
)
else:
# update pipeline
log.debug("Found pipeline for project %s, updating it", project_id)
query = (
comp_pipeline.update()
.where(comp_pipeline.c.project_id == project_id)
.values(
dag_adjacency_list=dag_adjacency_list, state=StateType.NOT_STARTED
)
)

await conn.execute(query)
await conn.execute(upsert_stmt)


@log_decorator(logger=log)
Expand Down