@@ -6,6 +6,7 @@ class Algolia_Algoliasearch_Model_Queue
66 const ERROR_LOG = 'algoliasearch_queue_errors.log ' ;
77
88 protected $ table ;
9+ protected $ logTable ;
910
1011 /** @var Magento_Db_Adapter_Pdo_Mysql */
1112 protected $ db ;
@@ -29,12 +30,16 @@ class Algolia_Algoliasearch_Model_Queue
2930
3031 private $ noOfFailedJobs = 0 ;
3132
33+ private $ logRecord = array ();
34+
3235 public function __construct ()
3336 {
3437 /** @var Mage_Core_Model_Resource $coreResource */
3538 $ coreResource = Mage::getSingleton ('core/resource ' );
3639
3740 $ this ->table = $ coreResource ->getTableName ('algoliasearch/queue ' );
41+ $ this ->logTable = $ this ->table .'_log ' ;
42+
3843 $ this ->db = $ coreResource ->getConnection ('core_write ' );
3944
4045 $ this ->config = Mage::helper ('algoliasearch/config ' );
@@ -47,6 +52,7 @@ public function add($class, $method, $data, $data_size)
4752 {
4853 // Insert a row for the new job
4954 $ this ->db ->insert ($ this ->table , array (
55+ 'created ' => date ('Y-m-d H:i:s ' ),
5056 'class ' => $ class ,
5157 'method ' => $ method ,
5258 'data ' => json_encode ($ data ),
@@ -55,19 +61,38 @@ public function add($class, $method, $data, $data_size)
5561 ));
5662 }
5763
58- public function runCron ()
64+ public function runCron ($ nbJobs = null , $ force = false )
5965 {
60- if (!$ this ->config ->isQueueActive ()) {
66+ if (!$ this ->config ->isQueueActive () && $ force === false ) {
6167 return ;
6268 }
6369
64- $ nbJobs = $ this ->config ->getNumberOfJobToRun ();
70+ $ this ->clearOldLogRecords ();
71+
72+ $ this ->logRecord = array (
73+ 'started ' => date ('Y-m-d H:i:s ' ),
74+ 'processed_jobs ' => 0 ,
75+ 'with_empty_queue ' => 0 ,
76+ );
77+
78+ $ started = time ();
79+
80+ if ($ nbJobs === null ) {
81+ $ nbJobs = $ this ->config ->getNumberOfJobToRun ();
82+ if (getenv ('EMPTY_QUEUE ' ) && getenv ('EMPTY_QUEUE ' ) == '1 ' ) {
83+ $ nbJobs = -1 ;
6584
66- if ( getenv ( ' EMPTY_QUEUE ' ) && getenv ( ' EMPTY_QUEUE ' ) == ' 1 ' ) {
67- $ nbJobs = - 1 ;
85+ $ this -> logRecord [ ' with_empty_queue ' ] = 1 ;
86+ }
6887 }
6988
7089 $ this ->run ($ nbJobs );
90+
91+ $ this ->logRecord ['duration ' ] = time () - $ started ;
92+
93+ $ this ->db ->insert ($ this ->logTable , $ this ->logRecord );
94+
95+ $ this ->db ->closeConnection ();
7196 }
7297
7398 public function run ($ maxJobs )
@@ -77,7 +102,7 @@ public function run($maxJobs)
77102 $ jobs = $ this ->getJobs ($ maxJobs , $ pid );
78103
79104 if (empty ($ jobs )) {
80- $ this -> db -> closeConnection () ;
105+ return ;
81106 }
82107
83108 // Run all reserved jobs
@@ -96,6 +121,8 @@ public function run($maxJobs)
96121 $ model = Mage::getSingleton ($ job ['class ' ]);
97122 $ method = $ job ['method ' ];
98123 $ model ->{$ method }(new Varien_Object ($ job ['data ' ]));
124+
125+ $ this ->logRecord ['processed_jobs ' ] += count ($ job ['merged_ids ' ]);
99126 } catch (\Exception $ e ) {
100127 $ this ->noOfFailedJobs ++;
101128
@@ -119,8 +146,6 @@ public function run($maxJobs)
119146
120147 return ;
121148 }
122-
123- $ this ->db ->closeConnection ();
124149 }
125150
126151 private function getJobs ($ maxJobs , $ pid )
@@ -381,4 +406,14 @@ private function maxValueInArray($array, $keyToSearch)
381406
382407 return $ currentMax ;
383408 }
409+
410+ private function clearOldLogRecords ()
411+ {
412+ $ idsToDelete = $ this ->db ->query ("SELECT id FROM {$ this ->logTable } ORDER BY started DESC, id DESC LIMIT 25000, " .PHP_INT_MAX )
413+ ->fetchAll (\PDO ::FETCH_COLUMN , 0 );
414+
415+ if ($ idsToDelete ) {
416+ $ this ->db ->query ("DELETE FROM {$ this ->logTable } WHERE id IN ( " . implode (", " , $ idsToDelete ) . ") " );
417+ }
418+ }
384419}
0 commit comments