override fun requestTask()

in plugin-azure-server/src/main/kotlin/jetbrains/buildServer/clouds/azure/arm/throttler/AzureThrottlerTaskQueueImpl.kt [65:108]


    override fun requestTask(flow: AzureThrottlerFlow, parameters: P): Single<AzureThrottlerAdapterResult<T>> {
        myCallHistory.addRequestCall()

        var timeToStart = LocalDateTime.now(Clock.systemUTC())
        if (task is AzureThrottlerCacheableTask<A, P, T>) {
            LOG.debug("[$name] Trying to get data from cache for task $taskId, mode $flow.")
            val cacheValue = task.getFromCache(parameters)
            if (cacheValue != null) {
                if (isTimeToUpdate()) {
                    LOG.debug("[$name] Prefetching data for task $taskId.")
                    val subject = PublishSubject.create<AzureThrottlerAdapterResult<T>>()
                    val subscriptionList = SubscriptionList()
                    subscriptionList.add(
                            subject
                                    .doAfterTerminate {
                                        LOG.debug("[$name] Prefetching data for task $taskId done.")
                                        subscriptionList.clear()
                                    }
                                    .subscribe({}, {})
                    )
                    requestQueue.addRequest(
                            timeToStart,
                            parameters,
                            subject,
                            true,
                            true
                    )
                }
                LOG.debug("[$name] Returning value from cache for task $taskId, mode $flow.")
                return Single.just<AzureThrottlerAdapterResult<T>>(AzureThrottlerAdapterResult(cacheValue, null, true))
            }
        }

        LOG.debug("[$name] There is no data in cache for task $taskId, mode $flow. Adding request to queue. Time to start: $timeToStart")

        val subject = PublishSubject.create<AzureThrottlerAdapterResult<T>>()
        requestQueue.addRequest(
                timeToStart,
                parameters,
                subject,
                false,
                true)
        return subject.toSingle()
    }