Commit 306a9bec by pupi1985

Apply some early returns in function qa_mailing_perform_step()

parent de02a7fc
...@@ -63,51 +63,56 @@ function qa_mailing_perform_step() ...@@ -63,51 +63,56 @@ function qa_mailing_perform_step()
$lastuserid = qa_opt('mailing_last_userid'); $lastuserid = qa_opt('mailing_last_userid');
if (strlen($lastuserid)) { if (strlen($lastuserid) == 0) {
$thistime = time(); return;
$lasttime = qa_opt('mailing_last_timestamp'); }
$perminute = qa_opt('mailing_per_minute');
if (($lasttime - $thistime) > 60) // if it's been a while, we assume there hasn't been continuous mailing... $thistime = time();
$lasttime = $thistime - 1; // ... so only do 1 second's worth $lasttime = qa_opt('mailing_last_timestamp');
else // otherwise... $perminute = qa_opt('mailing_per_minute');
$lasttime = max($lasttime, $thistime - 6); // ... don't do more than 6 seconds' worth
$count = min(floor(($thistime - $lasttime) * $perminute / 60), 100); // don't do more than 100 messages at a time if (($lasttime - $thistime) > 60) // if it's been a while, we assume there hasn't been continuous mailing...
$lasttime = $thistime - 1; // ... so only do 1 second's worth
else // otherwise...
$lasttime = max($lasttime, $thistime - 6); // ... don't do more than 6 seconds' worth
if ($count > 0) { $count = min(floor(($thistime - $lasttime) * $perminute / 60), 100); // don't do more than 100 messages at a time
qa_opt('mailing_last_timestamp', $thistime + 30);
// prevents a parallel call to qa_mailing_perform_step() from sending messages, unless we're very unlucky with timing (poor man's mutex)
$sentusers = 0; if ($count == 0) {
$users = qa_db_users_get_mailing_next($lastuserid, $count); return;
}
if (count($users)) { qa_opt('mailing_last_timestamp', $thistime + 30);
foreach ($users as $user) { // prevents a parallel call to qa_mailing_perform_step() from sending messages, unless we're very unlucky with timing (poor man's mutex)
$lastuserid = max($lastuserid, $user['userid']);
}
qa_opt('mailing_last_userid', $lastuserid); $sentusers = 0;
qa_opt('mailing_done_users', qa_opt('mailing_done_users') + count($users)); $users = qa_db_users_get_mailing_next($lastuserid, $count);
$isModeratingUsers = qa_opt('moderate_users'); if (count($users)) {
foreach ($users as $user) {
$lastuserid = max($lastuserid, $user['userid']);
}
foreach ($users as $user) { qa_opt('mailing_last_userid', $lastuserid);
if (($user['flags'] & QA_USER_FLAGS_NO_MAILINGS) || // exclude users who don't want to get the mailings qa_opt('mailing_done_users', qa_opt('mailing_done_users') + count($users));
($user['flags'] & QA_USER_FLAGS_USER_BLOCKED) || // exclude blocked users
($isModeratingUsers && ($user['level'] < QA_USER_LEVEL_APPROVED))) { // if moderating users exclude unapproved users
continue;
}
qa_mailing_send_one($user['userid'], $user['handle'], $user['email'], $user['emailcode']); $isModeratingUsers = qa_opt('moderate_users');
$sentusers++;
}
qa_opt('mailing_last_timestamp', $lasttime + $sentusers * 60 / $perminute); // can be floating point result, based on number of mails actually sent foreach ($users as $user) {
if (($user['flags'] & QA_USER_FLAGS_NO_MAILINGS) || // exclude users who don't want to get the mailings
($user['flags'] & QA_USER_FLAGS_USER_BLOCKED) || // exclude blocked users
($isModeratingUsers && ($user['level'] < QA_USER_LEVEL_APPROVED))) { // if moderating users exclude unapproved users
continue;
}
} else qa_mailing_send_one($user['userid'], $user['handle'], $user['email'], $user['emailcode']);
qa_mailing_stop(); $sentusers++;
} }
qa_opt('mailing_last_timestamp', $lasttime + $sentusers * 60 / $perminute); // can be floating point result, based on number of mails actually sent
} else {
qa_mailing_stop();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment