Skip to content

Commit

Permalink
feat(bridge): Add SyncRecord for tracking syncs
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfkaeser committed May 30, 2023
1 parent 2f4fcd5 commit 859cb2b
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 5 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

0 comments on commit 859cb2b

Please sign in to comment.