PHP API /ppp active remove user after few seconds ASYNC

Current situation .

PPPUSER1 opens a webpage → webpage sends “/ppp active remove [find where name=PPPUSER1]” in middle of the page and do some stuff → webpage show some messages to user

Right now the PPPUSER1 could not see the messages because disconnected before page completely loads .

I want a way in PHP API for this situation .

PPPUSER1 opens a webpage → webpage sends “/ppp active remove [find where name=PPPUSER1]” in middle of the page and do some stuff but user will be disconnected after few seconds → webpage show some messages to user → then users really disconnects

The safest way is to add a scheduler script that will do the removal. Set it to run just two seconds from the router’s current clock, and do the removal within that script.

Why two seconds and not just one? To address the scenario where you’ve fetched the clock just a microsecond before it’s the next second already, and thus the scheduler script, added that microsecond later, never executes, because the time has already passed.

It should go without saying that you should only add that scheduler script after you’ve already echoed everything the user should see, and are just about to end the PHP request.

Is it possible to do it with your other post http://forum.mikrotik.com/t/passing-to-paramater-to-script-via-api/73970/2 ?

Main problem is that . The script is MVC . The code which removes ppp user runs in controller but the message run shows in view which is after controller and the code it complex enough not to edit everything .

Is there a way that i say to router “Hey router please remove this user after 5 second , Bye” without need to wait for answer (async) then show all messages and router removes that user ?

Thanks

Sure, you could use that to more easily add the scheduler script. I mean, with that approach, you only need

$util->exec(
    '/system scheduler add name="API_REMOVE_$user" interval=5s on-event={
        /ppp active remove [find where name=$user]
        /system scheduler remove "API_REMOVE_$user"
    }',
    array(
        'user' => 'PPPUSER1'
    )
); 



Just set the scheduler script to 5 seconds afterwards instead of only 2 then (as is already done above, for your convinience). However more time you need for the view to finish and echo everything, plus an extra second “to be safe”.

When you add a scheduler script, the only answer the API is giving you is whether the addition of the scheduler script was a success - you can’t know (at least not directly) whether that script itself ran without errors, even if you wanted to. In fact, if you’re using exec(), you won’t even know whether the scheduler script was added successfully - you’ll just know if the code you had inside exec() was executed at all or not (e.g. if you had permission issues).

(P.S. The above also shows a more stable approach with regards to the scheduler script not missing its appointment that I only remembered is possible now - using an interval, instead of the start-time; Even if the cue is missed, which in this case will in fact be by definition, the script will start a few seconds later, and remove itself to prevent further executions)

Thanks a lot . like always your answer was best .
+1 karma added :wink: