|
| 1 | +package me.snoty.backend.integration.moodle |
| 2 | + |
| 3 | +import kotlinx.coroutines.runBlocking |
| 4 | +import me.snoty.backend.integration.common.InstanceId |
| 5 | +import me.snoty.backend.integration.common.IntegrationConfig |
| 6 | +import me.snoty.backend.integration.common.IntegrationScheduler |
| 7 | +import me.snoty.backend.integration.common.IntegrationSchedulerFactory |
| 8 | +import me.snoty.backend.integration.common.diff.EntityDiffMetrics |
| 9 | +import me.snoty.backend.integration.common.diff.IUpdatableEntity |
| 10 | +import me.snoty.backend.integration.moodle.request.getCalendarUpcoming |
| 11 | +import me.snoty.backend.scheduling.JobRunrUtils |
| 12 | +import org.jobrunr.jobs.lambdas.JobRequest |
| 13 | +import org.jobrunr.jobs.lambdas.JobRequestHandler |
| 14 | +import org.slf4j.LoggerFactory |
| 15 | +import java.util.* |
| 16 | + |
| 17 | +class ScheduleMoodleRequest : JobRequest { |
| 18 | + override fun getJobRequestHandler(): Class<out JobRequestHandler<JobRequest>> { |
| 19 | + @Suppress("UNCHECKED_CAST") |
| 20 | + return MoodleRequestHandler::class.java as Class<out JobRequestHandler<JobRequest>> |
| 21 | + } |
| 22 | + |
| 23 | + var user: UUID? = null |
| 24 | + var settings: MoodleSettings? = null |
| 25 | +} |
| 26 | + |
| 27 | +class MoodleRequestHandler(val moodleScheduler: MoodleScheduler) : JobRequestHandler<ScheduleMoodleRequest> { |
| 28 | + override fun run(jobRequest: ScheduleMoodleRequest) { |
| 29 | + runBlocking { |
| 30 | + moodleScheduler.fetchAssignements(jobRequest.settings!!) |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +open class MoodleScheduler(private val entityDiffMetrics: EntityDiffMetrics, private val moodleAPI: MoodleAPI = MoodleAPIImpl()) : IntegrationScheduler<MoodleSettings> { |
| 36 | + private val jobRunrUtils = JobRunrUtils("moodle") |
| 37 | + private val logger = LoggerFactory.getLogger(javaClass) |
| 38 | + |
| 39 | + fun updateStates(instanceId: InstanceId, elements: List<IUpdatableEntity<Long>>) { |
| 40 | + elements.forEach { |
| 41 | + val result = MoodleEntityStateTable.compareAndUpdateState(instanceId, it) |
| 42 | + entityDiffMetrics.process(result) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + suspend fun fetchAssignements(moodleSettings: MoodleSettings) { |
| 47 | + val instanceId = moodleSettings.baseUrl.hashCode() |
| 48 | + val assignments = moodleAPI.getCalendarUpcoming(moodleSettings) |
| 49 | + updateStates(instanceId, assignments) |
| 50 | + logger.info("Fetched ${assignments.size} assignments for ${moodleSettings.username}") |
| 51 | + // TODO: send update events |
| 52 | + } |
| 53 | + |
| 54 | + override fun schedule(config: IntegrationConfig<MoodleSettings>) { |
| 55 | + val instanceId = config.settings.baseUrl.hashCode() |
| 56 | + jobRunrUtils.scheduleJob(listOf(instanceId, config.user), customizer = { |
| 57 | + // TODO: inline this, unfortunately, jobrunr is doing some weird stuff with the JobActivator |
| 58 | + this.withDetails { |
| 59 | + fetchAll(config) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + open fun fetchAll(config: IntegrationConfig<MoodleSettings>) { |
| 65 | + runBlocking { |
| 66 | + fetchAssignements(config.settings) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + class Factory(private val moodleAPI: MoodleAPI) : IntegrationSchedulerFactory<MoodleSettings> { |
| 71 | + override fun create(entityDiffMetrics: EntityDiffMetrics): IntegrationScheduler<MoodleSettings> { |
| 72 | + return MoodleScheduler(entityDiffMetrics, moodleAPI) |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments