0

我正在尝试通过 tcp 协议对 ZeroMQ (PHP-ZMQ) 使用繁重的资源内容循环操作。

到目前为止,我有以下代码:

    class SJB_Miscellaneous_SendNotifications extends SJB_Function
    {
        public function execute()
        {
            try{
                set_time_limit(0);
                $i18n = SJB_I18N::getInstance();
                $lang = $i18n->getLanguageData($i18n->getCurrentLanguage());
                $current_date = strftime($lang['date_format'], time());

                // Send Search Notifications
                $saved_searches = SJB_SavedSearches::getAutoNotifySavedSearches();
                // echo "<pre>"; print_r($saved_searches); echo "</pre>"; exit;
                $listing = new SJB_Listing();
                $notified_saved_searches_sid = array();
                $notificationsLimit = (int) SJB_Settings::getSettingByName('num_of_listings_sent_in_email_alerts');

                /* ZeroMQ implementation */
                $queue = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ, "MySock1");
                $queue->connect("tcp://127.0.0.1:5555");

                /* Assign socket 1 to the queue, send and receive */
                $time = time();
                $queue->send(json_encode($saved_searches));
                // echo $queue->recv();

                // exit;

                $count = 1; 
                // array_shift($saved_searches);
                $server = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REP);
                $server->bind("tcp://127.0.0.1:5555");
                $msg = $server->recv();
                $saved_searches = json_decode($msg, true);
                // echo "<pre>"; var_dump($saved_searches); echo "</pre>"; 
                // exit;
                foreach ($saved_searches as $saved_search) {
                    $searcher = new SJB_ListingSearcher();
                    $listing->addActivationDateProperty();
                    $search_data = unserialize($saved_search['data']);
                    $search_data['active']['equal'] = 1;
                    $datearr = explode('-', $saved_search['last_send']);
                    $saved_search['last_send'] = strftime($lang['date_format'], mktime(0, 0, 0, $datearr[1], $datearr[2], $datearr[0]));
                    $search_data['activation_date']['not_less'] = $saved_search['last_send'];
                    $search_data['activation_date']['not_more'] = $current_date;
                    $listing_type_sid = 0;
                    if ($search_data['listing_type']['equal']) {
                        $listing_type_id = $search_data['listing_type']['equal'];
                        $listing_type_sid = SJB_ListingTypeManager::getListingTypeSIDByID($listing_type_id);
                        if (SJB_ListingTypeManager::getWaitApproveSettingByListingType($listing_type_sid))
                            $search_data['status']['equal'] = 'approved';
                    }

                    // echo "<pre>"; echo $saved_search['user_sid']; print_r($listing_type_id); echo "</pre>"; exit;
                    $id_alias_info = $listing->addIDProperty();
                    $username_alias_info = $listing->addUsernameProperty();
                    $listing_type_id_info = $listing->addListingTypeIDProperty();
                    $aliases = new SJB_PropertyAliases();
                    $aliases->addAlias($id_alias_info);
                    $aliases->addAlias($username_alias_info);
                    $aliases->addAlias($listing_type_id_info);

                    $search_data['access_type'] = array(
                        'accessible' => $saved_search['user_sid'],
                    );

                    $criteria = SJB_SearchFormBuilder::extractCriteriaFromRequestData($search_data, $listing);

                    $searcher->found_object_sids = array();
                    $searcher->setLimit($notificationsLimit);
                    $sorting_fields = array('CityRegion' => 'ASC');

                    $found_listings_ids = $searcher->getObjectsSIDsByCriteria($criteria, $aliases, $sorting_fields);

                    // echo "<pre>"; var_dump($found_listings_ids); echo "</pre>";

                    if (count($found_listings_ids)) {
                        $saved_search['activation_date'] = $saved_search['last_send'];
                        if (SJB_Notifications::sendUserNewListingsFoundLetter($found_listings_ids, $saved_search['user_sid'], $saved_search, $listing_type_sid)) {
                            SJB_Statistics::addStatistics('sentAlert', $listing_type_sid, $saved_search['sid']);
                            SJB_DB::query('UPDATE `saved_searches` SET `last_send` = CURDATE() WHERE `sid` = ?n', $saved_search['sid']);
                        }
                        $notified_saved_searches_sid[] = $saved_search['sid'];
                    }

                    echo nl2br($count."\n");
                    $count++;
                }

                // To be entered from task_scheduler.php
                $expired_contracts_id = null; $expired_listings_id = null;
                $template_processor = SJB_System::getTemplateProcessor();
                $template_processor->assign('expired_listings_id', null);
                $template_processor->assign('deactivated_listings_id', null);
                $template_processor->assign('expired_contracts_id', null);

                $template_processor->assign('notified_saved_searches_id', $notified_saved_searches_sid);
                $scheduler_log = $template_processor->fetch('task_scheduler_log.tpl');

                if ($log_file = @fopen('task_scheduler.log', 'a+')) {
                    fwrite($log_file, $scheduler_log);
                    fclose($log_file);
                }

                SJB_DB::query('INSERT INTO `task_scheduler_log`
                    (`last_executed_date`, `notifieds_sent`, `expired_listings`, `expired_contracts`, `log_text`)
                    VALUES ( NOW(), ?n, ?n, ?n, ?s)',
                    count($notified_saved_searches_sid), count($expired_listings_id), count($expired_contracts_id), $scheduler_log);
            } catch(Exception $e){
                echo $e->getMessage();
            }
        }
    } // END class SJB_Miscellaneous_SendNotifications extends SJB_Function

有没有一种好方法可以将 ZeroMQ 用于这种类型的操作,我想释放循环操作,尤其是当它到达$saved_searchesforeach 循环时?

4

0 回答 0