Skip to content
Advertisement

Symfony command doesn’t send all tracking requests

I am using symfony 3.4 (I know, needs to be upgraded, we are working on it). I have written a shell command to run every month as a cron job.

I have the following code:

// ConnectivityInventoryCommand.php

protected function execute(InputInterface $input, OutputInterface $output)
{
    try {
        $connectivityInventoryList = $this->connectivityInventoryService->generateInventoryReport($startDate, $endDate);
        $this->connectivityInventoryUploadService->uploadInventoryReport($connectivityInventoryList, $endDate);

        $metrics = $this->pushMetrics($this->prepareMetrics($connectivityInventoryList));

        /** @var Output $output */
        $output->result(sprintf("Job finished successfully"), null, $this->metricsToArray($metrics));
    } catch(Exception $exception) {
        $output->result(sprintf("Job failed with exception: %s", $exception->getMessage()));
    }
}

In the uploadInventoryReport method, it connects to an FTP server, uploads a file, and when the file is successfully uploaded, it executes this piece of code.

foreach ($this->getPartnerEmailsAsArray($partnerConnectivityInventory->getPartnerEmail()) as $partnerEmail) {
    $payload = (new TrackingPayload())
        ->setProperties(
             (new ConnectivityInventoryProperties())
                 ->setPartnerId($partnerConnectivityInventory->getPartnerId())
                 ->setPartnerName($partnerConnectivityInventory->getPartnerName())
                 ->setLanguage('en-US')
                 ->setEmail($partnerEmail)
                 ->setLink($absoluteFilePath)
         );
     $this->trackingService->track($payload);
}

Tracking service is a Segment PHP client.

I’m expecting 9 segment events to be tracked, but I’m getting anything from 6 – 8 in any given run. I know there are 9 emails, because if I debug, it enters the loop 9 times, with 9 different email addresses.

Everything works perfectly, bar the segment events.

I’ve attempted to add a sleep at the end of the foreach, but this still doesn’t help. I’ve also added a sleep at the end of the execute-method thinking it might be that it’s dying before getting the calls out, but the sleep at the end of the method, just seems to delay the segment callout until the sleep has passed.

Is there something I’m missing or not completely understanding correctly?

Advertisement

Answer

In case someone wishes to fix this issue, this is what I realised and how I fixed it.

It seems like the client doesn’t get around to flushing everything properly before the command finishes. I’m not 100% sure about the internals, but my fix makes me feel that it’s related to that.

In $this->trackingService (which is a wrapper for the linked client) I added a function called forceFlush() which just calls Segment::flush(). I call this method at the end of the try-catch in the execute method.

As soon as this is called, all the expected values appear in segment.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement