Chat No Sockets Cron Tutorial

Chat No Sockets Cron Tutorial

Chat No Sockets Cron Tutorial

Yesterday’s Chat No Sockets Primer Tutorial had a “command line” PHP usage component, we wonder whether you noticed? We intend to keep this arrangement for our “no sockets” Chat web application. It will not function as that “Chat” without the command line part of the “equation” being activated. You might think of it as the “traffic cop” of the web application.

As far as that “command line” PHP usage goes …

  • Isn’t it great to have the one code source for all this?
  • How are we going to manage this command line usage, out of …
    1. interactive in an interactive command line session
    2. set off a background process run of it via the “&” suffix
    3. crontab it (on our Linux web server)

    ?

Suggestion 1 is kludgy, over the top and awkward to arrange for any long period of time, though useful if non-continuous “process coverage” is the go.

Suggestion 2 and 3 are great for “continuous process coverage” (we privately think of as “jigsaw coverage”), but in our books (and the pamphlettes have scarpered it to Pamphlette Island … which they must intend on “founding”?!) crontab is a better option to take for at least two reasons …

  • crontab resurrects itself on a system reboot
  • crontab is self documenting (an important advantage regarding command line processing usage)

… but if we are to use crontab “jigsaw coverage”, to avoid “jigsaw overlap” we’re going to have to change … regarding …

  ┌────────── minute (0 – 59)
   │ ┌──────── hour (0 – 23)
   │ │ ┌────── day of month (1 – 31)
   │ │ │ ┌──── month (1 – 12)
   │ │ │ │ ┌── day of week (0 – 6 => Sunday – Saturday, or
   │ │ │ │ │ 1 – 7 => Monday – Sunday)
   ↓ ↓ ↓ ↓ ↓
   * * * * * command to be executed

… thanks to https://stackoverflow.com/questions/18919151/crontab-day-of-the-week-syntax for above crontab syntax … as per ( our assumption being that the user will use one of the “every ? units” syntax such as
*/6 * * * * php this_php_happens_every_six_minutes.php
) …

To <— <— From

if ($argv) { // command line ...
$cfindings="";
$goes=0;
$howmanygoes="-1";
$par=getenv("TERM"); // thanks to https://stackoverflow.com/questions/3214935/can-a-bash-script-tell-if-its-being-run-via-cron
if ("$par" == "$TERM") { // via cron
$cfindings1=exec("crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 1");
$cfindings2=exec("crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 2");
$cfindings3=exec("crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 3");
$cfindings4=exec("crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 4");
$cfindings5=exec("crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 5");
if (strpos($cfindings1, "*/") !== false) {
$howmanygoes=12 * explode("*/", $cfindings1)[1];
} else if (strpos($cfindings2, "*/") !== false) {
$howmanygoes=720 * explode("*/", $cfindings2)[1];
} else if (strpos($cfindings3, "*/") !== false) {
$howmanygoes=17280 * explode("*/", $cfindings3)[1];
}
}
while (1) {
foreach (glob("chat_*.rls") as $ourfilename) {
rename($ourfilename, explode(".rls", $ourfilename)[0] . ".line");
}
if ($howmanygoes >= 0) { $goes++; if ($goes >= $howmanygoes) { exit; } }
sleep(5);
}
exit;
}

if ($argv) { // command line ...
while (1) {
foreach (glob("chat_*.rls") as $ourfilename) {
rename($ourfilename, explode(".rls", $ourfilename)[0] . ".line");
}
sleep(5);
}
exit;
}

Again, feel free to try the changed php_listener.php‘s chat web application “second draft”.


Previous relevant Chat No Sockets Primer Tutorial is shown below.

Chat No Sockets Primer Tutorial

Chat No Sockets Primer Tutorial

We’re starting on a new PHP web application project. We’ve got a “first draft” of a chat web application that does not use sockets, but rather …

  1. invites somebody else (via “Invite” button) via email or SMS
  2. that “somebody else” opens the web application via email or SMS links
  3. from then on keep the chat going via “Send” buttons in the two chat incarnations

It needs more work, that is for sure, but perhaps you want to see php_listener.php‘s chat web application “first draft”.

As with good learning between two parties, each listens, takes it in, and replies, as required.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Event-Driven Programming, Operating System, Tutorials and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *