Is there any TIMEOUT for cURL calls or Symfony2 task execution?

707 views Asked by At

I'm just try to find is there is any TIMEOUT or MAX EXECUTION TIME for execute Symfony2 tasks. Why? Any time I execute the task pdone:sync it stop with this log:

Symfony > pdone:sync

  [Symfony\Component\Debug\Exception\ContextErrorException]
  Notice: Undefined offset: 2000

pdone:sync
The command terminated with an error status (1)

This is my code which is called from task as a service and second SOQL query returns exactly 6911 items so it should insert 6911 and not stop at 2000, why? what is happening there? Any advice or help or ideas?

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use PDI\PDOneBundle\Entity;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RequestStack;

class SyncController extends Controller
{
    private $_em;
    private $_container;
    private $_rs;

    public function __construct(Container $container, EntityManager $em, RequestStack $rs)
    {
        $this->_container = $container;
        $this->_em = $em;
        $this->_rs = $rs;
    }

    public function syncVeevaData()
    {
        $em = $this->_em;
        $request = $this->_rs->getCurrentRequest();
        $container = $this->_container;

        // Will need this in the whole function so lets declare it here
        $now = new \DateTime();

        $expireTime = clone $now;
        $expireTime = $expireTime->modify('+1 week');

        // Request veeva token and instance URL
        $token_url = $container->getParameter('login_uri')."/services/oauth2/token";

        $params = "grant_type=".$container->getParameter('grant_type')
            ."&client_id=".$container->getParameter('client_id')
            ."&username=".$container->getParameter('veeva_username')
            ."&password=".$container->getParameter('veeva_password')
            ."&client_secret=".$container->getParameter('client_secret')
            ."&redirect_uri=".urlencode($container->getParameter('redirect_uri'));

        $curl = curl_init($token_url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

        $json_response = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        if ($status != 200) {
            $respObj['error'] = "Error: call to token URL $token_url failed with status $status, response $json_response, curl_error ".curl_error(
                    $curl
                ).", curl_errno ".curl_errno($curl);

            return $respObj;
        }

        curl_close($curl);

        $response = json_decode($json_response, true);

        $veevaToken = $respObj['access_token'] = $response['access_token'];
        $instanceUrl = $respObj['instance_url'] = $response['instance_url'];

        if (!isset($veevaToken) || $veevaToken == "") {
            $respObj['error'] = "Error - access token missing from response!";

            return $respObj;
        }

        if (!isset($instanceUrl) || $instanceUrl == "") {
            $respObj['error'] = "Error - instance URL missing from response!";

            return $respObj;
        }

        // Request reps and sync against DB
        $soqlQuery1 = "SELECT Id,FirstName,LastName,Username,Email,LastModifiedDate FROM User";
        $soqlUrl1 = $instanceUrl.'/services/data/v28.0/query/?q='.urlencode($soqlQuery1);

        $curl = curl_init($soqlUrl1);

        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $veevaToken"));

        $jsonResponse = curl_exec($curl);

        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if ($status != 200) {
            $respObj['error'] = "Error: call to token URL $token_url failed with status $status, response $json_response, curl_error ".curl_error(
                    $curl
                ).", curl_errno ".curl_errno($curl);

            return $respObj;
        }

        curl_close($curl);

        $soqlObj1 = json_decode($jsonResponse, true);

        for ($i = 0; $i < $soqlObj1['totalSize']; $i++) {
            $entReps = $em->getRepository('PDOneBundle:Representative')->findOneBy(
                array(
                    'veeva_rep_id' => $soqlObj1['records'][$i]['Id'],
                )
            );

            if (!$entReps) {
                // if there is no reps, then we add
                $newReps = new Entity\Representative();

                // we set the values from veeva
                $newReps->setVeevaRepId($soqlObj1['records'][$i]['Id']);
                $newReps->setEmail($soqlObj1['records'][$i]['Email']);
                $newReps->setFirst($soqlObj1['records'][$i]['FirstName']);
                $newReps->setLast($soqlObj1['records'][$i]['LastName']);
                $newReps->setUsername($soqlObj1['records'][$i]['Username']);
                $newReps->setLastLoginAt($now);
                $newReps->setLastSyncAt($now);
                $newReps->setDisplayName(
                    $soqlObj1['records'][$i]['FirstName'].' '.$soqlObj1['records'][$i]['LastName']
                );
                $newReps->setRepType("VEEVA");

                $em->persist($newReps);
                $em->flush();

                // Put new reps into $newRepsArr
                $repsArr[$newReps->getId()] = $newReps->getVeevaRepId();
            } else {
                $lastModifiedDate = new \DateTime(
                    $soqlObj1['records'][$i]['LastModifiedDate']
                );

                if ($lastModifiedDate > $entReps->getLastSyncAt()) {
                    // obtained a reps, we update its data
                    $entReps->setEmail($soqlObj1['records'][$i]['Email']);
                    $entReps->setFirst($soqlObj1['records'][$i]['FirstName']);
                    $entReps->setLast($soqlObj1['records'][$i]['LastName']);
                    $entReps->setUsername($soqlObj1['records'][$i]['Username']);
                    $entReps->setLastLoginAt($now);
                    $entReps->setLastSyncAt($now);
                    $entReps->setDisplayName(
                        $soqlObj1['records'][$i]['FirstName'].' '.$soqlObj1['records'][$i]['LastName']
                    );
                    $entReps->setRepType("VEEVA");

                    // Put updated reps into $updRepsArr
                    $repsArr[$entReps->getId()] = $entReps->getVeevaRepId();
                }
            }
        }

        $em->flush();

        // Get territories and sync against DB
        $soqlQuery2 = "SELECT Id,Name,LastModifiedDate FROM Territory";
        $soqlUrl2 = $instanceUrl.'/services/data/v28.0/query/?q='.urlencode($soqlQuery2);

        $curl = curl_init($soqlUrl2);

        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $veevaToken"));

        $jsonResponse = curl_exec($curl);

        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if ($status != 200) {
            $respObj['error'] = "Error: call to token URL $token_url failed with status $status, response $json_response, curl_error ".curl_error(
                    $curl
                ).", curl_errno ".curl_errno($curl);

            return $respObj;
        }

        curl_close($curl);

        $soqlObj2 = json_decode($jsonResponse, true);

        for ($i = 0; $i < $soqlObj2['totalSize']; $i++) {
            $entTerritory = $em->getRepository('PDOneBundle:Territory')->findOneBy(
                array(
                    'veeva_territory_id' => $soqlObj2['records'][$i]['Id']
                )
            );

            if (!$entTerritory) {
                // if there is no territory, then we add
                $newTerritory = new Entity\Territory();

                // we set the values from veeva
                if ($soqlObj2['records'][$i]['Id'] !== null || $soqlObj2['records'][$i]['Id'] !== "") {
                    $newTerritory->setVeevaTerritoryId($soqlObj2['records'][$i]['Id']);
                    $newTerritory->setName($soqlObj2['records'][$i]['Name']);

                    $em->persist($newTerritory);
                    $em->flush();
                }

                $terrArr[] = $newTerritory->getId();
                $terrFailArr[] = $soqlObj2['records'][$i]['Name'];
            } else {
                $lastModifiedDate = new \DateTime(
                    $soqlObj2['records'][$i]['LastModifiedDate']
                );

                if ($lastModifiedDate > $entTerritory->getUpdatedAt()) {
                    // obtained a territory, we update its data
                    $entTerritory->setName($soqlObj2['records'][0]['Name']);
                }

                $terrArr[] = $entTerritory->getId();
            }
        }

        $em->flush();

        return $respObj;
    }
}
0

There are 0 answers