对于我的网站,我每 30 分钟运行一次 Cron 作业,如果我尝试在这 30 分钟标记访问我的网站,我会收到 500 内部服务器错误,或者我会收到网站上任何页面的 5-10 秒加载时间。
我正在使用 Php,而我的 cron 作业正在使用 Php 和 MySQL。
我怎样才能使它不落后于我的整个网站或使它更快,从而减少滞后?
克朗斯:
每 15 分钟运行一次的 crons 之一:
<?php
require('functions.php');
global $mysqli;
$select = $mysqli->prepare("SELECT `tkn` FROM `users` ORDER BY `dt` ASC LIMIT 0, 200");
$select->execute();
$select->bind_result($cur_token);
$tokens = array();
while($select->fetch())
{
array_push($tokens, $cur_token);
}
foreach($tokens as $token)
{
$api = Class::Instance($token);
$info = $api->Users->Info();
if(empty($info->error))
{
$info->data->token = $token;
updateUser($info->data);
} else if($info->error->code == 400) {
$update = $mysqli->prepare("UPDATE `users` SET `active` = 0 WHERE `tkn` = ?");
$update->bind_param('s', $token);
$update->execute();
}
}
?>
其他 Cron 作业:
<?php
require('functions.php');
global $mysqli;
$select = $mysqli->prepare("SELECT `email`, `username`, `id` FROM `users` WHERE `email` IS NOT NULL AND `email` <> '' AND `credits` < `credits_offered` AND `emailed_credits` = 0");
$select->execute();
$select->bind_result($email, $username, $id);
$users = array();
while($select->fetch())
{
$users[] = array("id" => $id, "email" => $email, "username" => $username);
}
foreach($users as $user)
{
$to = $user['email'];
$subject = '';
$message = "";
$headers = 'From: email@domain.com' . "\r\n" .
'Reply-To: reply@domain.com';
mail($to, $subject, $message, $headers);
$update = $mysqli->prepare("UPDATE `users` SET `emailed` = 1 WHERE `id` = ?");
$update->bind_param('i', &$user['id']);
$update->execute();
$update->close();
}
?>