Skip to content

Commit

Permalink
Fix memory and scale issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MosheMaorKaltura committed Feb 8, 2018
1 parent f6f4905 commit 83c093a
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions batch/batches/UsersCsv/kAsyncUsersCsv.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private function generateUsersCsv(KalturaBatchJob $job, KalturaUsersCsvJobData $

//fill the csv with users data
$csvFile = fopen($filePath,"w");
$csvFile = $this->fillUsersCsv($csvFile, $data);
$this->fillUsersCsv($csvFile, $data);
fclose($csvFile);
$this->setFilePermissions($filePath);
self::unimpersonate();
Expand Down Expand Up @@ -89,7 +89,7 @@ protected function moveFile(KalturaBatchJob $job, KalturaUsersCsvJobData $data,
/**
* The function fills the csv file with the users data
*/
private function fillUsersCsv($csvFile, $data)
private function fillUsersCsv(&$csvFile,&$data)
{
$filter = clone $data->filter;
$pager = new KalturaFilterPager();
Expand All @@ -98,24 +98,56 @@ private function fillUsersCsv($csvFile, $data)

$additionalFields = $data->additionalFields;

$csvFile = $this->addHeaderRowToCsv($csvFile, $additionalFields);
$this->addHeaderRowToCsv($csvFile, $additionalFields);
$lastCreatedAtObjectIdList = array();
$lastCreatedAt=0;
$totalCount=0;
$filter->orderBy = KalturaUserOrderBy::CREATED_AT_ASC;
do
{
if($lastCreatedAt)
{
$filter->createdAtGreaterThanOrEqual = $lastCreatedAt;
}
try
{

$userList = KBatchBase::$kClient->user->listAction($filter, $pager);
$returnedSize = count($userList->objects);
}
catch(Exception $e)
{
KalturaLog::info("Couldn't list users on page: [$pager->pageIndex]" . $e->getMessage());
return $csvFile;
}
$csvFile = $this->addUsersToCsv($userList->objects, $csvFile, $data->metadataProfileId, $additionalFields);
$pager->pageIndex ++;
}
while ($pager->pageSize == count($userList->objects));

return $csvFile;
$lastObject = $userList->objects[$returnedSize-1];
$lastCreatedAt=$lastObject->createdAt;
$newCreatedAtListObject = array();

//contain only the users that are were not the former list
$uniqUsers = array();
foreach ($userList->objects as $user)
{
if(!in_array($user, $lastCreatedAtObjectIdList))
$uniqUsers[]=$user;
}
//Prepare list of the last second users to avoid duplicate in the next iteration
foreach ($uniqUsers as $user)
{
if($user->createdAt == $lastCreatedAt)
$newCreatedAtListObject[]=$user->id;
}
$lastCreatedAtObjectIdList = $newCreatedAtListObject;
$this->addUsersToCsv($userList->objects, $csvFile, $data->metadataProfileId, $additionalFields);
$totalCount+=count($uniqUsers);
KalturaLog::debug("Adding More - ".count($uniqUsers). " totalCount - ". $totalCount);
unset($newCreatedAtListObject);
unset($uniqUsers);
unset($userList);
if(function_exists('gc_collect_cycles')) // php 5.3 and above
gc_collect_cycles();
}
while ($pager->pageSize == $returnedSize);
}


Expand All @@ -135,10 +167,10 @@ private function addHeaderRowToCsv($csvFile, $additionalFields)
/**
* The function grabs all the fields values for each user and adding them as a new row to the csv file
*/
private function addUsersToCsv($users, $csvFile, $metadataProfileId, $additionalFields)
private function addUsersToCsv(&$users, &$csvFile, $metadataProfileId, $additionalFields)
{
if(!$users)
return $csvFile;
return ;

$userIds = array();
$userIdToRow = array();
Expand All @@ -155,8 +187,6 @@ private function addUsersToCsv($users, $csvFile, $metadataProfileId, $additional

foreach ($userIdToRow as $key=>$val)
fputcsv($csvFile, $val);

return $csvFile;
}

/**
Expand Down

0 comments on commit 83c093a

Please sign in to comment.