Open zhangweijiang opened 2 years ago
如果获取的聊天记录是新的机会占内存,如果是重复获取相同的聊天记录就不会
试下 gc_collect_cycles
是啊,我服务器跑着跑着内存就满了,这咋弄
是不是把new 对象放在了循环里面?
@liumingsongning 内存满了,错误信息和代码贴下
` public function handle() { $obj = new WxworkFinanceSdk(Config('weixinWork.corpid'), Config('weixinWork.MsgAppCorpsecret')); $privateKey = file_get_contents(base_path() . '/public/private2.pem'); $this->companyWechat=new CompanyWechat(); while (true) { try { $seq = \App\Models\wechat_msg_log::orderBy("seq", "desc")->first(); $start = 400000; if ($seq) { $start = $seq->seq; } $chats = json_decode($obj->getChatData($start, 100), true); if(count($chats['chatdata'])==0){ sleep(60602); } foreach ($chats['chatdata'] as $val) { $create = null; // @param1为需要解密的数据(base64),@param2为解密后的数据,@param3为私钥 // @param4为对应的密钥格式(PKCS1开头:begin rsa private key,PKCS8开头:begin private key),都可以,只要@param4选择了正确的私钥读取参数 openssl_private_decrypt(base64_decode($val['encrypt_random_key']), $decryptRandKey, $privateKey, OPENSSL_PKCS1_PADDING); $msg = $obj->decryptData($decryptRandKey, $val['encrypt_chat_msg']);
$data = json_decode($msg, true);
if(!isset($data["msgtype"])){
continue;
}
if ($data["msgtype"] == "image") {
$url = "/tmp/".config('app.site').'/'. $data["msgid"] . ".jpg";
$obj->downloadMedia($data["image"]["sdkfileid"], $url);
$disk = Storage::disk('qiniu');
$fileContents = fopen($url, "r");
$filename = 'companyWechat/' . $data["msgid"] . '.jpg';
$disk->put($filename, $fileContents);
fclose($fileContents);
sleep(2);
unlink($url);
$create["img_url"] = config('filesystems.disks.qiniu.domain') . '/' . $filename;;
}
$from_content = $this->fromContent($data["from"],$data["roomid"]);
$create["action"] = $data["action"];
$create["from"] = $data["from"];
$create["from_name"] = $from_content["from_name"];
$create["from_avatar"] = $from_content["from_avatar"];
$create["msgid"] = $data["msgid"];
$create["msgtime"] = $data["msgtime"];
$create["msgtype"] = $data["msgtype"];
$create["roomid"] = $data["roomid"];
$create["text"] = isset($data["text"]) ? json_encode($data["text"]) : null;
$create["tolist"] = json_encode($data["tolist"]);
$create["content"] = $msg;
$create["seq"] = $val["seq"];
\App\Models\wechat_msg_log::create($create);
}
} catch (\WxworkFinanceSdkException $e) {
// var_dump($e->getMessage(), $e->getCode());
}
}
}
private function fromContent($from,$roomid)
{
$data["from_avatar"] = null;
$data["from_name"] = null;
if($from==""){
return $data;
}
$top = substr($from, 0, 2);
if ($top == "wo" || $top == "wm") {
$uid = \App\Models\company_wechat_external_user::where("external_userid", $from)->first();
if ($uid) {
$data["from_name"] = $uid->name;
$data["from_avatar"] = $uid->avatar;
} else {
$external_user = $this->companyWechat->getExternalcontact($from);
if ($external_user["errcode"] == 0) {
\App\Models\company_wechat_external_user::create($external_user["external_contact"]);
$data["from_name"] = $external_user["external_contact"]["name"];
$data["from_avatar"] = $external_user["external_contact"]["avatar"];
}
// else if($external_user["errcode"]==84061){
// }
else{
$groupData=\App\Models\weixin_work_group::where('chat_id',$roomid)->whereNotNull("member_list")->first();
if(!$groupData){
$group=$this->companyWechat->refreshGroupDetails($roomid);
if(!$group){
return $data;
}
$arr=$group->member_list;
}else{
$group=$groupData;
$arr=json_decode($group->member_list);
}
if(!$arr){
$group=$this->companyWechat->refreshGroupDetails($roomid);
if(!$group){
return $data;
}
$arr=$group->member_list;
};
foreach ($arr as $key => $value) {
if($value->userid==$from){
$data["from_name"]=$value->name;
$company_wechat_external_user_create["external_userid"]=$from;
$company_wechat_external_user_create["name"]=$value->name;
\App\Models\company_wechat_external_user::create($company_wechat_external_user_create);
}
}
}
}
} else if ($top == "wb") {
} else {
$uid = \App\Models\company_wechat_user::where("userid", $from)->first();
if ($uid) {
$data["from_name"] = $uid->name;
} else {
$user = $this->companyWechat->getUser($from);
if($user["errcode"]!=60111){
$create["name"] = $user["name"];
$create["userid"] = $user["userid"];
$data["from_name"] = $user["name"];
\App\Models\company_wechat_user::create($create);
}
}
}
return $data;
}`
一个多小时跑了12% 对内存了,32g的
能否去掉业务逻辑。 只跑chatData.downloadMedia
测试下?
我手头上没有可用的企业微信。测不了哈。或者+下 麻花疼号 5687046 跟踪下
加了,老庞吗?5687046