Memory leak using repository in iteration (In dev env) - memory

Hello I'm running into memory leakage when trying to create a lot of product reviews using the following code:
echo "Start: " . memory_get_usage();
foreach ($reviews['items'] as $review) {
$newReview = $this->handleReviewData($review, $languages, $salesChannels, $context);
if ($newReview) {
$reviewsToImport[] = $newReview;
}
echo "End: " . memory_get_usage();
}
$this->productReviewRepository->create($reviewsToImport, $context);
I'm running multiple queries on $this->productReviewRepository within $this->handleReviewData. So I'm guessing that somehow creates the leak, but I can't find a ->flush() or ->clear($entity) (because it isn't Symfony). Any idea's?
It adds +-1000000 bytes (1mb) of memory for each iteration.
HandleReviewData function:
public function handleReviewData($review, $languages, $salesChannels, $context): ?array
{
$reviewLocaleCode = $review['questionnaire']['locale'];
$productSku = $review['product']['sku'];
$salesChannels = $salesChannels->filter(static function ($salesChannel) use ($review) {
/* #var $salesChannel TrustApiEntity */
return $salesChannel->getChannelId() === $review['channelRef'];
});
$salesChannelId = $salesChannels->first()?->getSalesChannel()->getId();
if (!$salesChannelId) {
return null;
}
$languageId = array_search(str_replace('_', '-', $reviewLocaleCode), $languages, true);
if (!$languageId) {
return null;
}
$products = $this->productRepository
->search((new Criteria())
->addFilter(new EqualsFilter('productNumber',
$productSku))
, $context);
$productId = $products->first()?->getId();
if (!$productId) {
return null;
}
$existingReview = $this->productReviewRepository
->searchIds((new Criteria())
->addFilter(
new EqualsFilter('title', $review['title']),
new EqualsFilter('content', $review['comment'])
)
, $context)->getIds();
if (count($existingReview) > 0) {
return null;
}
return [
'productId' => $productId,
'salesChannelId' => $salesChannelId,
'languageId' => $languageId,
'externalUser' => $review['customer']['firstName'] ?? 'Anonymous',
'externalEmail' => $review['customer']['email'] ?? 'anonymous',
'title' => $review['title'],
'content' => $review['comment'],
'points' => round($review['rating'], 0),
'status' => true,
'createdAt' => new \DateTime('#' . strtotime($review['createdAt']))
];
}

I think this is intended behaviour. I encountered similiar issues when doing migrations from other systems to Shopware 6, where also a lot of data is read and written.
The issue with this is no issue in Shopware itself but the underlying Symfony framework because in dev environment the Symfony profiler is logging a lot of data for the current request which leads to huge amounts of memory used. I've seen it grow to several GB of data.

Related

Youtube Data API is returning invalid data

I'm not really sure how to ask this, but I have a php script that pulls data from the youtube-v3-api for my youtube channel, mainly a list of videos that I have published. It's been working great up until earlier today when I went to run it again because I added a new video. Here's the output for the first object in the items array
items:[
{
kind:"youtube#searchResult",
etag:"" XpPGQXPnxQJhLgs6enD_n8JR4Qk/tluoWYe5GE9lVFAkcMtcec2Ycug"",
id:{
kind:"youtube#channel",
channelId:"UCD8d3FGC907iS1qiMF0ccxA"
},
snippet:{
publishedAt:"2006-04-30T19:39:08.000Z",
channelId:"UCD8d3FGC907iS1qiMF0ccxA",
title:"Travis Ballard",
description:"",
thumbnails:{
default:{
url:"https://yt3.ggpht.com/-L5MV7tUNjlk/AAAAAAAAAAI/AAAAAAAAAAA/dXNuqxAYprw/s88-c-k-no-mo-rj-c0xffffff/photo.jpg"
},
medium:{
url:"https://yt3.ggpht.com/-L5MV7tUNjlk/AAAAAAAAAAI/AAAAAAAAAAA/dXNuqxAYprw/s240-c-k-no-mo-rj-c0xffffff/photo.jpg"
},
high:{
url:"https://yt3.ggpht.com/-L5MV7tUNjlk/AAAAAAAAAAI/AAAAAAAAAAA/dXNuqxAYprw/s800-c-k-no-mo-rj-c0xffffff/photo.jpg"
}
},
channelTitle:"Travis Ballard",
liveBroadcastContent:"none"
}
}
]
This does not represent a video on my channel? also, it's not in order at all. It should be ordered by date as i'm asking it to in my php script:
<?php
const APIKEY = 'MyAPIKeyHere';
const CHANNELID = 'UCD8d3FGC907iS1qiMF0ccxA';
const VIDEO_COUNT = 50;
class FetchYoutubeVideos {
private $api_key = null;
private $channel_id = null;
private $count = null;
public function __construct($api_key, $channel_id, $count = 10) {
$this->api_key = $api_key;
$this->channel_id = $channel_id;
$this->count = $count;
$this->writeToFile(
'videos.json',
$this->fetch($this->getApiUrl())
);
printf( 'fetched videos from: %s', $this->getApiUrl());
}
public function getApiUrl($options = array()) {
$endpoint = 'https://www.googleapis.com/youtube/v3/search';
$default_options = array(
'key' => $this->api_key,
'channelId' => $this->channel_id,
'part' => 'snippet,id',
'order' => 'date',
'maxResults' => $this->count
);
$options = array_merge($options, $default_options);
return sprintf('%s/?%s', $endpoint, http_build_query($options));
}
public function fetch($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return $result;
}
public function writeToFile($filename, $data) {;
$fh = fopen($filename, 'w+');
fwrite($fh, $data, strlen($data));
fclose($fh);
}
}
$fetchVideos = new FetchYoutubeVideos(APIKEY, CHANNELID, VIDEO_COUNT);
The newest video should be 'Tragic - Rock-a-Hoola Revisited' and then 'Rocket Timelapse - Anycubic i3 Mega'. You can reference my yt channel to see what it should be returning: https://www.youtube.com/channel/UCD8d3FGC907iS1qiMF0ccxA?view_as=subscriber
Any insights into why it changed? Or why it's not returning the data that it should be?
You experienced an API issue which is known for a few days now. Follow-up https://issuetracker.google.com/issues/128673552, or the answers already given on this site (e.g. https://stackoverflow.com/a/55246970/8327971).
Either of the two links we'll lead you to find a workaround for the issue at hand.
Note that, until now, Google has refrained itself from providing an ETA for when it'll enable back the API's features it disabled. I suppose that it may well take a few more days (perhaps weeks?) until we'll see those disabled features working again.

Chaining Futures Do Not Execute In Order

I'm currently reading variables from a Bluetooth device. This obviously takes an undetermined amount of time, so I am using futures (This method is readCharacteristic in my code down below).
More than one read operation cannot take place at a time - if a second read operation is started while a first operation is still in progress, Flutter will throw an error.
My understanding was that chaining futures together using .then() would only allow the next statement to execute when the previous call had finished. This idea seems to be true until I try to read a third value - that is when the error is thrown, because of the overlapping read events.
Here is my code:
readCharacteristic(scanDurationCharacteristic)
.then((list) => sensorScanDuration = list[0].toDouble())
.then((_) {
readCharacteristic(scanPeriodCharacteristic)
.then((list) => sensorScanPeriod = list[0].toDouble());
}).then((_) {
readCharacteristic(aggregateCharacteristic)
.then((list) => sensorAggregateCount = list[0].toDouble());
}).then((_) {
readCharacteristic(appEUICharacteristic)
.then((list) => appEUI = decimalToHexString(list));
}).then((_) {
readCharacteristic(devEUICharacteristic)
.then((list) => devEUI = decimalToHexString(list));
}).then((_) {
readCharacteristic(appKeyCharacteristic)
.then((list) => appKey = decimalToHexString(list));
});
What is a better way to ensure that these read events will not overlap?
Although R.C Howell answer is correct, prefer using async/await keywords instead. This is much more readable and you're less likely to make an error
Future<void> scanBluetooth() async {
sensorScanDuration = (await readCharacteristic(scanDurationCharacteristic))[0].toDouble();
sensorScanPeriod = (await readCharacteristic(scanPeriodCharacteristic))[0].toDouble();
sensorAggregateCount = (await readCharacteristic(aggregateCharacteristic))[0].toDouble();
appEUI = await readCharacteristic(appEUICharacteristic).then(decimalToHexString);
devEUI = await readCharacteristic(devEUICharacteristic).then(decimalToHexString);
appKey = await readCharacteristic(appKeyCharacteristic).then(decimalToHexString);
}
If you would like to chain Futures, you must return the previous Future from within the then method of the previous Future.
The documentation says to chain like so,
expensiveA()
.then((aValue) => expensiveB())
.then((bValue) => expensiveC())
.then((cValue) => doSomethingWith(cValue));
Which is the same as,
expensiveA()
.then((aValue) {
return expensiveB();
}).then((bValue) {
return expensiveC();
}).then((cValue) => doSomethingWith(cValue));
As this applies to your case,
readCharacteristic(scanDurationCharacteristic)
.then((list) {
sensorScanDuration = list[0].toDouble();
return readCharacteristic(scanPeriodCharacteristic);
}).then((list) {
sensorScanPeriod = list[0].toDouble());
return readCharacteristic(aggregateCharacteristic);
}).then((list) {
sensorAggregateCount = list[0].toDouble());
return readCharacteristic(appEUICharacteristic);
}).then((list) {
appEUI = decimalToHexString(list));
return readCharacteristic(devEUICharacteristic);
}).then((list) {
devEUI = decimalToHexString(list));
return readCharacteristic(appKeyCharacteristic);
}).then((list) => appKey = decimalToHexString(list));

ZF2/3 Load Modules from Database

I would like to know if there is a way to load modules from a database table in zend framework 2 preferable 3? I want to be able to dynamically disable or enable modules based on a status column inside a database table
I'm pretty sure you can accomplish this by attaching listener to some of ModuleManager events.
There are docs for v3 https://docs.zendframework.com/zend-modulemanager/module-manager/ and v2 https://framework.zend.com/manual/2.1/en/modules/zend.module-manager.module-manager.html
And don't forget autoloading for v3
By reading your question tom_cruz, I realize that I have exactly the same one ;-)
I went through the ZF2 source code of ModuleManager, ModuleManagerFactory, ModuleEvent and some listeners. After analyzing the flow, my new question is:
"What do I expect from an active/inactive module?"
Nearly every important stuff is done by the events Nemutaisama mentioned. i.e. loading config by adding getConfig() Method to the Module.php class.
ATM I'm not able to answer the above question. I'll come back to this one laters. But right now, I think it's an application problem, not a framework one.
I have done this some time ago by:
Create a "Core" Module responsible with fetching modules from database.
1.1 In the Module.php add module listener
public function init(ModuleManagerInterface $manager)
{
$sharedEventManger = $manager->getEventManager()->getSharedManager();
$sharedEventManger->attach(ModuleManager::class, ModuleEvent::EVENT_LOAD_MODULES_POST, new ModuleListener(), 10000);
}
Module Listener created by me was something like:
public function __invoke(ModuleEvent $event)
{
$target = $event->getTarget();
$serverName = $_SERVER['SERVER_NAME'];
if(! $serverName) { return; }
//module ok
if(! $target instanceof ModuleManagerInterface) { return; }
//config data
$configListener = $event->getConfigListener();
$config = $configListener->getMergedConfig(false);
//app modules
$modules = $target->getModules();
//select active modules
$adapter = new Adapter($config['db']);
$sql = new Sql($adapter);
$select = $sql->select(['c' => 'customers'])
->join(['cm' => 'customers_modules'], 'cm.customer_id = c.id', ['module' => 'module'])
->where(['c.domain' => $serverName])
->where(['cm.active' => 1]);
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
if($result instanceof ResultInterface && $result->isQueryResult() && $result->getAffectedRows()) {
//change db connection params here (if you use different db for customers)
while ($current = $result->current()) {
if (! in_array($current['module'], $modules)) {
try {
$target->loadModule($current['module']) ;
} catch (\RuntimeException $e) {
$target->loadModule('WQ' . str_replace($current['prefix'], '', $current['module']));
}
$modules[] = $current['module'];
$module = $target->getModule($current['module']);
if (($module instanceof ConfigProviderInterface) || (is_callable([$module, 'getConfig']))) {
$moduleConfig = $module->getConfig();
$config = ArrayUtils::merge($config, $moduleConfig);
}
}
$result->next();
}
}
$target->setModules($modules);
$configListener->setMergedConfig($config);
}
Hope it was useful.

iOS Ionic 2 app unable to read from SqlStorage

We're building an Ionic 2 (currently beta 11).
We're using built-in SqlStorage database. From Android our app is able to read and write date just fine, but with iOS we can only write the data. When we attempt to read the data we get the number of rows returned but none of the actual data.
getQueueItems(): Promise<any> {
return new Promise<any>((resolve, reject) => {
this.sql.query('SELECT * FROM queue').then(
(res) => {
console.log(res, 'result');
// resolve(sqlResult.res.rows);
}
);
}).catch(() => {
});
}
The resultset looks like this:
{
"tx": {
"db": {
"openargs": {
"name":"__ionicstorage",
"location":2,
"createFromLocation":0,
"backupFlag":2,
"existingDatabase":false,
"dblocation":"nosync"
},
"dbname":"__ionicstorage"
},
"txlock":true,
"readOnly":false,
"executes":[],
"finalized":true
},
"res": {
"rows": {
"length":3
},
"rowsAffected":0
}
}
Does anyone know how we can read from SqlStorage so that iOS gets the data?
After a lot of searching and reading tons of forum posts we finally found the answer. I'm posting here for future searchers, hopefully it will help you.
The trick is that in order for the query results to be usable by all platforms you have to iterate through the result set yourself adding the appropriate objects as you go.
this.sql.query('SELECT * FROM queue').then(
(sqlResult) => {
let queueItems = [];
for(let i = 0; i < sqlResult.res.rows.length; i++){
queueItems.push(sqlResult.res.rows.item(i));
}
resolve(queueItems);
}
);

Async Futures running in sequence to completion

I encountered the following example (Example 1 below) of Futures which caused me to wonder if I could alter the way that I was handling Futures and remove all of the nested function calls that preserve order of processing, which however result in indentation which I find a bit messy.
The altered version of my program did not work however. It did not preserve the order of processing and did not “wait” for function to complete. For example, before returning from the first call (fGetUserInput), another subsequent function was called.
Why is it that in Example 1, all of the “1st level” “new Future”s processed sequentially, however in Example 2, my altered code, the order of processing is not preserved. While the call to fGetUserInput is being processed, one of the Futures that follows it is processed?
Is it perhaps that “Example 1” only “works” because all of the statements are synchronous?
I came across a reference to “runAsync”. Can that be used to achieve what I want? (process in sequence without all of the indentation).
// Example 1. Code that I encountered for Futures //
import 'dart:async';
main() {
new Future(() => print('1'))
.then((_) => print('a'))
.then((_) => print('b'));
new Future(() => print('2'))
.then((_) => print('c'))
.then((_) => print('d'));
new Future(() => print('3'))
.then((_) =>
new Future(() => print('e'))
.then((_) => print('f'))
);
new Future(() => print('4'))
.then((_) =>
new Future(() => print('g'))
.then((_) => print('d'))
);
}
The above results in the following console output order :-
1 a b 2 c d 3 4 e f g d
Which I thought made sense.
Therefore, I modified my code to test it as follows :-
// Example 2. Altered version of my code which //
// does not preserve the order of processing, //
// which is necessary for program to function. //
new async.Future(() => fGetUserInput())
.then((lInput) {
iMaxIters = int.parse(lInput[4]);
tClearTable = (lInput[5] == "y");
iDivisor = fInitialize(iMaxIters);
tgPrint = false; // printing off
sUri =
"postgres://${lInput[1]}:${lInput[2]}#localhost:5432/${lInput[3]}";
sStartTime = lInput[7];
})
.catchError((oError) => fFatal("Get User Input", oError));
new async.Future(() => fConnectToDb(sUri, sStartTime))
.then((bool tConnected) {
if (ogDb == null)
fFatal("Unable to connect to database", "");
print ("Processing database ......");
})
.catchError((oError) => fFatal("Connect to Db", oError));
new async.Future(() => fClearTable(tClearTable))
.then((sResult) => print (sResult+"\n"))
.catchError((oError) => fFatal("Clear Table", oError));
new async.Future(() => fProcessInserts(iMaxIters, iDivisor))
.then((sResult) => print (""))
.catchError((oError) => fFatal("Process Inserts", oError));
new async.Future(() => fSetupRandKeys())
.then((sResult) => print (""))
.catchError((oError) => fFatal("Setup Random Keys", oError));
new async.Future(() => fProcessUpdates(iMaxIters, iDivisor))
.then((sResult) {
String sTotValue = fFormatAmount(igGrandTotAmt, true, 2);
fPrint ("Grand Total added to database = \$${sTotValue}");
ogDb.close();
exit(0);
})
.catchError((oError) => fFatal("Process Updates", oError));
}
void fFatal (String sMessage, Error oError) {
print("\n\nFatal Error. $sMessage\n${oError}");
exit(1);
}
async.Future<String> fProcessInserts(int iMaxIters, int iDiv) {
async.Completer oCompleter = new async.Completer<String>();
int iTot = 0;
Function fLoop;
print ("\nProcessing Inserts ......");
fResetAndStartWatch();
The following is my code prior to the above changes, and the following Example 3 appears to work OK. I don't like the extent of indentation, and in situations with more function calls, that would increase the extent of indentation. I was hoping for a more elegant way to do it.
// Example 3: The original version of my code //
// which does preserve the order of processing //
void main() {
print("");
String sCheckPoint = "Get User Input";
fGetUserInput()
.then((lInput) {
int iMaxIters = int.parse(lInput[4]);
bool tClearTable = (lInput[5] == "y");
int iDiv = fInitialize(iMaxIters);
tgPrint = false; // printing off
String sUri =
"postgres://${lInput[1]}:${lInput[2]}#localhost:5432/${lInput[3]}";
sCheckPoint = "Connect to Database";
fConnectToDb(sUri, lInput[7]).then((bool tConnected) {
if (ogDb == null)
fFatal(sCheckPoint, "Unable to conenct to Db");
print ("Processing database ......");
sCheckPoint = "Clear Table";
fClearTable(tClearTable).then((sResult) {
print (sResult+"\n");
sCheckPoint = "Process Inserts";
fProcessInserts(iMaxIters, iDiv).then((sResult) {
print;
sCheckPoint = "Set-up Random Keys";
fSetupRandKeys().then((sResult) {
print;
sCheckPoint = "Process Updates";
fProcessUpdates(iMaxIters, iDiv).then((sResult) {
String sTotValue = fFormatAmount(igGrandTotAmt, true, 2);
fPrint ("Grand Total added to database = \$${sTotValue}");
ogDb.close();
exit(0);
});
});
});
});
});
})
.catchError((oError) => fFatal(sCheckPoint, oError));
}
void fFatal (String sMessage, Error oError) {
print("\n\nFatal Error. $sMessage\n${oError}");
exit(1);
}
async.Future<String> fProcessInserts(int iMaxIters, int iDiv) {
async.Completer oCompleter = new async.Completer<String>();
int iTot = 0;
Function fLoop;
print ("Processing Inserts ......");
fResetAndStartWatch();
Remember that you can chain futures, which will reduce your indentation by quite a bit.
The downside is that you don't get nested scopes, which can be useful if you have more than one value to propagate between async blocks, but that can be worked around in a few ways.
Here's you example 3 with chaining:
// Example 3 with chaining
void main() {
String checkPoint = "Get User Input";
getUserInput().then((input) {
int maxIters = int.parse(input[4]);
bool clearTable = (input[5] == "y");
int div = initialize(maxIters);
shouldPrint = false; // printing off
String uri =
"postgres://${input[1]}:${input[2]}#localhost:5432/${input[3]}";
checkPoint = "Connect to Database";
return connectToDb(uri, input[7]).then((bool connected) {
if (db == null)
fatal(checkPoint, "Unable to conenct to Db");
print ("Processing database ......");
checkPoint = "Clear Table";
return clearTable(shouldClearTable);
}).then((result) {
print (result+"\n");
checkPoint = "Process Inserts";
return processInserts(maxIters, div);
}).then((result) {
print('');
checkPoint = "Set-up Random Keys";
return setupRandKeys();
}).then((result) {
print('');
checkPoint = "Process Updates";
return processUpdates(maxIters, div);
}).then((result) {
String totValue = formatAmount(grandTotAmt, true, 2);
print("Grand Total added to database = \$${totValue}");
return db.close();
// exit(0); pretty much never call exit()
});
}).catchError((error) => fatal(checkPoint, error));
}
Edit: Oops, looking more closely I got bit by the scoping problem... I added a level of nesting just to capture the needed vars in a scope accessible by the following blocks. I'm also removing the hungarian-ish notation, because... don't do that in Dart :)

Resources