Work Manager with periodic request called multiple times
Asked Answered
E

1

11

I'm using work manager for periodic task. I've to execute single instance of worker

my code is below

  val workManager = WorkManager.getInstance()
  val callDataRequest = PeriodicWorkRequest.Builder(MyLoggerWork::class.java,
                15, TimeUnit.MINUTES)
                .addTag(worker)
                .build()
   workManager.enqueueUniquePeriodicWork(worker, ExistingPeriodicWorkPolicy.KEEP, callDataRequest)

and my worker logs as follows

18/09/2018 03:18:19
18/09/2018 03:18:19
18/09/2018 03:18:19
18/09/2018 03:18:19

18/09/2018 03:37:18
18/09/2018 03:37:18
18/09/2018 03:37:18
18/09/2018 03:37:18

here is my MyLoggerWork class

public class MyLoggerWork: Worker(){


override fun doWork(): Result {
    addlog()
    return Worker.Result.SUCCESS
}

private fun addlog() {

    try {

        val directory = File(Environment.getExternalStorageDirectory().path + "/", "Jobs")
        if (!directory.exists()) {
            directory.mkdir()
        }
        val file = File(directory.path, "jobs_.txt")
        if (!file.exists() && directory.exists()) {
            file.createNewFile()
        }
        try {
            val text = SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(Date())
            val stream = FileOutputStream(file, true)
            stream.write("\r\n".toByteArray())
            stream.write(text.toByteArray())
            stream.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }

    } catch (e: IOException) {
        e.printStackTrace()
    }
}
}

i am using following dependency

 implementation 'android.arch.work:work-runtime:1.0.0-alpha08'

why my work gets called 4 times???

Elanaeland answered 18/9, 2018 at 10:18 Comment(13)
Hi, please show your MyLoggerWork class and place where you write this code. Also please specify version of WorkManager. I faced similar issue some time ago. But it was issue in my code, after I fixed it the issue has gone.Metric
I am experiencing a similar issue. However, in my case, my Worker class runs twice, even though I only run it once. I tested it with printing Logs. I put a log for when I enqueue the Worker, and a log at the start of doWork(). The log in doWork() gets printed twice.Figone
Have you solved this issue? I have a same problem - my worker called about 10 times at one moment.. I'm using 'alpha10' versionCollard
In my case I set 15 minute time for test and then first 3 times worker class called twice then only once I'm using 1.0.0-alpha11. Any solution ? And I have another Question ? please look It and tell me something about it. please.Proparoxytone
In my case the result varies, all between one (1) and 10 calls on initial start. It seems som kind of race condition. I am using a Samsung S5 Active with API 21 (5.01) as testdevice. Will test more on emulator soon.Gale
It seems that the timer also is arbitrary, sometimes it aims well at 15 minutes, but sometimes it triggers on 4 minutes with several subsequent calls. Is something not cleaning up the scheduler here ? I am using 1.0.0-alpha11Gale
Has anyone considering reporting this on issuetracker.google.com ?Gale
@RoarGrønmo no, I haven't report about this. Worker is in alpha, so I think 'report' will not make any senseCollard
@Collard you are completely wrong. Alpha means that you can help devs in testing via finding and reporting bugs, therefore make project more stable. Btw i have same issue with multiple runs and fill corresponding issue issuetracker.google.com/issues/119886476Schoenfeld
Here is my issuetracker reference. There is i remedy in there. It's dirty, but it works though. issuetracker.google.com/issues/119582502Gale
I am facing the same problem. I have two different periodic tasks in my app and the problem happens only on one of the two. The difference is that that one that does not have the problem does not have any constraints and the backoff criteria is set as the following: .setBackoffCriteria(BackoffPolicy.LINEAR,0, TimeUnit.MINUTES). The job that has the problem requires internet connection and the backoff criteria are set as .setBackoffCriteria(BackoffPolicy.LINEAR, WorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS). This should be what is causing the problem.Proudfoot
I'm having this problem with version androidx.work:work-runtime:2.3.4Fruity
Same here with androidx.work:work-runtime:2.3.4. It got called 46 times in two seconds.Chuckwalla
H
1

You are using a very old version of WorkManager. You should switch to the latest stable version. You can try using 1.0.1 or better yet, switch to 2.0.1 which includes androidx dependencies.

Hillinck answered 26/5, 2019 at 15:45 Comment(1)
thank you, Rahul, I asked this question 8 months ago, that time 2.0.1 wasn't availableElanaeland

© 2022 - 2024 — McMap. All rights reserved.