PHP script continuing to run after page close [initiated from AJAX] -
everything google tells me should not happening, is.
i'm building migration tool build 'master database'. have admin panel, accessible few select people. there merge button starts ajax call run migration php function. i'm not positive how long script takes considering i'm still developing none less i'm expecting minimum of 20 minutes once pushed production , populated production database. not need lecture on best practices telling me not via gui. become cron well, want able induce manually, if admin desires.
so here's process. migration function closes session session_write_close() allowing me run multiple php scripts simultaneously. because start setinterval checks see session variable. 'progress' int on loop iteration i'm on. in migration script open sessions, add 1 int, , close sessions again. @ end of each loop.
by doing have created progress ajax. noticed something. if start migration, close out of tab - or refresh. once reload page progress continues grow. tells me migration script still executing in background.
if close 100% out of browser, or clear sessions no longer see progress go up. not because script stops. because progress indication relies on sessions , once clear sessions or close out browser session cookie changes. know script still running because can query database manually , see entries being added.
now question: not want this. if browser closes, if press refresh, if loose connection, etc want script terminated. want stop mid process.
i tried ignore_user_abort(false); i'm pretty sure specific command line , made no difference me.
i want terminated because i'm building 'progress resume' function can choose resume migration progress again.
any suggestions?
update:
i didn't want go route solution though of have session variable. , it's 'last time client validated' timestamp. in javascript, on client side, every 30 seconds hit php script 'update last time client validated'. , in migration function @ beginning of each loop check make sure timestamp isn't 60 seconds old example. if 60 seconds old, or older, die stopping script. locally mean 'if there no client updating timestamp can assume user closed out of browser/tab/refreshed'. , function can ignore check if in command line (cron). not ideal solution plan b
i am, , did, go solution ping client indicate if client still alive or not.
so did:
from client, in javascript, set setinterval run every 1.5 seconds , hits php script via ajax. php script updates session variable current timestamp (this database value if needed to, didn't want overhead of query).
$_session['migration_listsync_clientlastping_'.$decodedjson['progresskey']] = time();
then, inside migration function run check see if 'timestamp' on 10 seconds old, , if die - killing script.
if(isset($_session['migration_listsync_clientlastping_'.$progresskey])){ $calc = time() - $_session['migration_listsync_clientlastping_'.$progresskey]; if($calc > 10){ die(); } } i added 'progresskey' param random number 1-100 generated when function called. number generated in javascript , passed both of ajax calls. way if user refreshes page , pressed button again won't have 2 instances of function running. 'old' instance die after few seconds , new instance take over.
this isn't ideal solution effective one.
Comments
Post a Comment