public function __construct($script_name)
 {
     $this->script_name = $script_name;
     $this->register($script_name);
     SET_CONTEXT("FS");
     $MAX_ITERATIONS_DUE_TO_PROPEL_MEMORY_LEAK = 10000000;
     self::initDb();
     list($sleep_between_cycles, $number_of_times_to_skip_writing_sleeping) = self::getSleepParams('app_flatten_');
     $last_worker_count = 0;
     $iteration = 0;
     $c = new Criteria();
     $currentDc = kDataCenterMgr::getCurrentDc();
     $c->add(BatchJobPeer::DC, kDataCenterMgr::getCurrentDcId());
     $c->add(BatchJobPeer::JOB_TYPE, BatchJobType::FLATTEN);
     $c->add(BatchJobPeer::STATUS, BatchJob::BATCHJOB_STATUS_PROCESSED);
     $temp_count = 0;
     while (1) {
         self::exitIfDone();
         try {
             sleep($sleep_between_cycles);
             $jobs = BatchJobPeer::doSelect($c);
             foreach ($jobs as $job) {
                 $data = json_decode($job->getData(true), true);
                 $entry_id = $data['entryId'];
                 $entry_int_id = $data['entryIntId'];
                 $entry_version = $data['entryVersion'];
                 $file_format = $data['fileFormat'];
                 $entry = entryPeer::retrieveByPK($entry_id);
                 if (!$entry) {
                     // entry is probably deleted if it is not returned from retrieveByPK
                     // close job as failed
                     $job->setStatus(BatchJob::BATCHJOB_STATUS_FAILED);
                     $job->setDescription("could not retrieve entry, probably deleted");
                     KalturaLog::debug("could not retrieve entry {$entry_id} , probably deleted");
                     $job->save();
                     continue;
                 }
                 $fileSyncKey = $entry->getSyncKey(entry::FILE_SYNC_ENTRY_SUB_TYPE_DOWNLOAD, $file_format);
                 $fullFinalPath = kFileSyncUtils::getLocalFilePathForKey($fileSyncKey);
                 $finalPathNoExt = substr($fullFinalPath, 0, strlen($fullFinalPath) - strlen($file_format));
                 kFile::fullMkdir($fullFinalPath);
                 $wildcardFinalPath = $finalPathNoExt . "*";
                 $older_files = glob($wildcardFinalPath);
                 foreach ($older_files as $older_file) {
                     KalturaLog::debug("removing old file: [{$older_file}]");
                     @unlink($older_file);
                 }
                 KalturaLog::debug("Downloading: {$fullFinalPath}");
                 KCurlWrapper::getDataFromFile($data["serverUrl"], $fullFinalPath);
                 if (!file_exists($fullFinalPath)) {
                     KalturaLog::debug("file doesnt exist: " . $data["serverUrl"]);
                     $job->setDescription("file doesnt exist: " . $data["serverUrl"]);
                     $job->setStatus(BatchJob::BATCHJOB_STATUS_FAILED);
                 } else {
                     if (filesize($fullFinalPath) < 100000) {
                         @unlink($fullFinalPath);
                         KalturaLog::debug("file too small: " . $data["serverUrl"]);
                         $job->setDescription("file too small: " . $data["serverUrl"]);
                         $job->setStatus(BatchJob::BATCHJOB_STATUS_FAILED);
                     } else {
                         if ($data['email']) {
                             $downloadLink = $entry->getDownloadUrl() . '/format/' . $file_format;
                             kJobsManager::addMailJob(null, $entry_id, $entry->getPartnerId(), self::KALTURAS_FLATTEN_READY, kMailJobData::MAIL_PRIORITY_NORMAL, kConf::get("batch_flatten_video_sender_email"), kConf::get("batch_flatten_video_sender_name"), $data['email'], array($data['email'], $downloadLink));
                         }
                         KalturaLog::debug("Deleting: " . $data["deleteUrl"]);
                         kFile::downloadUrlToString($data["deleteUrl"]);
                         myNotificationMgr::createNotification(kNotificationJobData::NOTIFICATION_TYPE_ENTRY_UPDATE, $entry);
                         $job->setStatus(BatchJob::BATCHJOB_STATUS_FINISHED);
                         list($rootPath, $filePath) = kFileSyncUtils::getLocalFilePathArrForKey($fileSyncKey);
                         $fileFullPath = $rootPath . $filePath;
                         if (file_exists($fileFullPath)) {
                             try {
                                 kFileSyncUtils::createSyncFileForKey($rootPath, $filePath, $fileSyncKey);
                             } catch (Exception $ex) {
                                 KalturaLog::debug("ignore ERROR: " . $ex->getMessage());
                             }
                         } else {
                             KalturaLog::debug("The file [{$fileFullPath}] doesn't exists, not creating FileSync");
                         }
                     }
                 }
                 $job->save();
             }
         } catch (Exception $ex) {
             KalturaLog::debug("ERROR: " . $ex->getMessage());
             self::initDb(true);
             self::failed();
         }
         if ($temp_count == 0) {
             KalturaLog::debug("Ended conversion. sleeping for a while (" . $sleep_between_cycles . " seconds). Will write to the log in (" . $sleep_between_cycles * $number_of_times_to_skip_writing_sleeping . ") seconds");
         }
         $temp_count++;
         if ($temp_count >= $number_of_times_to_skip_writing_sleeping) {
             $temp_count = 0;
         }
     }
 }