How to know if a Worker has failed being triggered/executed by the queue?












0















I am appending workers in the queue as shown below. However, occasionally a worker won't be triggered. How can I know if a Worker did not get executed so that I can try to re-enqueue it? Maybe using the Operation instance returned by enqueue()? How to use it? If so, is it possible to have a hint of the reason of failure of triggering the Worker?



Note that I do not need to know if the Worker failed (return Result.FAILURE). The case I want to cover is when the Worker is not even called, which happens occasionally.



OneTimeWorkRequest sendMessageWorker = new OneTimeWorkRequest.Builder(SendMessageWorker.class)
.setInputData(data)
.setBackoffCriteria(BackoffPolicy.LINEAR, backOffDelayInMillis, TimeUnit.MILLISECONDS)
.build();

Operation operation = WorkManager.getInstance()
.beginUniqueWork(SendMessageWorker.class.getSimpleName(), ExistingWorkPolicy.APPEND, sendMessageWorker)
.enqueue();









share|improve this question





























    0















    I am appending workers in the queue as shown below. However, occasionally a worker won't be triggered. How can I know if a Worker did not get executed so that I can try to re-enqueue it? Maybe using the Operation instance returned by enqueue()? How to use it? If so, is it possible to have a hint of the reason of failure of triggering the Worker?



    Note that I do not need to know if the Worker failed (return Result.FAILURE). The case I want to cover is when the Worker is not even called, which happens occasionally.



    OneTimeWorkRequest sendMessageWorker = new OneTimeWorkRequest.Builder(SendMessageWorker.class)
    .setInputData(data)
    .setBackoffCriteria(BackoffPolicy.LINEAR, backOffDelayInMillis, TimeUnit.MILLISECONDS)
    .build();

    Operation operation = WorkManager.getInstance()
    .beginUniqueWork(SendMessageWorker.class.getSimpleName(), ExistingWorkPolicy.APPEND, sendMessageWorker)
    .enqueue();









    share|improve this question



























      0












      0








      0








      I am appending workers in the queue as shown below. However, occasionally a worker won't be triggered. How can I know if a Worker did not get executed so that I can try to re-enqueue it? Maybe using the Operation instance returned by enqueue()? How to use it? If so, is it possible to have a hint of the reason of failure of triggering the Worker?



      Note that I do not need to know if the Worker failed (return Result.FAILURE). The case I want to cover is when the Worker is not even called, which happens occasionally.



      OneTimeWorkRequest sendMessageWorker = new OneTimeWorkRequest.Builder(SendMessageWorker.class)
      .setInputData(data)
      .setBackoffCriteria(BackoffPolicy.LINEAR, backOffDelayInMillis, TimeUnit.MILLISECONDS)
      .build();

      Operation operation = WorkManager.getInstance()
      .beginUniqueWork(SendMessageWorker.class.getSimpleName(), ExistingWorkPolicy.APPEND, sendMessageWorker)
      .enqueue();









      share|improve this question
















      I am appending workers in the queue as shown below. However, occasionally a worker won't be triggered. How can I know if a Worker did not get executed so that I can try to re-enqueue it? Maybe using the Operation instance returned by enqueue()? How to use it? If so, is it possible to have a hint of the reason of failure of triggering the Worker?



      Note that I do not need to know if the Worker failed (return Result.FAILURE). The case I want to cover is when the Worker is not even called, which happens occasionally.



      OneTimeWorkRequest sendMessageWorker = new OneTimeWorkRequest.Builder(SendMessageWorker.class)
      .setInputData(data)
      .setBackoffCriteria(BackoffPolicy.LINEAR, backOffDelayInMillis, TimeUnit.MILLISECONDS)
      .build();

      Operation operation = WorkManager.getInstance()
      .beginUniqueWork(SendMessageWorker.class.getSimpleName(), ExistingWorkPolicy.APPEND, sendMessageWorker)
      .enqueue();






      android android-workmanager android-architecture-workmanager






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 21:00







      user2923322

















      asked Nov 14 '18 at 20:49









      user2923322user2923322

      13018




      13018
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Using ExistingWorkPolicy.APPEND, you´re creating a chain of work.

          If you return Result.FAILURE in your Worker (in case of an error), all the chain of Work is cancelled. This maybe the reason why you don´t see your worker running sometimes.



          One suggestion is to add a Tag or an Id to your worker and observe the status using a Livedata and the getStatusesByTagLivedata() or getStatusesById().



          You can take a look at this talk presented at the Android Developer Summit that covers this and other example.






          share|improve this answer
























          • I am sure it is not because of Result.FAILURE, because before returning such a result, I have error logging. Also all the logic of my worker is wrapped by a try/catch and if any error occurs I log and I can always see any error. It really seems that the worker, occasionally, is just not triggered at all by the queue (about 1 time on 100 times). I will watch the talk today! Thank you for the video

            – user2923322
            Nov 15 '18 at 9:01



















          0














          You can observe the Worker and get the state of worker by observing using method getStatusByIdLiveData



          mWorkManager.getStatusByIdLiveData(yourWorkRequest.id)
          .observe(lifeCycleOwner, Observer { workStatus ->
          //here you can get the status of work
          })





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308498%2fhow-to-know-if-a-worker-has-failed-being-triggered-executed-by-the-queue%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Using ExistingWorkPolicy.APPEND, you´re creating a chain of work.

            If you return Result.FAILURE in your Worker (in case of an error), all the chain of Work is cancelled. This maybe the reason why you don´t see your worker running sometimes.



            One suggestion is to add a Tag or an Id to your worker and observe the status using a Livedata and the getStatusesByTagLivedata() or getStatusesById().



            You can take a look at this talk presented at the Android Developer Summit that covers this and other example.






            share|improve this answer
























            • I am sure it is not because of Result.FAILURE, because before returning such a result, I have error logging. Also all the logic of my worker is wrapped by a try/catch and if any error occurs I log and I can always see any error. It really seems that the worker, occasionally, is just not triggered at all by the queue (about 1 time on 100 times). I will watch the talk today! Thank you for the video

              – user2923322
              Nov 15 '18 at 9:01
















            0














            Using ExistingWorkPolicy.APPEND, you´re creating a chain of work.

            If you return Result.FAILURE in your Worker (in case of an error), all the chain of Work is cancelled. This maybe the reason why you don´t see your worker running sometimes.



            One suggestion is to add a Tag or an Id to your worker and observe the status using a Livedata and the getStatusesByTagLivedata() or getStatusesById().



            You can take a look at this talk presented at the Android Developer Summit that covers this and other example.






            share|improve this answer
























            • I am sure it is not because of Result.FAILURE, because before returning such a result, I have error logging. Also all the logic of my worker is wrapped by a try/catch and if any error occurs I log and I can always see any error. It really seems that the worker, occasionally, is just not triggered at all by the queue (about 1 time on 100 times). I will watch the talk today! Thank you for the video

              – user2923322
              Nov 15 '18 at 9:01














            0












            0








            0







            Using ExistingWorkPolicy.APPEND, you´re creating a chain of work.

            If you return Result.FAILURE in your Worker (in case of an error), all the chain of Work is cancelled. This maybe the reason why you don´t see your worker running sometimes.



            One suggestion is to add a Tag or an Id to your worker and observe the status using a Livedata and the getStatusesByTagLivedata() or getStatusesById().



            You can take a look at this talk presented at the Android Developer Summit that covers this and other example.






            share|improve this answer













            Using ExistingWorkPolicy.APPEND, you´re creating a chain of work.

            If you return Result.FAILURE in your Worker (in case of an error), all the chain of Work is cancelled. This maybe the reason why you don´t see your worker running sometimes.



            One suggestion is to add a Tag or an Id to your worker and observe the status using a Livedata and the getStatusesByTagLivedata() or getStatusesById().



            You can take a look at this talk presented at the Android Developer Summit that covers this and other example.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 5:13









            pfmaggipfmaggi

            2,0001225




            2,0001225













            • I am sure it is not because of Result.FAILURE, because before returning such a result, I have error logging. Also all the logic of my worker is wrapped by a try/catch and if any error occurs I log and I can always see any error. It really seems that the worker, occasionally, is just not triggered at all by the queue (about 1 time on 100 times). I will watch the talk today! Thank you for the video

              – user2923322
              Nov 15 '18 at 9:01



















            • I am sure it is not because of Result.FAILURE, because before returning such a result, I have error logging. Also all the logic of my worker is wrapped by a try/catch and if any error occurs I log and I can always see any error. It really seems that the worker, occasionally, is just not triggered at all by the queue (about 1 time on 100 times). I will watch the talk today! Thank you for the video

              – user2923322
              Nov 15 '18 at 9:01

















            I am sure it is not because of Result.FAILURE, because before returning such a result, I have error logging. Also all the logic of my worker is wrapped by a try/catch and if any error occurs I log and I can always see any error. It really seems that the worker, occasionally, is just not triggered at all by the queue (about 1 time on 100 times). I will watch the talk today! Thank you for the video

            – user2923322
            Nov 15 '18 at 9:01





            I am sure it is not because of Result.FAILURE, because before returning such a result, I have error logging. Also all the logic of my worker is wrapped by a try/catch and if any error occurs I log and I can always see any error. It really seems that the worker, occasionally, is just not triggered at all by the queue (about 1 time on 100 times). I will watch the talk today! Thank you for the video

            – user2923322
            Nov 15 '18 at 9:01













            0














            You can observe the Worker and get the state of worker by observing using method getStatusByIdLiveData



            mWorkManager.getStatusByIdLiveData(yourWorkRequest.id)
            .observe(lifeCycleOwner, Observer { workStatus ->
            //here you can get the status of work
            })





            share|improve this answer






























              0














              You can observe the Worker and get the state of worker by observing using method getStatusByIdLiveData



              mWorkManager.getStatusByIdLiveData(yourWorkRequest.id)
              .observe(lifeCycleOwner, Observer { workStatus ->
              //here you can get the status of work
              })





              share|improve this answer




























                0












                0








                0







                You can observe the Worker and get the state of worker by observing using method getStatusByIdLiveData



                mWorkManager.getStatusByIdLiveData(yourWorkRequest.id)
                .observe(lifeCycleOwner, Observer { workStatus ->
                //here you can get the status of work
                })





                share|improve this answer















                You can observe the Worker and get the state of worker by observing using method getStatusByIdLiveData



                mWorkManager.getStatusByIdLiveData(yourWorkRequest.id)
                .observe(lifeCycleOwner, Observer { workStatus ->
                //here you can get the status of work
                })






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 '18 at 4:31

























                answered Nov 16 '18 at 4:03









                VishalVishal

                406210




                406210






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308498%2fhow-to-know-if-a-worker-has-failed-being-triggered-executed-by-the-queue%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini