cronjob tickets first version, processing tbd

This commit is contained in:
Xenomporio 2022-07-21 16:30:59 +02:00
parent 68965e87cd
commit 6cbc0bd08e

View File

@ -14,15 +14,14 @@ use Xentral\Modules\Ticket\Task\TicketImportHelperFactory;
$DEBUG = 0; $DEBUG = 0;
/*
$debugfile = "/var/www/html/Xenomporio/debug.txt"; $debugfile = "/var/www/html/Xenomporio/debug.txt";
function file_append($filename,$text) { function file_append($filename,$text) {
$oldtext = file_get_contents($filename); $oldtext = file_get_contents($filename);
file_put_contents($filename,$oldtext.$text); file_put_contents($filename,$oldtext.$text);
} }
file_put_contents($debugfile,"0"); file_put_contents($debugfile,"0");
*/
/** @var ApplicationCore $app */ /** @var ApplicationCore $app */
@ -43,9 +42,6 @@ if($mutex){
SET `mutexcounter`=`mutexcounter`+1 SET `mutexcounter`=`mutexcounter`+1
WHERE `mutex` = 1 AND (`parameter` = '".$cronjobname."')" WHERE `mutex` = 1 AND (`parameter` = '".$cronjobname."')"
); );
file_append($debugfile,"MUTEX");
return; return;
} }
$app->DB->Update( $app->DB->Update(
@ -58,9 +54,6 @@ $app->DB->Update(
$accountGateway = $app->Container->get('EmailAccountGateway'); $accountGateway = $app->Container->get('EmailAccountGateway');
$accounts = $accountGateway->getAccountsWithTicketActive(); $accounts = $accountGateway->getAccountsWithTicketActive();
file_append($debugfile,"Accs:".count($accounts).";");
// only load services if there is at least one account to import (performance) // only load services if there is at least one account to import (performance)
$ticketModule = null; $ticketModule = null;
$factory = null; $factory = null;
@ -86,51 +79,28 @@ foreach ($accounts as $account) {
'Start imap ticket import for {email}', 'Start imap ticket import for {email}',
['email' => $account->getEmailAddress(), 'account' => $account] ['email' => $account->getEmailAddress(), 'account' => $account]
); );
file_append($debugfile,"Account ".$account->getemailAddress());
// create mail client // create mail client
try { try {
$mailConfig = $configProvider->createImapConfigFromAccount($account); $mailConfig = $configProvider->createImapConfigFromAccount($account);
$mailClient = $factory->createImapClient($mailConfig); $mailClient = $factory->createImapClient($mailConfig);
} catch (Exception $e) { } catch (Exception $e) {
$logger->error('Failed to create email client', ['error' => (string)$e, 'account' => $account]); $logger->error('Failed to create email client', ['error' => (string)$e, 'account' => $account]);
file_append($debugfile,"Failed 1");
continue; continue;
} }
file_append($debugfile,"Connect to ".."SSL: ".$configProvider->isSslEnabled()." auth ".getAuthType()."\n");
// connect mail client // connect mail client
try {
try { try {
$mailClient->connect(); $mailClient->connect();
file_append($debugfile,"Meh");
} catch (Exception $e) { } catch (Exception $e) {
$logger->error('Error during imap connection', ['error' => (string)$e, 'account' => $account]); $logger->error('Error during imap connection', ['error' => (string)$e, 'account' => $account]);
file_append($debugfile,"Error ".(string)$e);
continue; continue;
} }
}
file_append($debugfile,"2");
// connet to INBOX folder // connet to INBOX folder
try { try {
$mailClient->selectFolder('INBOX'); $mailClient->selectFolder('INBOX');
} catch (Exception $e) { } catch (Exception $e) {
$logger->error('Failed to select INBOX folder', ['error' => (string)$e, 'account' => $account]); $logger->error('Failed to select INBOX folder', ['error' => (string)$e, 'account' => $account]);
file_append($debugfile,"Failed 2");
continue; continue;
} }
@ -147,17 +117,11 @@ foreach ($accounts as $account) {
$criteria = 'UNSEEN'; $criteria = 'UNSEEN';
} }
file_append($debugfile,"3");
// search new messages // search new messages
try { try {
$searchResult = $mailClient->searchMessages($criteria); $searchResult = $mailClient->searchMessages($criteria);
} catch (Exception $e) { } catch (Exception $e) {
$logger->error('Error during imap search', ['exception' => $e]); $logger->error('Error during imap search', ['exception' => $e]);
file_append($debugfile,"Failed 3");
continue; continue;
} }
$logger->debug('unread emails to import: {message_count}', ['message_count' => count($searchResult)]); $logger->debug('unread emails to import: {message_count}', ['message_count' => count($searchResult)]);
@ -171,7 +135,7 @@ foreach ($accounts as $account) {
); );
} }
$importer = $importHelperFactory->create($mailClient, $account, $projectId); $importer = $importHelperFactory->create($mailClient, $account, $projectId);
$insertedMailsCount = $importer->importMessages($searchResult); $insertedMailsCount = $importer->importMessages($searchResult); // TicketImportHelper->importMessages()
$totalEmailsImportCount += $insertedMailsCount; $totalEmailsImportCount += $insertedMailsCount;
// set mutex if the total amount of imported emails is more than 10 // set mutex if the total amount of imported emails is more than 10
@ -192,10 +156,6 @@ foreach ($accounts as $account) {
) { ) {
$logger->error('Tickets error'); $logger->error('Tickets error');
file_append($debugfile,"Failed 5");
return; return;
} }
$app->DB->Update( $app->DB->Update(
@ -209,4 +169,3 @@ $app->DB->Update(
"UPDATE `prozessstarter` SET `mutex`=0,`mutexcounter`=0 WHERE (`parameter` = '".$cronjobname."')" "UPDATE `prozessstarter` SET `mutex`=0,`mutexcounter`=0 WHERE (`parameter` = '".$cronjobname."')"
); );
file_append($debugfile,"END");