Skip to content

Commit 00da5c3

Browse files
committed
feat: Add a model for maintenance jobs
Add the model and a database table for maintenance jobs. This will initially be used for database clean-up tasks that take a long time to finish. The table can be used by those jobs to store progress information to be able to continue the work if the server was restarted before the job could finish. Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent 5f24cd2 commit 00da5c3

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2024 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.eclipse.apoapsis.ortserver.dao.tables
21+
22+
import kotlinx.serialization.json.JsonObject
23+
24+
import org.eclipse.apoapsis.ortserver.dao.utils.jsonb
25+
import org.eclipse.apoapsis.ortserver.dao.utils.toDatabasePrecision
26+
import org.eclipse.apoapsis.ortserver.model.MaintenanceJobData
27+
import org.eclipse.apoapsis.ortserver.model.MaintenanceJobStatus
28+
29+
import org.jetbrains.exposed.dao.LongEntity
30+
import org.jetbrains.exposed.dao.LongEntityClass
31+
import org.jetbrains.exposed.dao.id.EntityID
32+
import org.jetbrains.exposed.dao.id.LongIdTable
33+
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp
34+
35+
/**
36+
* A table that stores the state of maintenance jobs.
37+
*/
38+
object MaintenanceJobsTable : LongIdTable("maintenance_jobs") {
39+
val name = text("name")
40+
val status = enumerationByName<MaintenanceJobStatus>("status", 128)
41+
val startedAt = timestamp("started_at").nullable()
42+
val updatedAt = timestamp("updated_at").nullable()
43+
val finishedAt = timestamp("finished_at").nullable()
44+
val data = jsonb<JsonObject>("data").nullable()
45+
}
46+
47+
class MaintenanceJobDao(id: EntityID<Long>) : LongEntity(id) {
48+
companion object : LongEntityClass<MaintenanceJobDao>(MaintenanceJobsTable)
49+
50+
var name by MaintenanceJobsTable.name
51+
var status by MaintenanceJobsTable.status
52+
var startedAt by MaintenanceJobsTable.startedAt.transform({ it?.toDatabasePrecision() }, { it })
53+
var updatedAt by MaintenanceJobsTable.updatedAt.transform({ it?.toDatabasePrecision() }, { it })
54+
var finishedAt by MaintenanceJobsTable.finishedAt.transform({ it?.toDatabasePrecision() }, { it })
55+
var data by MaintenanceJobsTable.data
56+
57+
fun mapToModel() = MaintenanceJobData(
58+
id = id.value,
59+
name = name,
60+
status = status,
61+
startedAt = startedAt,
62+
updatedAt = updatedAt,
63+
finishedAt = finishedAt,
64+
data = data
65+
)
66+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE maintenance_jobs
2+
(
3+
id bigserial PRIMARY KEY,
4+
name text NOT NULL,
5+
status text NOT NULL,
6+
started_at timestamp,
7+
updated_at timestamp,
8+
finished_at timestamp,
9+
data jsonb
10+
);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) 2024 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.eclipse.apoapsis.ortserver.model
21+
22+
import kotlinx.datetime.Instant
23+
import kotlinx.serialization.json.JsonObject
24+
25+
/**
26+
* The data of a maintenance job, as stored in the database.
27+
*/
28+
data class MaintenanceJobData(
29+
/** The unique identifier. */
30+
val id: Long,
31+
32+
/** The name of the job. */
33+
val name: String,
34+
35+
/** The status of the job. */
36+
val status: MaintenanceJobStatus,
37+
38+
/** The time the job was started. */
39+
val startedAt: Instant?,
40+
41+
/** The time the job was last updated. */
42+
val updatedAt: Instant?,
43+
44+
/** The time the job finished. */
45+
val finishedAt: Instant?,
46+
47+
/** The job data. */
48+
val data: JsonObject?
49+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2024 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.eclipse.apoapsis.ortserver.model
21+
22+
/**
23+
* An enum representing the status of a maintenance job.
24+
*/
25+
enum class MaintenanceJobStatus(val completed: Boolean) {
26+
/** The job is currently running. */
27+
STARTED(completed = false),
28+
29+
/** The job has finished successfully. */
30+
FINISHED(completed = true),
31+
32+
/** The job has failed. */
33+
FAILED(completed = true);
34+
35+
companion object {
36+
val uncompletedStates = entries.filter { !it.completed }
37+
}
38+
}

0 commit comments

Comments
 (0)