我们知道发送邮件或短信可以添加到队列里进行异步任务 而不会因为延迟阻塞当前操作。
如果我们要给一万个用户发一封邮件,我需要把这1万个任务添加到队列里。
但是我如果程序中直接把一万个任务添加到队列,一次性循环或批量操作这样会阻塞当前操作,超时或者其他原因很容易出问题,你懂的。
所以我添加1万个任务到队列的这个操作也应该异步任务操作吧?,但是怎么做比较优雅?
我们知道发送邮件或短信可以添加到队列里进行异步任务 而不会因为延迟阻塞当前操作。
如果我们要给一万个用户发一封邮件,我需要把这1万个任务添加到队列里。
但是我如果程序中直接把一万个任务添加到队列,一次性循环或批量操作这样会阻塞当前操作,超时或者其他原因很容易出问题,你懂的。
所以我添加1万个任务到队列的这个操作也应该异步任务操作吧?,但是怎么做比较优雅?
网上找了一个laravel操作给手机发送短信代码,可以参考参考
//1.进入视图
<div class="register-main" id="redeemPrizes">
<ul class="register">
<li>
<label>手机号:</label>
<input class="ipt-box tel-bg" value="" id="regi_mobile" type="text">
</li>
<li>
<label>验证码:</label>
<input class="code" value="六位数字验证码" id="validatecode" type="text">
<input class="code" onclick='duanxin()' value="获取验证码" type="button">
</li>
</ul>
<span id="xin_top_userinfo" sname="http://www.xin.com"><div class="person-wrap" style="top:0px;"><a href="javascript:clear_invalid();show_popup('#popupLogin','#popupLogin%20.closeJs');" id="loginA" class="login">登录</a>/<a href="#" id="regA" rel="nofollow" class="register" >注册</a></div></span>
<div class="btn-div">
</div>
<script src="http://www.haoyunyun.cn/jquery.js"></script>
<script>
function duanxin(){
//获取手机ID
var iphone=$("#regi_mobile").val();
$.ajax({
url:'registers',
data:{'iphone':iphone},
type:"GET",
dataType:"Json",
success:function(msg){
if(msg['stat']=='100'){
alert('短信发送成功了');
}else{
alert('短信发送失败了');
}
}
});
}
</script>
//2.填写相应的路由
Route::any('registers','LoginController@login_do');
//3.写出相应的控制器
public function login_do(){
$iphone=$_GET['iphone'];
$code=rand(1000,9999);
setcookie('code',$code,time()+600);
//echo $url
$url='http://api.sms.cn/sms/?=send&uid=*********&pwd=**********&template=384859&mobile='.$iphone.'&content={"code":"'.$code.'"}';
$data=array();
$method='GET';
$res=$this->curlPost($url,$data,$method);
echo $res;
}
/*curlpost传值*/
public function curlPost($url,$data,$method){
$ch = curl_init(); //1.初始化
curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
//4.参数如下
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
if($method=="POST"){//5.post方式的时候添加数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);//6.执行
if (curl_errno($ch)) {//7.如果出错
return curl_error($ch);
}
curl_close($ch);//8.关闭
return $tmpInfo;
}
//4.完毕!
2 回答1.6k 阅读✓ 已解决
1 回答1.4k 阅读✓ 已解决
2 回答988 阅读✓ 已解决
2 回答1.1k 阅读
1 回答934 阅读
1 回答890 阅读
1 回答885 阅读
假设你的业务场景是:给站内所有会员发送通知邮件
那么你的任务队列本身的程序实现逻辑就是等待消息然后“获取所有会员邮箱,然后在一个任务中循环给所有会员发送通知邮件”(实际上应该还有更多的优化空间,比如支持区间的会员ID,规则的会员ID等等)
所以假设你后台有一个按钮是给所有会员发送通知,点击以后实际上只是给这个任务队列一个消息通知让他执行任务,而不是你点了那个按钮要获取所有会员邮箱,然后拆分成N个消息通知任务队列执行