protected function _dispatch($chunk = 50) { $mail = new Mail(); $notify = new Notify(); foreach ($notify->mail_dequeue($chunk) as $doc) { if ($doc['tries'] == self::MAX_TRIES) { continue; } try { if ($mail->to($doc['to'])->from($doc['from'])->reply_to(isset($doc['reply_to']) ? $doc['reply_to'] : $doc['from'])->subject($doc['subject'])->message($doc['message'])->send()) { $notify->mail_update($doc['_id'], array('processing' => false, 'sent' => true)); } else { $notify->mail_update($doc['_id'], array('processing' => false, 'tries' => $doc['tries'] + 1)); } } catch (\Exception $e) { echo $e->getTraceAsString() . PHP_EOL; } } }
/** * Sync sfa database to my database * @param string $display * @return boolean */ public function sync($display = false) { $this->log('Synchronization started ' . date('Y-m-d H:m:s') . "\n"); \DB::table('settings')->where('name', 'synching_sfi')->update(['value' => 1]); try { $dbh = new PDO("dblib:host={$this->host}:{$this->port};dbname={$this->database}", $this->dbuser, $this->dbpass); $configTables = config('sync.sync_tables'); $tables = array_keys($configTables); $limit = 1000; foreach ($tables as $table) { //Delete records from local database \DB::table($table)->whereNull('updated_at')->delete(); $ids = []; if ($keys = $configTables[$table]) { $records = \DB::table($table)->get($keys); $pKey = array_shift($keys); foreach ($records as $record) { $ids[] = $record->{$pKey}; } } $query = 'SELECT * FROM ' . $table; if ($ids) { //exclude records $query .= ' WHERE ' . $pKey . ' NOT IN(' . implode(',', $ids) . ')'; } $stmt = $dbh->prepare($query); $stmt->execute(); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); $data = $this->formatData($data); $count = count($data); if ($count > $limit) { foreach (array_chunk($data, $limit, true) as $row) { // Import data to local database \DB::table($table)->insert($row); } $msg = "{$table} : inserted " . count($data) . " records.\n"; $this->log($msg); if ($display) { echo $msg; } } else { // Import data to local database \DB::table($table)->insert($data); $msg = "{$table} : inserted " . count($data) . " records.\n"; $this->log($msg); if ($display) { echo $msg; } } unset($data); } } catch (PDOException $e) { $this->log('Error :' . $e->getMessage()); //$email = config('system.error_email'); if ($email) { $email = explode(',', $email); $data['email'] = $email; $data['errors'] = $e->getMessage(); \Mail::send('emails.error', $data, function ($m) use($email) { $m->from(config('system.from_email'), config('system.from')); $m->to($email)->subject('Application Error'); }); } return false; } \DB::table('settings')->where('name', 'synching_sfi')->update(['value' => 0]); $this->log('Synchronization ended ' . date('Y-m-d H:m:s') . "\n"); // update report summary columns PresenterFactory::getInstance('Reports')->updateReportSummary(); return true; }