Skip to content

Commit

Permalink
Merge pull request eclipse-tractusx#204 from catenax-ng/feat/bridge_s…
Browse files Browse the repository at this point in the history
…ync_record

Bridge: Sync record for tracking sync processes
  • Loading branch information
nicoprow authored May 30, 2023
2 parents ddcb1fb + 122e462 commit 6853144
Show file tree
Hide file tree
Showing 16 changed files with 417 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package com.catenax.bpdm.bridge.dummy.entity

import jakarta.persistence.*
import org.eclipse.tractusx.bpdm.common.model.BaseEntity
import org.eclipse.tractusx.bpdm.common.model.BaseSyncRecord
import org.eclipse.tractusx.bpdm.common.model.SyncStatus
import java.time.Instant

@Entity
@Table(name = "sync_records")
class SyncRecord(
@Enumerated(EnumType.STRING)
@Column(name = "type", nullable = false, unique = true)
override var type: SyncType,

@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false)
override var status: SyncStatus,

@Column(name = "from_time", nullable = false)
override var fromTime: Instant,

@Column(name = "progress", nullable = false)
override var progress: Float = 0f,

@Column(name = "count", nullable = false)
override var count: Int = 0,

@Column(name = "status_details")
override var errorDetails: String? = null,

@Column(name = "save_state")
override var errorSave: String? = null,

@Column(name = "started_at")
override var startedAt: Instant? = null,

@Column(name = "finished_at")
override var finishedAt: Instant? = null

) : BaseEntity(), BaseSyncRecord<SyncType>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package com.catenax.bpdm.bridge.dummy.entity

enum class SyncType {
GATE_TO_POOL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package com.catenax.bpdm.bridge.dummy.repository

import com.catenax.bpdm.bridge.dummy.entity.SyncRecord
import com.catenax.bpdm.bridge.dummy.entity.SyncType
import org.springframework.data.repository.CrudRepository

interface SyncRecordRepository : CrudRepository<SyncRecord, Long> {

fun findByType(type: SyncType): SyncRecord?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package com.catenax.bpdm.bridge.dummy.service

import com.catenax.bpdm.bridge.dummy.entity.SyncRecord
import com.catenax.bpdm.bridge.dummy.entity.SyncType
import com.catenax.bpdm.bridge.dummy.repository.SyncRecordRepository
import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.common.model.SyncStatus
import org.eclipse.tractusx.bpdm.common.service.BaseSyncRecordService
import org.springframework.stereotype.Service
import java.time.Instant

@Service
class SyncRecordService(
private val syncRecordRepository: SyncRecordRepository
) : BaseSyncRecordService<SyncType, SyncRecord>() {

override val logger = KotlinLogging.logger { }

override fun newSyncRecord(type: SyncType, syncStartTime: Instant) =
SyncRecord(type, SyncStatus.NOT_SYNCED, syncStartTime)

override fun save(record: SyncRecord) =
syncRecordRepository.save(record)

override fun findByType(type: SyncType) =
syncRecordRepository.findByType(type)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

package com.catenax.bpdm.bridge.dummy.service

import com.catenax.bpdm.bridge.dummy.entity.SyncType
import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType
import org.springframework.stereotype.Service
import java.time.Instant

@Service
class SyncService(
val gateQueryService: GateQueryService,
val poolUpdateService: PoolUpdateService,
val gateUpdateService: GateUpdateService
val gateUpdateService: GateUpdateService,
val syncRecordService: SyncRecordService
) {

private val logger = KotlinLogging.logger { }
Expand All @@ -36,19 +39,21 @@ class SyncService(

fun sync() {
logger.info("Bridge sync started...")
val syncRecord = syncRecordService.setSynchronizationStart(SyncType.GATE_TO_POOL)
try {
syncInternal()
syncInternal(syncRecord.fromTime)
logger.info("Bridge sync completed")
syncRecordService.setSynchronizationSuccess(SyncType.GATE_TO_POOL)
} catch (e: Exception) {
logger.error("Bridge sync failed with critical error:", e)
syncRecordService.setSynchronizationError(SyncType.GATE_TO_POOL, e.toString(), null)
throw e
}
}

private fun syncInternal() {
private fun syncInternal(modifiedAfter: Instant) {
// Check changelog entries from Gate (after last sync time)
val externalIdsByType = gateQueryService.getChangedExternalIdsByLsaType(null)
// TODO persist syncAfter=LocalDateTime.now()
val externalIdsByType = gateQueryService.getChangedExternalIdsByLsaType(modifiedAfter)

externalIdsByType[LsaType.LegalEntity]?.let { syncLegalEntities(it) }
externalIdsByType[LsaType.Site]?.let { syncSites(it) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE SEQUENCE IF NOT EXISTS bpdm_sequence START WITH 1 INCREMENT BY 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE TABLE sync_records
(
id BIGINT NOT NULL,
uuid UUID NOT NULL,
created_at TIMESTAMP with time zone NOT NULL,
updated_at TIMESTAMP with time zone NOT NULL,
type VARCHAR(255) NOT NULL,
status VARCHAR(255) NOT NULL,
progress FLOAT NOT NULL,
count INTEGER NOT NULL,
status_details VARCHAR(255),
save_state VARCHAR(255),
started_at TIMESTAMP with time zone,
finished_at TIMESTAMP with time zone,
from_time TIMESTAMP with time zone,
CONSTRAINT pk_sync_records PRIMARY KEY (id)
);

ALTER TABLE sync_records
ADD CONSTRAINT uc_sync_records_type UNIQUE (type);

ALTER TABLE sync_records
ADD CONSTRAINT uc_sync_records_uuid UNIQUE (uuid);
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package org.eclipse.tractusx.bpdm.pool.exception
package org.eclipse.tractusx.bpdm.common.exception

import org.eclipse.tractusx.bpdm.pool.api.model.SyncType
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ResponseStatus

@ResponseStatus(HttpStatus.CONFLICT)
class BpdmSyncConflictException(
val type: SyncType
val type: Enum<*>
) : RuntimeException("Synchronization of type $type already running")
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package org.eclipse.tractusx.bpdm.pool.exception
package org.eclipse.tractusx.bpdm.common.exception

class BpdmSyncStateException(
msg: String
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package org.eclipse.tractusx.bpdm.common.model

import java.time.Instant

interface BaseSyncRecord<SYNC_TYPE> {
var type: SYNC_TYPE

var status: SyncStatus

var fromTime: Instant

var progress: Float

var count: Int

var errorDetails: String?

var errorSave: String?

var startedAt: Instant?

var finishedAt: Instant?
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package org.eclipse.tractusx.bpdm.pool.api.model
package org.eclipse.tractusx.bpdm.common.model

enum class SyncStatus{
NOT_SYNCED,
Expand Down
Loading

0 comments on commit 6853144

Please sign in to comment.