modifying documentation, simplifications to BulkEmailJob.php, a=chris

Chris Pollett [2015-12-05 23:Dec:th]
modifying documentation, simplifications to BulkEmailJob.php, a=chris
Filename
src/configs/Config.php
src/library/MailServer.php
src/library/MediaConstants.php
src/library/media_jobs/BulkEmailJob.php
src/library/media_jobs/VideoConvertJob.php
diff --git a/src/configs/Config.php b/src/configs/Config.php
index 169718eeb..f8d6dd5b5 100755
--- a/src/configs/Config.php
+++ b/src/configs/Config.php
@@ -801,14 +801,6 @@ nsdefine('STANDARD_GROUP_ITEM', 0);
 if(!nsdefined("SEND_MAIL_MEDIA_UPDATER")) {
     nsdefine('SEND_MAIL_MEDIA_UPDATER', false);
 }
-/**
- *  This value will block the name server for some time
- *  before sending mails to next mailer batch. One can set this to 0
- *  if one does not want name server to stop.
- */
-if(!nsdefined("MAIL_SENDING_INTERVAL")) {
-    nsdefine('MAIL_SENDING_INTERVAL', 60);
-}
 /**
  *  Indicates the thread was created to go alongside the creation of a wiki
  *  page so that people can discuss the pages contents
diff --git a/src/library/MailServer.php b/src/library/MailServer.php
index 3f06003ae..263e0343a 100644
--- a/src/library/MailServer.php
+++ b/src/library/MailServer.php
@@ -349,19 +349,20 @@ class MailServer implements MediaConstants
         if ($file_count > 0) {
             $file = end($files);
             $file_name = str_replace($mail_directory."/", "", $file);
-                 $new_file_name = substr($file_name, 0, -4);
-            $diff = $current_time - $new_file_name;
+            $last_file_time = substr($file_name, 0, -4);
+            $diff = $current_time - $last_file_time;
         }
         $mail_details = serialize(array($subject, $from, $to, $message));
         $this->messages = "Queuing: " . $mail_details;
         if ($diff > C\MAX_MAIL_TIMESTAMP_LIMIT || $file_count == 0)
         {
+            crawlLog("...Creating a new file for next mailer batch!\n");
             $file_time = time();
             $fp = fopen($mail_directory . "/" . $file_time . ".txt", "a+");
             if (flock($fp, LOCK_EX | LOCK_NB)) {
                 crawlLog("....Lock for mail file acquired!" .
                     " Sending emails!\n");
-                fwrite($fp, self::MESSAGE_SEPARATOR .$mail_details);
+                fwrite($fp, self::MESSAGE_SEPARATOR . $mail_details);
                 fwrite($fp, PHP_EOL);
                 flock($fp, LOCK_UN);
             } else {
@@ -369,8 +370,7 @@ class MailServer implements MediaConstants
                     " for $file_time.txt!\n");
             }
         } else {
-            crawlLog("...Creating a new file for next mailer batch!\n");
-            $fp = fopen($mail_directory."/".$new_file_name.".txt", "a+");
+            $fp = fopen($mail_directory."/".$last_file_time.".txt", "a+");
             if (flock($fp, LOCK_EX | LOCK_NB)) {
                 crawlLog("....Lock acquired! Sending emails now!\n");
                 fwrite($fp, $mail_details);
diff --git a/src/library/MediaConstants.php b/src/library/MediaConstants.php
index fb7183b02..7e9c0772f 100644
--- a/src/library/MediaConstants.php
+++ b/src/library/MediaConstants.php
@@ -65,11 +65,6 @@ interface MediaConstants
      * names to concatenate them.
      */
     const ASSEMBLE_FILE = "/ready_to_assemble.txt";
-    /**
-     * The text file used to recognize that the file has
-     * been concatenated.
-     */
-    const CONCATENATED_FILE = "/concatenated.txt";
     /**
      * Used to place text files(mailer lists) for sending
      * in batches.
diff --git a/src/library/media_jobs/BulkEmailJob.php b/src/library/media_jobs/BulkEmailJob.php
index b53fc3702..b43ff592f 100644
--- a/src/library/media_jobs/BulkEmailJob.php
+++ b/src/library/media_jobs/BulkEmailJob.php
@@ -119,7 +119,7 @@ class BulkEmailJob extends MediaJob
     {
         if (!isset($tasks["name"]) || !isset($tasks["data"])) {
             L\crawlLog("...Email Task received incomplete !");
-            return null;
+            return false;
         }
         L\crawlLog("----Email file name: {$tasks['name']}");
         $emails = explode(self::MESSAGE_SEPARATOR, $tasks["data"]);
@@ -131,7 +131,7 @@ class BulkEmailJob extends MediaJob
                     $email[0], $email[1], $email[2], $email[3]);
             }
         }
-        return $tasks["name"];
+        return false;
     }
     /**
      * Handles the request to get the mailer list file for
@@ -150,54 +150,22 @@ class BulkEmailJob extends MediaJob
             return false;
         }
         $files = glob($mail_directory."/*.txt");
-        foreach($files as $file){
-            $file_name = str_replace($mail_directory."/","", $file);
-            if (strpos($file_name, 'taken') !== false) {
-                continue;
-            } else {
-                $is_taken_file = $mail_directory."/taken-".$file_name;
-                if (in_array($is_taken_file, $files)) {
-                    continue;
-                } else {
-                    $fp = fopen($mail_directory."/".$file_name, "a+");
-                    if (flock($fp, LOCK_EX | LOCK_NB)) {
-                        $taken_file_name = "taken-".$file_name;
-                        file_put_contents($mail_directory."/".$taken_file_name,
-                             $machine_id);
-                        $task = [];
-                        $task["name"] = $file_name;
-                        $task["data"] = file_get_contents($file);
-                        return $task;
-                    } else {
-                        continue;
-                    }
-                }
+        $sendable_file = false;
+        foreach($files as $email_file) {
+            if(time() - filemtime($email_file) >
+                C\MAX_MAIL_TIMESTAMP_LIMIT) {
+                $sendable_file = $email_file;
+                break;
             }
         }
-        return false;
-    }
-    /**
-     * Handles request to unlock the mailing list file
-     * and delete it.
-     *
-     * @param int $machine_id id of machine which is done sending emails
-     * @param array $data file name to unlock
-     */
-    public function putTasks($machine_id, $data = null)
-    {
-        if (empty($data)) {
-            return "No file name sent";
-        }
-        $file_name = $data;
-        if (!preg_match("/\d+\.txt/", $file_name)) {
-            return "Invalid file name: $file_name";
-        }
-        $mail_directory = C\WORK_DIRECTORY . self::MAIL_FOLDER;
-        if (!file_exists($mail_directory. "/" . $file_name)) {
-            return "File $file_name does not exist";
+        if (!$sendable_file) {
+            return false;
         }
-        unlink($mail_directory. "/" . $file_name);
-        unlink($mail_directory. "/" . "taken-".$file_name);
-        return "Email task completed";
+        $file_name = str_replace($mail_directory."/","", $sendable_file);
+        $task = [];
+        $task["name"] = $file_name;
+        $task["data"] = file_get_contents($sendable_file);
+        unlink($file);
+        return $task;
     }
 }
diff --git a/src/library/media_jobs/VideoConvertJob.php b/src/library/media_jobs/VideoConvertJob.php
index 6634d73b4..e539071a7 100644
--- a/src/library/media_jobs/VideoConvertJob.php
+++ b/src/library/media_jobs/VideoConvertJob.php
@@ -88,9 +88,11 @@ class VideoConvertJob extends MediaJob
        $this->concatenateVideos();
     }
     /**
-     * Checks name server for a video segment to convert. If there are
-     * converts the mov or avi segment file to an mp4 file
+     * Called from run() with conversion tasks from name server. If there are
+     * any  mov or avi segmentconverts them mp4
      * This function would only be called by client media updaters.
+     *
+     * @param array $tasks
      */
     public function doTasks($tasks)
     {
@@ -264,8 +266,7 @@ class VideoConvertJob extends MediaJob
             mkdir($converted_folder);
         }
         foreach (glob($converted_folder."/*") as $video_path) {
-            if (file_exists($video_path . self::CONCATENATED_FILE) ||
-                file_exists($video_path . self::ASSEMBLE_FILE)) {
+            if (file_exists($video_path . self::ASSEMBLE_FILE)) {
                 continue;
             }
             if (!file_exists($video_path.self::COUNT_FILE)) {
@@ -394,10 +395,6 @@ class VideoConvertJob extends MediaJob
         if (!$data) {
             return "No data received by web server.";
         }
-        if (file_exists($converted_folder . "/" .
-            $folder_name . self::CONCATENATED_FILE)) {
-            return "";
-        }
         $upload_flag = false;
         if (file_exists($converted_folder . "/" . $folder_name)){
             if (file_exists($upload_path)){
@@ -430,6 +427,10 @@ class VideoConvertJob extends MediaJob
      * conversion. This selection is based upon if the file was taken
      * previously or not. If it was then its timestamp is checked.
      * Otherwise new file is sent for conversion along with its folder name.
+     *
+     * @param int $machine_id not used
+     * @param array $data not used
+     * @return array an associate array with info on a file to convert
      */
     public function getTasks($machine_id, $data = null)
     {
ViewGit