0

I've a Worker in which i first want to apply FFMPEG command before uploading it to server. As Worker is already running in background so to keep the result on hold until file uploads I've used RxJava .blockingGet() method. But I'm unable to understand that how to execute FFmpeg command synchronously by anyway i.e. RxJava etc. One tip that I found is to use ListenableWorker but it's documentation says that it stops working after 10 minutes. So, i don't want to go with that solution. Following is the method of FFmpeg just like any other async method. How can i make it synchronous or integrate it with RxJava? Any ideas would be appreciable.

 ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
                    @Override
                    public void onFailure(String s) {
                    }

                    @Override
                    public void onSuccess(String s) {
                       uploadMediaItem(mediaUpload);
                    }

                    @Override
                    public void onProgress(String s) {
                    }

                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onFinish() {

                        // countDownLatch.countDown();

                    }
                });

This is the flow of my Worker:

  1. check pending post count in DB.
  2. Pick first post and check if it has pending media list to upload.
  3. Pick media recursively and check if editing is required on it or not.
  4. Apply FFmpeg editing and upload and delete from DB.
  5. Repeat the cycle until last entry in the DB.

Thanks

Usman Rana
  • 2,067
  • 1
  • 21
  • 32
  • RxWorker it's a child of ListenableWorker and it has the same 10 minutes task lenght limits. If you need to run a background task for more time, WorkManager may not be the right solution. It would be helpful to understand what are you trying to achieve here. – pfmaggi Mar 29 '19 at 22:50
  • Good point that any worker is child of ListenableWoker and hence having a limit of 10 minutes. I just want to upload Posts the flow is mentioned above. What is best solution for that as per your suggestion please? – Usman Rana Apr 01 '19 at 05:52
  • You can take a look at the [Background processing Guide](https://developer.android.com/guide/background/). In your particular case, Foreground Services look like a good fit. – pfmaggi Apr 01 '19 at 06:08
  • But services don't have constraints like 'Network Connected' etc. I want to run it only when network is available – Usman Rana Apr 01 '19 at 06:41

1 Answers1

0

If you wanna create a syncronous job you need to use the CountDownLatch class (there is a comment in your code).

CountDownLatch is a syncronization object that can be used in cases like this.

As for now there isn't a valid method to have sync workers.

Listenable workers is useful when you want to monitor the worker itself from your app using a Livedata that return useful information (e.g. the status). If I remember correctly the standard Worker class also descend from Listenable worker so you can use that.

In your case is useful to have two workers: the first apply a FFMPEG command, and the second worker that take the output of this command to do the network upload. Separating this two operations allows you to have more time for complete the two works (10 + 10).

In your case you can do something like this for the first worker:

 private final CountDownLatch syncLatch = new CountDownLatch(1);
 ...ctor
 doWork(){

    //your asyncronous call
    ...
    @Override
    public void onFinish() {
//you need to save error status into a onSuccess and onFailure
        syncLatch.countDown();

     }
    ...
    //end


    syncLatch.await();

    ... 
    //evaluate if there are errors
    ...
    //create output to pass to the next worker 
    Data outputData = ...
    //pass the result to second worker, remember that onfailure will stop all subsequent workers
    if(error==true)
    {

        return Result.failure(outputData);
    }else{
        return Result.success(outputData);
    }
}

For the second worker you can do the same according to your upload function behavihour to syncronize the call.

Hope this help.

Cheers.

TomD88
  • 721
  • 6
  • 11
  • Thanks for response. But I've tried LatchCount, it keeps Worker on hold but it ffmpeg doesn't work either and stays stuck. That's why i removed that approach. – Usman Rana Mar 29 '19 at 08:12
  • 1
    If I understand correctly your ffmpeg command takes a lot of time to be executed, so no methods (onSuccess,onFailure) aren't called and you remain blocked. Am I right? Check this link if can help you cuz is similar to your topic https://stackoverflow.com/questions/51756305/synchronous-or-asynchronous-rxjava-inside-the-worker-from-workmanager-component – TomD88 Mar 29 '19 at 08:43
  • How can this be used in my case. The flow is that: i- check pending post count in DB. ii- Pick first post and check if it has pending media list to upload. iii- Pick media recursively and check if editing is required on it or not. iv- Apply FFmpeg editing and upload and delete from DB. v- repeat the cycle until last entry in the DB. – Usman Rana Mar 29 '19 at 09:40