Запрос WorkManager OneTimeWork прекращает запуск задач при сбое процесса - PullRequest
0 голосов
/ 12 марта 2019

У меня есть загрузка данных с помощью workmanager.

Процесс идет так:

  • Задание отправлено на сервер.
  • Задание запускается и загружаются данные
  • Если задача не удалась, я повторяю задачу, используя опцию Worker Retry
  • Если все идет хорошо, я отмечаю задачу как выполненную.

Моя проблема заключается в том, что при сбое задачи несколько раз WorkManager прекращает выполнение задач, а все запросы остаются в очереди.

Это мой журнал процесса

03-11-2019 09:00:49; SyncWorker,
03-11-2019 09:00:45; SyncWorker: Work task: a8806533-1b3a-44ef-bdeb-8cfb11fe4e91 started processing
03-11-2019 09:00:45; SyncWorker: Data to JSON Deserialization
03-11-2019 09:00:45; SyncWorker: Json Data: {"params":{"action":"upsert","dbName":"dinanthnd_de_xss_441_dev","endpoint":"http:\/\/usfla0764\/dinanthnd_de\/xsales_replication_wcf_44x_sqlite\/sync","reference":"tegz: transactionalTable","rotCode":"tegz","servicesUrl":"http:\/\/dev.xsm.local\/dinanthnd_de\/xsales_services\/api\/sync\/","target":"xsmsync","winAuth":false},"payload":[{"timer":[{"tmrCode":"tegz00123","rotCode":"","cusCode":"0001545078","rotCustomerCode":"tegz","ttpCode":"tmcus","tmrDescription":"","tmrStartTime":"2019-03-07 16:36:27","tmrEndTime":"2019-03-07 16:36:30","tmrRouteType":"0","tmrCustomerType":"1","tmrGeneralType":"0","tmrDeleted":"0","lgnCode":"0016000053","tmrGPSCoordinate1":"4.751054607695377","tmrGPSCoordinate2":"-74.04848087994479","tmrSent":"0","tmrHold":"0"}]}]}
03-11-2019 09:00:45; SyncBroker: Getting Connection Parameters
03-11-2019 09:00:49; SyncBroker: Setting Connection
03-11-2019 09:00:49; SyncBroker: Connecting to Server url
03-11-2019 09:00:49; SyncBroker: Upload to Server Process Running
03-11-2019 09:00:49; SyncBroker: Exception Message: Unable to resolve host "dev.xsm.local": No address associated with hostname
03-11-2019 09:00:49; SyncBroker: StackTrace: java.net.UnknownHostException: Unable to resolve host "dev.xsm.local": No address associated with hostname 
03-11-2019 09:00:49; SyncWorker: Upload to Server Process Failed and will be retried.
03-11-2019 09:00:49; SyncWorker: Work task: a8806533-1b3a-44ef-bdeb-8cfb11fe4e91 Ended



03-11-2019 12:46:37; SyncService,
03-11-2019 12:46:37; SyncService: Someone Called SYNC.
03-11-2019 12:46:37; SyncService: SYNC PROCESS creating new task.
03-11-2019 12:46:37; SyncTasker: Data Recieved before creating task: {"params":{"action":"upsert","dbName":"dinanthnd_de_xss_441_dev","endpoint":"http://usfla0764/dinanthnd_de/xsales_replication_wcf_44x_sqlite/sync","reference":"tegz: gpsTrackingReference","rotCode":"tegz","servicesUrl":"url","target":"xsmsync","winAuth":false},"payload":[{"routeTracking":[{"jrnCode":"S0000747775","lgnCode":"0016000053","rotCode":"tegz","rtrDate":"2019-02-19 12:46:37","rtrGPSCoordinate1":"4.751263046832478","rtrGPSCoordinate2":"-74.04820321635722","rtrMultimedia":""}]}]}
03-11-2019 12:46:37; SyncTasker: Creating One Time Worker for SYNC_UPLOAD_WORKER
03-11-2019 12:46:37; SyncTasker: Job queued with id 91775d69-c4a5-49f5-a0ac-9b54fb906874
03-11-2019 12:46:37; SyncTasker: Creating task process finished

03-11-2019 12:47:27; SyncService,
03-11-2019 12:47:27; SyncService: Someone Called SYNC.
03-11-2019 12:47:27; SyncService: SYNC PROCESS creating new task.
03-11-2019 12:47:27; SyncTasker: Data Recieved before creating task: {"params":{"action":"upsert","dbName":"dinanthnd_de_xss_441_dev","endpoint":"http://usfla0764/dinanthnd_de/xsales_replication_wcf_44x_sqlite/sync","reference":"tegz: transactionalTable","rotCode":"tegz","servicesUrl":"url","target":"xsmsync","winAuth":false},"payload":[{"timer":[{"tmrCode":"tegz00104","rotCode":"","cusCode":"0001304948","rotCustomerCode":"tegz","ttpCode":"tmcus","tmrDescription":"","tmrStartTime":"2019-03-11 12:47:24","tmrEndTime":"2019-03-11 12:47:27","tmrRouteType":"0","tmrCustomerType":"1","tmrGeneralType":"0","tmrDeleted":"0","lgnCode":"0016000053","tmrGPSCoordinate1":"4.7511469","tmrGPSCoordinate2":"-74.0484048","tmrSent":"0","tmrHold":"0"}]},{"routeInfoTypeDetailUp":[{"rntID":"1","ritCode":"tegz00012","iftCode":"005","rntBoolValue":"","rntCharValue":"","rntMoneyValue":"","rntIntegerValue":"58688","rntDateValue":"","rntTableValue":""},{"rntID":"2","ritCode":"tegz00012","iftCode":"013","rntBoolValue":"","rntCharValue":"xhcf","rntMoneyValue":"","rntIntegerValue":"","rntDateValue":"","rntTableValue":""},{"rntID":"3","ritCode":"tegz00012","iftCode":"014","rntBoolValue":"","rntCharValue":"","rntMoneyValue":"","rntIntegerValue":"","rntDateValue":"","rntTableValue":"11"}]},{"routeInfoTypeUp":[{"ritCode":"tegz00012","rotCode":"tegz","ritSystemDate":"2019-03-11 12:47:15","ritProcess":"0","lgnCode":"0016000053","ritGPSCoordinate1":"4.7511469","ritGPSCoordinate2":"-74.0484048","ritSent":"0","ritHold":"0"}]}]}
03-11-2019 12:47:27; SyncTasker: Creating One Time Worker for SYNC_UPLOAD_WORKER
03-11-2019 12:47:27; SyncTasker: Job queued with id ee4b6ab6-2ba9-45ee-8355-a42a8f3c926d
03-11-2019 12:47:27; SyncTasker: Creating task process finished

03-11-2019 12:47:38; SyncService,
03-11-2019 12:47:38; SyncService: Someone Called SYNC.
03-11-2019 12:47:38; SyncService: SYNC PROCESS creating new task.
03-11-2019 12:47:38; SyncTasker: Data Recieved before creating task: {"params":{"action":"upsert","dbName":"dinanthnd_de_xss_441_dev","endpoint":"http://usfla0764/dinanthnd_de/xsales_replication_wcf_44x_sqlite/sync","reference":"tegz: transactionalTable","rotCode":"tegz","servicesUrl":"url","target":"xsmsync","winAuth":false},"payload":[{"timer":[{"tmrCode":"tegz00105","rotCode":"","cusCode":"0001545067","rotCustomerCode":"tegz","ttpCode":"tmcus","tmrDescription":"","tmrStartTime":"2019-03-11 12:47:34","tmrEndTime":"2019-03-11 12:47:38","tmrRouteType":"0","tmrCustomerType":"1","tmrGeneralType":"0","tmrDeleted":"0","lgnCode":"0016000053","tmrGPSCoordinate1":"4.7511469","tmrGPSCoordinate2":"-74.0484048","tmrSent":"0","tmrHold":"0"}]}]}
03-11-2019 12:47:38; SyncTasker: Creating One Time Worker for SYNC_UPLOAD_WORKER
03-11-2019 12:47:38; SyncTasker: Job queued with id 07155377-905e-4b2e-95ac-86612c927c99
03-11-2019 12:47:38; SyncTasker: Creating task process finished

03-11-2019 12:47:41; SyncService,
03-11-2019 12:47:41; SyncService: Someone Called SYNC.
03-11-2019 12:47:41; SyncService: SYNC PROCESS creating new task.
03-11-2019 12:47:41; SyncTasker: Data Recieved before creating task: {"params":{"action":"upsert","dbName":"dinanthnd_de_xss_441_dev","endpoint":"http://usfla0764/dinanthnd_de/xsales_replication_wcf_44x_sqlite/sync","reference":"tegz: gpsTrackingReference","rotCode":"tegz","servicesUrl":"url","target":"xsmsync","winAuth":false},"payload":[{"routeTracking":[{"jrnCode":"S0000747775","lgnCode":"0016000053","rotCode":"tegz","rtrDate":"2019-02-19 12:47:41","rtrGPSCoordinate1":"4.751181792387373","rtrGPSCoordinate2":"-74.0483747230906","rtrMultimedia":""}]}]}
03-11-2019 12:47:41; SyncTasker: Creating One Time Worker for SYNC_UPLOAD_WORKER
03-11-2019 12:47:41; SyncTasker: Job queued with id 3a17e248-fbbe-400a-80fd-821c0cf2f6f3
03-11-2019 12:47:41; SyncTasker: Creating task process finished

03-11-2019 12:47:49; SyncService,
03-11-2019 12:47:49; SyncService: Someone Called SYNC.
03-11-2019 12:47:49; SyncService: SYNC PROCESS creating new task.
03-11-2019 12:47:49; SyncTasker: Data Recieved before creating task: {"params":{"action":"upsert","dbName":"dinanthnd_de_xss_441_dev","endpoint":"http://usfla0764/dinanthnd_de/xsales_replication_wcf_44x_sqlite/sync","reference":"tegz: transactionalTable","rotCode":"tegz","servicesUrl":"url","target":"xsmsync","winAuth":false},"payload":[{"timer":[{"tmrCode":"tegz00106","rotCode":"","cusCode":"0001304980","rotCustomerCode":"tegz","ttpCode":"tmcus","tmrDescription":"","tmrStartTime":"2019-03-11 12:47:44","tmrEndTime":"2019-03-11 12:47:48","tmrRouteType":"0","tmrCustomerType":"1","tmrGeneralType":"0","tmrDeleted":"0","lgnCode":"0016000053","tmrGPSCoordinate1":"4.7511469","tmrGPSCoordinate2":"-74.0484048","tmrSent":"0","tmrHold":"0"}]}]}
03-11-2019 12:47:49; SyncTasker: Creating One Time Worker for SYNC_UPLOAD_WORKER
03-11-2019 12:47:49; SyncTasker: Job queued with id 8e387e8a-0f94-443b-913f-da0071607474
03-11-2019 12:47:49; SyncTasker: Creating task process finished

03-11-2019 12:48:41; SyncService,
03-11-2019 12:48:41; SyncService: Someone Called SYNC.
03-11-2019 12:48:41; SyncService: SYNC PROCESS creating new task.
03-11-2019 12:48:41; SyncTasker: Data Recieved before creating task: {"params":{"action":"upsert","dbName":"dinanthnd_de_xss_441_dev","endpoint":"http://usfla0764/dinanthnd_de/xsales_replication_wcf_44x_sqlite/sync","reference":"tegz: gpsTrackingReference","rotCode":"tegz","servicesUrl":"url","target":"xsmsync","winAuth":false},"payload":[{"routeTracking":[{"jrnCode":"S0000747775","lgnCode":"0016000053","rotCode":"tegz","rtrDate":"2019-02-19 12:48:41","rtrGPSCoordinate1":"4.751491633935309","rtrGPSCoordinate2":"-74.04813616862626","rtrMultimedia":""}]}]}
03-11-2019 12:48:41; SyncTasker: Creating One Time Worker for SYNC_UPLOAD_WORKER
03-11-2019 12:48:41; SyncTasker: Job queued with id 97bced4d-04cd-40e3-9f8d-cfcc387b0b22
03-11-2019 12:48:41; SyncTasker: Creating task process finished

03-11-2019 12:49:41; SyncService,
03-11-2019 12:49:41; SyncService: Someone Called SYNC.
03-11-2019 12:49:41; SyncService: SYNC PROCESS creating new task.
03-11-2019 12:49:41; SyncTasker: Data Recieved before creating task: {"params":{"action":"upsert","dbName":"dinanthnd_de_xss_441_dev","endpoint":"http://usfla0764/dinanthnd_de/xsales_replication_wcf_44x_sqlite/sync","reference":"tegz: gpsTrackingReference","rotCode":"tegz","servicesUrl":"url","target":"xsmsync","winAuth":false},"payload":[{"routeTracking":[{"jrnCode":"S0000747775","lgnCode":"0016000053","rotCode":"tegz","rtrDate":"2019-02-19 12:49:41","rtrGPSCoordinate1":"4.751241480374802","rtrGPSCoordinate2":"-74.04829571578559","rtrMultimedia":""}]}]}
03-11-2019 12:49:41; SyncTasker: Creating One Time Worker for SYNC_UPLOAD_WORKER
03-11-2019 12:49:41; SyncTasker: Job queued with id f72caeae-0368-4f89-aeab-4c843d8149ae
03-11-2019 12:49:41; SyncTasker: Creating task process finished

Это моя реализация OneTimeWorkRequest

 implementation "android.arch.work:work-runtime-ktx:1.0.0-rc02"

   val constraints = Constraints.Builder()
                    .setRequiredNetworkType(NetworkType.CONNECTED)
                    //.setRequiresBatteryNotLow(true)
                    .build()
 val work = OneTimeWorkRequest.Builder(SyncWorker::class.java)
                    .setConstraints(constraints)
                    .setInitialDelay(10, TimeUnit.SECONDS)
                    .setInputData(inputData)
                    .addTag(workTag)
                    .build()

  WorkManager.getInstance().enqueue(work)

Рабочая реализация

class SyncWorker(context : Context, params : WorkerParameters) : Worker(context, params) {
    private val LOG_NAME = this.javaClass.simpleName

    override fun doWork(): Result {
        val logger = Logger()
        logger.appendLogToStartProcess(LOG_NAME,"Work task: $id started processing")
        var result : Result
        var tagIsRunning = false
        try {
            result = if(!tagIsRunning){
                val syncData = inputData.getString(SyncGlobals.SYNC_DATA)
                logger.appendLogToContinueProcess(LOG_NAME,"Data to JSON Deserialization")
                val json = JSONObject(syncData)
                logger.appendLogToContinueProcess(LOG_NAME,"Json Data: $json")
                val runWork = SyncBroker(logger).uploadSync(json,syncData!!)
                if (runWork.taskCompleted) {
                    logger.appendLogToContinueProcess(LOG_NAME,"Upload to Server Process Done.")
                    Result.success()
                } else {
                    if(runWork.taskRetry){
                        logger.appendLogToFinishProcess(LOG_NAME,"Upload to Server Process Failed and will be retried.")
                        Result.retry()
                    }else{
                        logger.appendLogToFinishProcess(LOG_NAME,"Upload to Server Process Failed and will be canceled.")
                        Result.failure()
                    }
                }
            }else{
                Result.failure()
            }

        }catch (jsonE: JSONException){
            logger.appendLogToExceptionProcess(LOG_NAME, jsonE)
            logger.appendLogToContinueProcess(LOG_NAME,"Upload to Server Process Failed and will be canceled.")
            result = Result.failure()
        }catch (e: Exception){
            logger.appendLogToExceptionProcess(LOG_NAME, e)
            logger.appendLogToContinueProcess(LOG_NAME,"Upload to Server Process Failed and will be canceled.")
            result = Result.failure()
        }finally {
            logger.appendLogToFinishProcess(LOG_NAME,"Work task: $id Ended")
            Logger.writeLog(LOG_NAME,logger.returnBuilderLogMessage())
        }

        return result
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...