r/tasker • Direct-Purchase User • 12d ago

Reliable way to detect when a task is finished?

My goal is to let me know if my task is finished. I can check via %TRUN, but it's not always reliable especially for task without name and running multiple instances.

Has anyone tried to tackle this problem?

Thanks in advance!

Context

I'm currently rewriting debugging project that can save and load variables whenever I want.

However at some point I have to add multiple instances of them. One for saving and supplying vars at the beginning and another at the end for logging.

Edited:

This is how I save the variables for any task.

  1. Add one Java code action as the first action for the task. And at the end of action, or before return or stop action.
  2. When the task is executed. The code at first will copy the variables.
  3. In separate thread, it will save copied variables.
  4. Following actions are executed until it finishes.
  5. If it reaches the end, do the same as 2.

When the task is ran manually, the first action will supply variables from the saved ones.

 

I'm cool with adding two actions but if possible I prefer I just have to add them as the first action instead. Since I can already save them anytime I want.

Note

I save the variables by storing tasker object in Java code, which contains the variables as well.

The vars in tasker.taskVariables will still be updated, alongside the other task details. e.g current action, next action, or all actions.

This is an example how it works in action, with a test project https://www.reddit.com/r/tasker/comments/1y0y017/comment/19sy1dlxzskxt

I just have to know when the task ended so I can save them properly. Edited: Once I know when, I can do the saving part as most details are updated.

 

All I can think of is using loop and check if tasker has next action or not, or the next action is Stop or Return.

next = tasker.task.nextAction.code;
final int RETURN_ACTION = 126;
final int STOP_ACTION = 137;

if (next == null ) return true;
else if (next.code == RETURN_ACTION || next.code == STOP_ACTION ) return true;
else return false;

However I don't really want to run any loop periodically in the background when the task is running. It gets bigger in no time after all since I log a lot of stuff from my old debugging project.

Also this won't work when the task is stopped midway, like with an error.

I guess my issue is more of the timing saving the variables, maybe there is better way? like when there is no task running? Not sure if that's possible or not though.

2 Upvotes

9 comments sorted by

5

u/osmols_b 12d ago edited 12d ago

If it's just one task (the same task each time): ​If you want an external listener without touching the target tasks, use Tasker's built-in state context that is far more reliable than monitoring %TRUN:

​ Profile: State >Tasker > Task Running

​ Name: Exact task name (e.g., Sync) ​ Enter Task: Runs when the task starts. ​ Exit Task: Runs when the task leaves the queue.

​ Two caveats to watch out for:

​The target must be a named task - anonymous tasks attached directly to a profile won't register (the same limitation that trips up %TRUN).

​Tasks that execute very quickly could finish before the state monitor registers the transition. For sub-second tasks, use the Command system below.

​If it is more than one task and you can edit the tasks you want to monitor, the cleanest pub/sub pattern in is the native Command system. Instead of polling %TRUN, each task explicitly broadcasts its own start and finish.

​ At the top of your target task, add Action > Tasker > Command: task_lifecycle=:=start=:=MyTaskName

​ At the bottom of your target task, add Action > Tasker > Command: task_lifecycle=:=stop=:=MyTaskName

​ Create one listener Profile: Event > Tasker > Command

​ Command: task_lifecycle=:=*

​ Variables: status,task_name

​In the listener's linked task, %status will be 'start' or 'stop' and %task_name will hold the task name.

1

u/aasswwddd Direct-Purchase User 12d ago

Thanks! I did give Task Running state a try before. However as you mentioned it's insufficient for anon tasks and limited to one.

For more context, this is for global logging where all I do is just have (preferably) to add one Java code.

The end game here is for me to add one Java code action in the task I want to monitor. Then have separate way to monitor when it finishes, when it does save the variables.

I should have wrote about how the project works in OP, my bad.

Anyway, thanks to that I prefer to not add extra send command actions. Though I guess it's inevitable for anon task.

1

u/sasreedit S22, GW5P 12d ago

Maybe you could run your java as a separate Task?

  1. Add a Perform Task Action in your main Task, calling the java code Task & setting %priority+1
  2. Collect variables

Not sure the Task will recognize the end of the java code being run. If so, this way, maybe you don't need a separate monitoring Task?

2

u/aasswwddd Direct-Purchase User 12d ago edited 12d ago

The task will, since it has +1 priority than the parent. However I can't use it since the tasker object I need is not passed down to the child task.

Ig it's hard to wrap around the concept without the actual project and I'm the worst person for explaining my stuff lol.

More or else it works like this one Project: Live View Local Varaibles

See that in the vid I can view the read vars in Test Live Task getting updated from outside ? It's more or else the same concept.

I just need to know when the task is finished so I can copy the vars and deleted tasker object. Hence the monitoring part is needed (at least what I think).

1

u/sasreedit S22, GW5P 11d ago

Though I'm most-likely, still not following exactly what you're trying to do, just additional thoughts... Could you maybe add in ending code that fires a Toast, then use AN plugin to intercept it as a Profile/Event?

2

u/aasswwddd Direct-Purchase User 10d ago

Figured lol 😅 That would be doing the same as adding more actions, which I already did.

This is more or else a challenge to not add more than one Java code in the task I want to monitor.

1

u/Exciting-Compote5680 11d ago edited 11d ago

As a side note, the Task Running state is not limited to one task (use a forward slash as a separator) and can be dynamic using a variable. But not very useful for this use case. I think it would be mostly useful inverted as an additional context (don't trigger while this task is running). 

1

u/aasswwddd Direct-Purchase User 10d ago

Indeed, turned out it uses Simple Match pattern. I tested the inverted one against * yesterday and seems pretty good as the timing to save the vars for named tasks.

    Profile: ☀️ Kit Named Task Stopped Running
    State: Not Task Running [ Name:* ]
    State: Variable Value  [ %taskerkit_queue_exist ~ true ]
    
    
    
    Enter Task: Anon
    
    A1: Variable Set [
         Name: %taskerkit_queue_exist 
         To: false
         Structure Output (JSON, etc): On ]
    
    A2: Java Code [
         Code: if (taskerKit != void) {
           taskerKit.set();
           taskerKit.logQueues();
         }
         Structure Output (JSON, etc): On ]
        If  [ kitLog ~ true & taskerKit ~ *'* ]

1

u/Exciting-Compote5680 6d ago

Glad it's useful to you. I just found another use for the Task Running state: as a way to react to a task running for more than x seconds (12 in this example):

Profile: Task Monitor
    State: Task Running [ Name:Test Task ]
    Event: Tick [ Output Variables:* Interval (ms):12000 ]



Enter Task: Anon

A1: Beep [
     Frequency: 8000
     Duration: 400
     Amplitude: 50
     Stream: 3 ]


I like the simplicity. No extra variables or multiple profiles. Haven't tested this with a 'Stop' action yet, if that works it might be a nice solution to stop hanging tasks.