-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add service to run maintenance jobs #1034
Draft
mnonnenmacher
wants to merge
7
commits into
eclipse-apoapsis:main
Choose a base branch
from
boschglobal:maintenance-jobs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
24c0384
feat(dao): Add index on `packages_authors.package_id`
mnonnenmacher 523ed35
feat(dao): Use the package with the lowest id in `findByPackage`
mnonnenmacher 64a0519
feat: Add a model for maintenance jobs
mnonnenmacher 704508a
feat: Add a service to run maintenance jobs
mnonnenmacher fafccd1
test(dao): Move helper function to fixtures
mnonnenmacher cb62da8
feat(maintenance-service): Add a job to deduplicate packages
mnonnenmacher 40ac248
feat(core): Run the maintenance service
mnonnenmacher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License 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 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.eclipse.apoapsis.ortserver.dao.tables | ||
|
||
import kotlinx.serialization.json.JsonObject | ||
|
||
import org.eclipse.apoapsis.ortserver.dao.utils.jsonb | ||
import org.eclipse.apoapsis.ortserver.dao.utils.toDatabasePrecision | ||
import org.eclipse.apoapsis.ortserver.model.MaintenanceJobData | ||
import org.eclipse.apoapsis.ortserver.model.MaintenanceJobStatus | ||
|
||
import org.jetbrains.exposed.dao.LongEntity | ||
import org.jetbrains.exposed.dao.LongEntityClass | ||
import org.jetbrains.exposed.dao.id.EntityID | ||
import org.jetbrains.exposed.dao.id.LongIdTable | ||
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp | ||
|
||
/** | ||
* A table that stores the state of maintenance jobs. | ||
*/ | ||
object MaintenanceJobsTable : LongIdTable("maintenance_jobs") { | ||
val name = text("name") | ||
val status = enumerationByName<MaintenanceJobStatus>("status", 128) | ||
val startedAt = timestamp("started_at").nullable() | ||
val updatedAt = timestamp("updated_at").nullable() | ||
val finishedAt = timestamp("finished_at").nullable() | ||
val data = jsonb<JsonObject>("data").nullable() | ||
} | ||
|
||
class MaintenanceJobDao(id: EntityID<Long>) : LongEntity(id) { | ||
companion object : LongEntityClass<MaintenanceJobDao>(MaintenanceJobsTable) | ||
|
||
var name by MaintenanceJobsTable.name | ||
var status by MaintenanceJobsTable.status | ||
var startedAt by MaintenanceJobsTable.startedAt.transform({ it?.toDatabasePrecision() }, { it }) | ||
var updatedAt by MaintenanceJobsTable.updatedAt.transform({ it?.toDatabasePrecision() }, { it }) | ||
var finishedAt by MaintenanceJobsTable.finishedAt.transform({ it?.toDatabasePrecision() }, { it }) | ||
var data by MaintenanceJobsTable.data | ||
|
||
fun mapToModel() = MaintenanceJobData( | ||
id = id.value, | ||
name = name, | ||
status = status, | ||
startedAt = startedAt, | ||
updatedAt = updatedAt, | ||
finishedAt = finishedAt, | ||
data = data | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
dao/src/main/resources/db/migration/V72__createIndexPackagesAuthorsPackageId.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE INDEX packages_authors_package_id ON packages_authors(package_id); |
10 changes: 10 additions & 0 deletions
10
dao/src/main/resources/db/migration/V73__maintenanceJobs.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE maintenance_jobs | ||
( | ||
id bigserial PRIMARY KEY, | ||
name text NOT NULL, | ||
status text NOT NULL, | ||
started_at timestamp, | ||
updated_at timestamp, | ||
finished_at timestamp, | ||
data jsonb | ||
); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License 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 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.eclipse.apoapsis.ortserver.model | ||
|
||
import kotlinx.datetime.Instant | ||
import kotlinx.serialization.json.JsonObject | ||
|
||
/** | ||
* The data of a maintenance job, as stored in the database. | ||
*/ | ||
data class MaintenanceJobData( | ||
/** The unique identifier. */ | ||
val id: Long, | ||
|
||
/** The name of the job. */ | ||
val name: String, | ||
|
||
/** The status of the job. */ | ||
val status: MaintenanceJobStatus, | ||
|
||
/** The time the job was started. */ | ||
val startedAt: Instant?, | ||
|
||
/** The time the job was last updated. */ | ||
val updatedAt: Instant?, | ||
|
||
/** The time the job finished. */ | ||
val finishedAt: Instant?, | ||
|
||
/** The job data. */ | ||
val data: JsonObject? | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License 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 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.eclipse.apoapsis.ortserver.model | ||
|
||
/** | ||
* An enum representing the status of a maintenance job. | ||
*/ | ||
enum class MaintenanceJobStatus(val completed: Boolean) { | ||
/** The job is currently running. */ | ||
STARTED(completed = false), | ||
|
||
/** The job has finished successfully. */ | ||
FINISHED(completed = true), | ||
|
||
/** The job has failed. */ | ||
FAILED(completed = true); | ||
|
||
companion object { | ||
val uncompletedStates = entries.filter { !it.completed } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
mnonnenmacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Copyright (C) 2024 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License 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 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
plugins { | ||
// Apply precompiled plugins. | ||
id("ort-server-kotlin-jvm-conventions") | ||
id("ort-server-publication-conventions") | ||
|
||
// Apply third-party plugins. | ||
alias(libs.plugins.kotlinSerialization) | ||
} | ||
|
||
group = "org.eclipse.apoapsis.ortserver.services" | ||
|
||
dependencies { | ||
api(projects.model) | ||
|
||
implementation(projects.dao) | ||
implementation(projects.utils.logging) | ||
|
||
implementation(libs.kotlinxSerializationJson) | ||
|
||
runtimeOnly(libs.logback) | ||
|
||
testImplementation(testFixtures(projects.dao)) | ||
testImplementation(projects.utils.test) | ||
|
||
testImplementation(libs.kotestAssertionsCore) | ||
testImplementation(libs.kotestRunnerJunit5) | ||
testImplementation(libs.mockk) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) 2024 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License 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 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.eclipse.apoapsis.ortserver.services.maintenance | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
import org.eclipse.apoapsis.ortserver.model.MaintenanceJobData | ||
import org.eclipse.apoapsis.ortserver.utils.logging.withMdcContext | ||
|
||
abstract class MaintenanceJob { | ||
abstract val name: String | ||
|
||
private val _active = AtomicBoolean(false) | ||
|
||
val active get() = _active.get() | ||
|
||
suspend fun start(service: MaintenanceService, jobData: MaintenanceJobData) { | ||
_active.set(true) | ||
|
||
try { | ||
withMdcContext("maintenanceJob" to name) { | ||
execute(service, jobData) | ||
} | ||
} finally { | ||
_active.set(false) | ||
} | ||
} | ||
|
||
abstract suspend fun execute(service: MaintenanceService, jobData: MaintenanceJobData) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Will this be removed again when the job is done?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's stored in a
ThreadLocal
so it will die with the thread.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, what I meant was the whole block to add the job to the maintenance service. When the migration is complete it is not needed any more. If the job is started, it will probably still do some (minor) background activity.
I guess, the underlying problem here is that the minimum maintenance service implementation does not provide a mechanism to add/remove jobs dynamically; therefore, starting of jobs has to be hard-coded.