Very quick question about Server Side Tasks
02-28-2009, 05:55 AM
|
Very quick question about Server Side Tasks
|
Posts: 59
Name: Jaryth
Location: Canada
|
So, over the last while, I've dedicated myself to learning PHP, and in that regard (as far as Im concerned) I've learned a lot.
However, upon starting to write simple PHP applications... something becomes apparent...
So, when a user requests a page, the PHP script is run, can store temporary variables, Cookies, Sessions, and even edit Databases. Thats all well and good, but if no one goes to that PHP page... nothing gets executed right?
My question then becomes, how can I do tasks server side? I know this is getting into non-php stuff, but what I want specificaly, is what should I be using (or start learning) to do these things?
For example, a PHP script that edits a database and changes veriables per a users request, but once an hour I want something to reset those verables in that databse back to defults. So what would I have to learn/do to do this? Perl? Python? whats the best way to accompish these tasks?
Thanks!!
(Specific example: for my first minor site, I desided to make (as many many people have before when first starting PHP) a simple game site (only for my own/friends use... just as a test of skill) and its comming along good, but I have a combat system... and once a character has been knocked to 0 health, the only way to 'revive them' is to log into there account and heal them, or edit the database directly... I want something that I can make, that will do tasks like automaticaly revive all of the accounts every hour... and other things on that nature.)
__________________
Please login or register to view this content. Registration is FREE My personal website
-Jaryth (UID590)
|
|
|
|
02-28-2009, 06:17 AM
|
Re: Very quick question about Server Side Tasks
|
Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
|
Depends on your hosting and server access;
*nix, you may be able to setup "cron jobs" to run scheduled operations.
Windows, You may be able to use "Scheduled Tasks"
However, despite running the servers I do neither
For tasks that need to be completed after a set time I will call a routine on every page access that checks current time and a list of queued tasks, if one is overdue/due, run it and remove the task from the list, then continue on with the requested page.
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
02-28-2009, 06:19 AM
|
Re: Very quick question about Server Side Tasks
|
Posts: 79
Name: Ed Barnett
|
That's a great question - one that I often asked myself.
This will depend on the server you have your site hosted on. For Linux based servers you use something called Crontab which is a scheduled task manager (effectively). In essence you tell Crontab what you want to do and at what frequency.
To this end, you can tell it to run the rest.php script every hour. So... in short, you can still do this in PHP you just need to specify the time and script to run in Crontab. I'm sure someone will be able to tell you what it is on a windows server 
|
|
|
|
02-28-2009, 06:25 AM
|
Re: Very quick question about Server Side Tasks
|
Posts: 59
Name: Jaryth
Location: Canada
|
So, after a quick check into it, I do have access to Cron Jobs (Im going to have to look that up in a moment... that name... something about it just sounds odd lol), so using that and EdB's comment about running PHP scripts using it... I should be good.
hmm so just to make sure, lets say I where to set up a job that every minute it runs a "tasks.php" script. That script then runs a set routine of tasks (exuciting all the tasts that need to be cone per minute, and per hour, depending on the time).
chrishirst's comment about doing a pageperpage check of the tasks may work as well... however Im not sure how to effectivly pull that off.
Thank you both however!
__________________
Please login or register to view this content. Registration is FREE My personal website
-Jaryth (UID590)
|
|
|
|
02-28-2009, 07:32 AM
|
Re: Very quick question about Server Side Tasks
|
Posts: 59
Name: Jaryth
Location: Canada
|
Update:
So, after a fair bit of time, fighting with the Deamhosts (my host) method of handling Cron Jobs, and eventually giving up using there console, and jumping right into SSH'ing for the first time I finally got it to run a seccessfull PHP Script.
However, I was wondering...
@chrishirst
Why is it better doing it that way, instead of running it though Cron or Schedualed tasks? You seam like quite the experienced and wise person, so I take it that there must be a reason correct?
__________________
Please login or register to view this content. Registration is FREE My personal website
-Jaryth (UID590)
|
|
|
|
02-28-2009, 11:51 AM
|
Re: Very quick question about Server Side Tasks
|
Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
|
Well it's not necessarily "better" as such, just when I moved from Company Intranet to shared hosting I had to find a way that didn't need getting the hosting company involved in setting up timed ops, because very often they won't / can't / don't know how.
So I worked out a way I could transfer the "house keeping" methods I applied to desktop applications, such as deleting expired data, backups, operator task notifications etc, and applied that to web applications.
It's really as simple as the things you would normally do at startup, initialise variables, instantiate objects, tell the user they have messages etc, but because you don't know who has created the page request, you don't always pass it to the browser, just simply run the operation in the background.
If it is a known user that logs in or requests the page you can process tasks that are related to them.
If it is an anonymous request then run the next task in the queue and delete it.
All your DB entries need are a task ID, User ID (if it needs to be done for a specific user), date/time it has to be done by and some variable values that tell the system what to do.
Your pages already have the management function page included which reads the database but it performs no action unless the variables are set.
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
02-28-2009, 03:12 PM
|
Re: Very quick question about Server Side Tasks
|
Posts: 59
Name: Jaryth
Location: Canada
|
Hmm I see. That does seem like a very good way to manage everything on your own, so you have complete control over it. But having these things being checked every time any page is loaded by anyone... even if no tasks are done, doing a half dozen to a dozen checks every page load... with a lot of users going at once, wouldn't that cause a performance hit?
Like I said, I've just started learning PHP, but when I see people that have scripts... that include scripts... that include scripts, and the final product ends up being a very nice, modular code with a bunch of different parts coming together, well that’s all well and good, and I know PHP executes extremely fast and all, but the fact that the server has to run around and grab all of these included’s seams like it must slow it down a bit to process. But... I don’t know.
__________________
Please login or register to view this content. Registration is FREE My personal website
-Jaryth (UID590)
|
|
|
|
02-28-2009, 03:35 PM
|
Re: Very quick question about Server Side Tasks
|
Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
|
There is obviously some small increase in processing but provided you don't try and run a 500,000 row multi table update query it's isn't noticeable.
On a busy server there will only be a need to run "SELECT taskid FROM queue WHERE active = 1 LIMIT 1,0;" on every page request and only if it returns a taskid would you do something with it. Probably less than 1% of requests need to do any additional ops.
Some stress testing I did a long time ago on a much less powerful machine (a PII 350 MHz lab rat) than we have now showed no significant issues.
If anything the timed operations were more intrusive as they often locked records for a considerable time (in computer terms of course)
Don't worry about using includes and extra load, there is a thread here recently where some speed tests were done, and the difference with includes is truly minute.
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
02-28-2009, 03:50 PM
|
Re: Very quick question about Server Side Tasks
|
Posts: 59
Name: Jaryth
Location: Canada
|
Hmm, sounds good! Thank you for clearing that up sir!
__________________
Please login or register to view this content. Registration is FREE My personal website
-Jaryth (UID590)
|
|
|
|
|
« Reply to Very quick question about Server Side Tasks
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|