I have a (php/mysql) system that rewards users for doing things on the site. For the sake of the question, let's say a user gets a rose for every 5 comments they make.
The front page has a counter that shows how many roses have been earned today, and how many roses have been earned all-time. The rough logic is below:
//Save a single, discrete event. $user --> numerical value, 1:1.
function first($user) {
//save values related to $user into TABLE_1;
return second($user);
}
//Calculate the sum() of all numerical events for $user for today and save that.
function second($user) {
//save values calculated from TABLE_1 into TABLE_2;
return third();
}
//Calculate the sum() of all numerical events for all users
function third() {
//save values calculated from TABLE_2 into TABLE_3;
return true/false;
}
When a comment is saved, the first()
function is kicked off. first()
saves a record of the comment event as a row in TABLE_1: user id, numerical value of the event (1
, since 1 comment was saved), and timestamp.
Next, second()
is called; second()
does some math to figure out if the user has earned a rose with this comment and saves 3 values in TABLE_2: uid, roses_today, timestamp.
This is the value that's getting screwed up. For some reason, the value of roses_today varies from what it should be, sometimes being higher and sometimes being lower. There is no pattern -- some values are wildly high, some are barely too low, some are right on; there's no rhyme or reason here.
Also throwing a wrench in the works: if I call second($user)
manually, the values it calculates and stores in TABLE_2 are correct. This function seems to only fail if it's being called in real-time.
Edit: Those suggesting it's a problem with the code that isn't there or that I should step through the code, please read the previous paragraph. This function seems to only fail if it's being called in real-time. Calling the very same function manually produces correct results.