CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Exiting on failure for cron file  (Read 45 times)

durangod

  • Full Member
  • ***
  • Posts: 157
  • Karma: +2/-0
  • Weebles Wobble - but they dont fall down!
Exiting on failure for cron file
« on: 24 Aug 2023, 07:21:03 am »
Hi,

I am creating a cron file to maintain my users online table.  It is written to be executed only by the server.

If someone tries to run the cron from the browser here is what i do..

Code: [Select]
  http_response_code(403); 
   exit;

What i was wondering if should i use a message output for debugging purposes like so...

Code: [Select]
http_response_code(403); 
 die('Forbidden');

I dont think i need any output, i know the cron doesnt...    is the output better to use than not to use?

Anyway just FYI here is how in my mind this update of the online table will work.   

The online table has the users id and timestamp, 
The user table record has an activity stamp that changes as the user moves throught the site.   
Then the cron is run every 10 min and compares the two values

If the stamp in the users table is greater than the stamp in the online table, this means the user is still active and the online table stamp is updated

If the stamp in the users table is less than the online table then the user has left the site and the online table  data is cleared until they log in again. 

Does that sound about right?

« Last Edit: 24 Aug 2023, 07:55:10 am by durangod »

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 704
  • Karma: +8/-0
    • GrumpyYoungMan
Re: Exiting on failure for cron file
« Reply #1 on: 24 Aug 2023, 09:44:34 am »
Can you just not silently run a bash script? and not not a PHP one?
« Last Edit: 24 Aug 2023, 01:31:37 pm by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

durangod

  • Full Member
  • ***
  • Posts: 157
  • Karma: +2/-0
  • Weebles Wobble - but they dont fall down!
Re: Exiting on failure for cron file
« Reply #2 on: 24 Aug 2023, 01:10:59 pm »
if this was a personal private script sure.  But its going to masses so that means all of them would need shell_exec enabled n thats not going to happen with shared hosts.

Some hosts make exception for something like FFMPEG but very few.  So it has to be cron.  :)

durangod

  • Full Member
  • ***
  • Posts: 157
  • Karma: +2/-0
  • Weebles Wobble - but they dont fall down!
Re: Exiting on failure for cron file
« Reply #3 on: 24 Aug 2023, 05:46:09 pm »
Here is my take on the cron to update the online table or users table when users leave without logoff or are online for awhile.  I figure the cron should probably run 2x per hour

Have a look and see if you can think of anything i missed, something that might cause a loop or stall, i think i covered the bases.

All names have been changed for security.

So we have the users table and the online table in the comparrison

Code: [Select]

//get all users in the online table
$u_table = $db->onlineUsers(); //everyone in the online table

if(is_array($u_table) && !empty($u_table))
{
   
   foreach($u_table as $ukey => $uval)
   {
     
  //u_ut is  user table
      $u_ut = $db->LastLogin( (int) $uval['id']); //last login info from users table
     
      $userid = (int) $uval['id']; //user id
       
       
      //users table info
      $logindate  =  clean($ud_ut['login_date']);
      $loginstamp =  clean($ud_ut['login_stamp']);
      $ip     =  clean($ud_ut['ip']);  //the ip is only for passing data to update the record - or code a new function
     
      //online table info
      $ol_stamp = clean($uval['stamp']);
      $ol_date = clean($uval['date']);
     
     
      //if users table data GT online tables data
      //user is still online - update online table
     
      if( (int) $loginstamp > (int) $ol_stamp )
      {
         
        //update the online table
         
        $data = [
                    'stamp'    =>   $loginstamp,
                    'date'     =>   $logindate
                   ];
         
        $update_online_table  = $db->UserLogTable( (int) $userid, $data);
         
         
      }else{
         
              //user has left without logging off
         
              //update the users table
         
              $data = [
                          'login_stamp'   =>   $ol_stamp,
                          'login_date'     =>   $ol_date,
                          'ip'    =>   $ip
                         ];
         
               $update_users_table = $db->rUpdateUsersTable( (int) $userid, $data);
             
             
              //remove user from online table
  //they are no longer here so remove them from the online table
              $remove = $db->removeFromOnlineTable( (int) $userid);
             
 
  //this should not invoke unless there is a problem
  //because it only has acess if there is a record
  //so if it fails then something is actually wrong
 
              if(!$remove)
              {
               
                error_log("Update Login Table Cron Error", 0); 
                 
              }
             
         
           }//close else
           
   }//close foreach
 
 //echo "finished"; for testing
 exit;
   
}//close if array utable
« Last Edit: 24 Aug 2023, 05:48:41 pm by durangod »

 

SMF spam blocked by CleanTalk

Advertisement