Add the concept of queuePriority to jobs.

This commit is contained in:
Greyson Parrelli
2024-10-09 12:22:10 -04:00
parent 229a78305f
commit 5cafea2bde
12 changed files with 274 additions and 88 deletions

View File

@@ -63,7 +63,8 @@ class JobDatabase(
const val SERIALIZED_DATA = "serialized_data"
const val SERIALIZED_INPUT_DATA = "serialized_input_data"
const val IS_RUNNING = "is_running"
const val PRIORITY = "priority"
const val GLOBAL_PRIORITY = "global_priority"
const val QUEUE_PRIORITY = "queue_priority"
val CREATE_TABLE =
"""
@@ -81,7 +82,8 @@ class JobDatabase(
$SERIALIZED_INPUT_DATA TEXT DEFAULT NULL,
$IS_RUNNING INTEGER,
$NEXT_BACKOFF_INTERVAL INTEGER,
$PRIORITY INTEGER DEFAULT 0
$GLOBAL_PRIORITY INTEGER DEFAULT 0,
$QUEUE_PRIORITY INTEGER DEFAULT 0
)
""".trimIndent()
}
@@ -140,6 +142,11 @@ class JobDatabase(
if (oldVersion < 3) {
db.execSQL("ALTER TABLE job_spec ADD COLUMN priority INTEGER DEFAULT 0")
}
if (oldVersion < 4) {
db.execSQL("ALTER TABLE job_spec RENAME COLUMN priority TO global_priority")
db.execSQL("ALTER TABLE job_spec ADD COLUMN queue_priority INTEGER DEFAULT 0")
}
}
override fun onOpen(db: SQLiteDatabase) {
@@ -179,7 +186,7 @@ class JobDatabase(
.select()
.from(Jobs.TABLE_NAME)
.where("${Jobs.QUEUE_KEY} = ?", queue)
.orderBy("${Jobs.PRIORITY} DESC, ${Jobs.CREATE_TIME} ASC, ${Jobs.ID} ASC")
.orderBy("${Jobs.GLOBAL_PRIORITY} DESC, ${Jobs.QUEUE_PRIORITY} DESC, ${Jobs.CREATE_TIME} ASC, ${Jobs.ID} ASC")
.limit(1)
.run()
.readToSingleObject { it.toJobSpec() }
@@ -224,7 +231,8 @@ class JobDatabase(
Jobs.LAST_RUN_ATTEMPT_TIME,
Jobs.NEXT_BACKOFF_INTERVAL,
Jobs.IS_RUNNING,
Jobs.PRIORITY
Jobs.GLOBAL_PRIORITY,
Jobs.QUEUE_PRIORITY
)
return readableDatabase
.query(Jobs.TABLE_NAME, columns, null, null, null, null, "${Jobs.CREATE_TIME}, ${Jobs.ID} ASC")
@@ -236,7 +244,8 @@ class JobDatabase(
createTime = cursor.requireLong(Jobs.CREATE_TIME),
lastRunAttemptTime = cursor.requireLong(Jobs.LAST_RUN_ATTEMPT_TIME),
nextBackoffInterval = cursor.requireLong(Jobs.NEXT_BACKOFF_INTERVAL),
priority = cursor.requireInt(Jobs.PRIORITY),
globalPriority = cursor.requireInt(Jobs.GLOBAL_PRIORITY),
queuePriority = cursor.requireInt(Jobs.QUEUE_PRIORITY),
isRunning = cursor.requireBoolean(Jobs.IS_RUNNING),
isMemoryOnly = false
)
@@ -440,7 +449,8 @@ class JobDatabase(
serializedInputData = this.requireBlob(Jobs.SERIALIZED_INPUT_DATA),
isRunning = this.requireBoolean(Jobs.IS_RUNNING),
isMemoryOnly = false,
priority = this.requireInt(Jobs.PRIORITY)
globalPriority = this.requireInt(Jobs.GLOBAL_PRIORITY),
queuePriority = this.requireInt(Jobs.QUEUE_PRIORITY)
)
}
@@ -483,13 +493,14 @@ class JobDatabase(
Jobs.SERIALIZED_DATA to this.serializedData,
Jobs.SERIALIZED_INPUT_DATA to this.serializedInputData,
Jobs.IS_RUNNING to if (this.isRunning) 1 else 0,
Jobs.PRIORITY to this.priority
Jobs.GLOBAL_PRIORITY to this.globalPriority,
Jobs.QUEUE_PRIORITY to this.queuePriority
)
}
companion object {
private val TAG = Log.tag(JobDatabase::class.java)
private const val DATABASE_VERSION = 3
private const val DATABASE_VERSION = 4
private const val DATABASE_NAME = "signal-jobmanager.db"
@SuppressLint("StaticFieldLeak")