I'm trying to think of a way to do something efficiently, but am at a dead end.

Essentially, what I want to do is to have a queue (in the form of a MySQL Table), and I want to do something with each entry of the table.

There are two things that cause me problems with this, however:

1) Items can be added to the queue at any time (And a lot of the time it is going to be empty)

2) There needs to be a 30 second delay between each event firing (So 30 seconds per row).

The best example I can think of that would be easy to relate to would be to say that my mail server only lets me send one mail per 30 seconds and I wanted to do lost password requests and signup confirmations via E-mail.

So, in theory, I would just add a row to the table any time I wanted to send out an E-mail. Have the subject, id (for ordering), and message, and then there would be some way of calling a script that would send one every 30 seconds until the queue was empty.

I would also like to not use a normal cron tab because I don't want it firing every 30 seconds because that would obviously be an unneeded strain on the server.

Any ideas?

    No thoughts?

    Worse comes to worst I will just set up the 30 second cron, but I'd rather not.

    Another potential idea was to just put a function in the global.inc file that will check for rows in the database and if one was found, call the event, but the problem with that lies when a sizeable queue comes up and few people are visiting the page.

      If coded correctly it's a simple query every 30th second which you can exit from should it contain no new rows, I doubt you can even measure the "strain" this will put on your server 🙂

      I haven't worked much with cron jobs yet so i'm probably not the best one to answer anyways 🙂

        You're probably right, but regardless I don't see the point in having a script running 24/7 firing every 30 seconds 🙂

        I thought of a workaround (Basically unsetting the crontab when the queue is empty, and starting it when it is not), but I am having troubles getting the cron working.

        I have chmodded the script and containing folder to 755 and have tried all of the following cron tabs, none of which seem to worK:

        * * * * * /aa/cgi-system/php.cgi /home/root/domain.com/cron/cron.php
        * * * * * wget http://www.domain.com/cron/cron.php

        All the paths are correct, so I'm at a dead-end.

        Anybody have any ideas?

          I wouldn't worry too much about a cron job running every 30 seconds. A few instructions every minute would barely make a dent in the processing capability of a modern computer. If the clock speed is going at 2 GHZ, that's 2 BILLION times per second. what's a handful of instructions here and there?

            Any system with cron will also have a utility called "at". It's basically a one-shot cron.

            Consider this scenario:
            The script sends the first email in the queue, and the removes it from the queue. If there are emails remaining, register an at job to run in thirty seconds that will run the current script.

            How you start the script in the first place is another matter. If it's via cron you need to give it plenty of time to work through the queue: what you don't want is for cron to start a new instance of the script while the previous queue is still being processed, since that would cause (attempts to) send emails more frequently than twice a minute.

            Another approach would be to simply have the script run for as long as necessary to get the job done, with a sleep(30) between each iteration of the mail loop. Resetting the timeout to something a little over 30 seconds for each iteration would keep it running for as long as necessary until the queue is empty.

            sneakyimp wrote:

            If the clock speed is going at 2 GHZ, that's 2 BILLION times per second. what's a handful of instructions here and there?

            Don't forget file access times, memory consumption, and courtesy to other processes. If you've got 150,000 emails to send out then you might as well have cron running every thirty seconds, but if you only have 150 emails then it's just a Big Dumb Waste of computing power. And if you have 150,000 emails to send out you want to send them much faster than two per minute.

              Weedpacket wrote:

              Any system with cron will also have a utility called "at". It's basically a one-shot cron.

              Consider this scenario:
              The script sends the first email in the queue, and the removes it from the queue. If there are emails remaining, register an at job to run in thirty seconds that will run the current script.

              Awesome, the at utility is perfect for my needs.

              After some playing around and fun stuff I got it working correctly.

              Cheers!

                Or so I thought!

                I am having a problem and I have absolutely no idea what is going on.

                The script works perfectly if I type the command through SSH.

                Using this:

                at -f /home/root/domain.org/cron/at now + 1 minute

                It calls the script, the script works, everything is great.

                However, if I try and use the command through shell_exec, it just doesn't work.

                <?
                shell_exec("at -f /home/root/domain.org/cron/at now + 1 minute");
                ?>

                The at task appears to be scheduled with atq, it seems to fire correctly, but it doesn't run the script.

                And the file 'at' which is being referenced is just this:

                php /home/root/domain.org/cron/at.php

                because if I call at.php directly it tried to interpret it as a shell script.

                Any ideas why it would work through SSH but not shell_exec?

                  Write a Reply...