同时执行许多请求

时间:2018-11-24 17:55:28

标签: php cron

我有一个Instagram机器人网站

我为每个用户执行的程序执行的任务必须每分钟运行一次

现在我的服务器遇到了问题

我希望我的计划得到优化

我是427位用户,我的用户脚本创建了一个cron作业并每分钟重复一次

所有cron作业都发送这种格式

mysite.com/api/cron/ [用户页面名称]

我的函数记录了这段代码

    /****************************************/
/* CRON                                 */
/****************************************/
public function cron($type = ""){
    $schedule_list = $this->db->select('activities.*, account.username, account.password, account.proxy, account.default_proxy')
    ->from($this->tb_activities." as activities")
    ->join($this->tb_accounts." as account", "activities.account = account.id")
    ->where("account.username = '{$type}' AND activities.status = 1 AND activities.time <= '".NOW."' AND account.status = 1")->order_by("time", "asc")->limit(2,0)->get()->result();
    if(!empty($schedule_list)){
        foreach ($schedule_list as $key => $schedule) {
            if(!permission("instagram/activity", $schedule->uid)){
                $this->db->delete($this->tb_activities, array("id" => $schedule->id));
                $this->db->update($this->tb_activities, array("status" => 0, array("id" => $pid)));
                $this->db->delete('instagram_activities_log', "uid = '{$schedule->uid}'");
                $this->db->delete('instagram_activities', "uid = '{$schedule->uid}'");
                $this->db->delete('instagram_accounts', "uid = '{$schedule->uid}'");
                $this->deleteLineInFile("./cron.txt",$schedule->username);
                exec('crontab ./cron.txt');
                $this->model->send_email(get_option("email_renewal_reminders_subject", ""), get_option("email_renewal_reminders_content", ""), $schedule->uid);
            }
            $action = $schedule->action;
            echo $schedule->action;
            $mainId = $schedule->pid;
            $settings = json_decode($schedule->data);
            $speed = get_value($settings, "speed");
            $next = get_time_next_schedule($schedule->settings, $speed->$action);

            $stop = $this->stop_activity($mainId, $schedule->action , $schedule);
            print_r(get_random_numbers(20));
            if($stop){ break; }

            if($next->task != 0){
                $schedule->number = $next->task;
                $proxy_data = get_proxy($this->tb_accounts, $schedule->proxy, $schedule);
                try {
                    $ig = new InstagramAPI($schedule->username, $schedule->password, $proxy_data->use);
                    $result = $ig->activity->process($schedule);
                    //print_r($result);
                    if(is_array($result) || is_object($result)){
                        $this->save_log($schedule, $result);
                        $next->numbers = get_action_left($next->numbers, $result, $next->task);
                    }

                } catch (Exception $e) {
                    print_r($e->getMessage());
                    $mess = Instagram_Get_Message($e->getMessage());
                    //ig_update_setting($schedule->action."_block", $e->getMessage(), $mainId);
                    if(strpos($mess, "This action was blocked")){
                        $next->numbers = get_random_numbers(0);
                        ig_update_setting($schedule->action."_block", "", $mainId);
                    }
                    else{
                        $next->numbers = get_action_left($next->numbers, array(), $next->task);
                    }
                }
            }

            $this->db->update(INSTAGRAM_ACTIVITIES, array(
                    "time" => date("Y-m-d H:i:s", strtotime(NOW) + $next->minute*60), 
                    "settings" => json_encode($next->numbers),
                    "changed" => date("Y-m-d H:i:s", strtotime(NOW) + $next->minute*60)
                ), 
                array("id" => $schedule->id)
            );

        }
    }else{
        echo "No activity";
    }

我希望将所有这些请求一起发送而不会出错或失败

0 个答案:

没有答案