Welcome Guest, Not a member yet? Register   Sign In
Running scheduled tasks in CI?
#1

[eluser]err403_love[/eluser]
Hi,

I've been brainstorming on how best to implement a "Scheduled Task" system for my first CI project.

First, I want to run it using AJAX at the bottom of all my pages. I don't need any help with this part exactly, other than knowing if there are any issues with running a CI controller over an XHR.

Second is security. I was thinking I will need some method to encode a string and then decode it on the other side, and if it doesn't match the controller will exit. I think this is necessary because I can't have a static URL for the task script, otherwise sketchy people would be able to do a sort of "attack" on my server by running the URL continuously.

This method certainly doesn't have to be foolproof, though. What my task involves is updating local XML files with external ones, which I then store a serialized array in the database based off of the local XML file.

So I was thinking I could encode something that changes often enough, like the Unix timestamp, but have it rounding up to the nearest minute and then do the encode, so by the next second later when it decodes in another script it will still match. I realize there will be a second or so out of each minute (during the change to the next minute) where the script won't run when it's supposed to, but if a visitor visits the next page soon after, the script should run fine, and I don't have a problem with this.

Is this too complicated? Is there some better way to pass a random string through the URL that will change and will not work repeatedly (opening up to attacks)?

Third, the actual reason I need to do this is because when it is time to update the XML files, the page will stall badly until that part is finished. The data that is being displayed most of the time, though, is being taken right out of the database, so I can offload the actual file updates into a task that runs LAST and with AJAX so that it doesn't affect the page load so greatly.

Any ideas or suggestions would be appreciated.
#2

[eluser]err403_love[/eluser]
Well, I've figured it out. Thanks though.

Instead of keeping the script constantly available, I only display the Javascript in the footer after the files have not been modified for 10 minutes. On the next page load, my jQuery runs in the footer, passes an encrypted key as an argument in the URL, then the controller decrypts it, and it gives it 10 seconds wiggle room. (The key is a difference between the current time and the "filemtime" of a log file that records the Scheduled Task activity.)

I currently only have the code specialized for one task, but as I need more tasks to be updated and cached to my datastore, I'll attempt to recode it so that I can plug in any scheduled task and make sure it runs at the proper times and is safely protected with my rudimentary encryption method.

It works like a dream, and jQuery certainly helped make the AJAX part easy-peasy!
#3

[eluser]Michael Wales[/eluser]
Sounds like a task for cron to me...
#4

[eluser]err403_love[/eluser]
No, not really. I don't need it updating the files unless someone is actually viewing that page. The XML files are specific to a user and whether or not they've chosen to display this information. If nobody is viewing a certain user's page, I certainly don't want my server still working away every 10 minutes at updating that information.

And plus, since there will be more than one user (when I code for it) the cron would end up being really intensive, because the only way I could really do it as a cron is to force every XML file in my cache directory to update at the same time, even if they don't need to be.

So, I guess this is more an "on-demand" cron, and I'd say it works very well. I'd like turning it into a backend so that an admin can manage how often each task runs, view logs, and add new tasks to run. When this app is actually complete, I see it needing a LOT of things like this because I will be displaying a lot of information that does NOT need to be updated with every page load and I really want to use as few queries on each page as possible so that I know it will scale well if it ever gets a huge amount of traffic. Kind of like vBulletin's "Scheduled Task" system, I guess. But using an AJAX loader at the bottom of the page instead of loading the PHP file through a tiny GIF. In their defense, AJAX wasn't even an idea when they first designed vBulletin. :roll: Smile




Theme © iAndrew 2016 - Forum software by © MyBB