فهرست منبع

Ticket #5291 - Improve uploader and storage collaboration. P2.

AntonLV 10 ماه پیش
والد
کامیت
93c79e2abd

+ 10 - 2
inc/classes/BxDolCronStorage.php

@@ -14,8 +14,16 @@ class BxDolCronStorage extends BxDolCron
         set_time_limit(36000);
         ignore_user_abort();
 
-        if (BxDolStorage::pruneDeletions()) // if any files were deleted
-            BxDolInstallerUtils::checkModulesPendingUninstall(); // try to uninstall modules pending for uninstall
+        /**
+         * if any files were deleted, try to uninstall modules pending for uninstall
+         */
+        if (BxDolStorage::pruneDeletions())
+            BxDolInstallerUtils::checkModulesPendingUninstall();
+
+        /**
+         * delete outdated ghosts
+         */
+        BxDolStorage::pruneGhosts();
     }
 }
 

+ 23 - 1
inc/classes/BxDolStorage.php

@@ -59,6 +59,8 @@ define('BX_DOL_STORAGE_DEFAULT_ICON_FONT', 'far file'); ///< default font icon i
 
 define('BX_DOL_STORAGE_QUEUED_DELETIONS_PER_RUN', 200); ///< max number of file deletions per one cron run, @see BxDolStorage::pruneDeletions
 
+define('BX_DOL_STORAGE_GHOST_LIFETIME', 600); ///< ununsed ghost lifetime, @see BxDolStorage::pruneGhosts
+
 /**
  * This class unify storage.
  * As the result there are many advantages:
@@ -262,7 +264,7 @@ abstract class BxDolStorage extends BxDolFactory implements iBxDolFactoryObject
 
     /**
      * Delete files queued for deletions
-     * It is alutomatically called upin cron execution, usually one time per minute.
+     * It is alutomatically called upon cron execution, usually one time per minute.
      * Max number of deletetion per time is defined in @see BX_DOL_STORAGE_QUEUED_DELETIONS_PER_RUN
      * @return number of deleted records
      */
@@ -277,6 +279,26 @@ abstract class BxDolStorage extends BxDolFactory implements iBxDolFactoryObject
 
         return $iDeleted;
     }
+    
+    /**
+     * Delete outdated ghosts
+     * It is alutomatically called upon cron execution, usually one time per minute.
+     */
+    public static function pruneGhosts()
+    {
+        $iLifetime = ($iLifetime = getParam('sys_storage_ghost_lifetime')) !== false ? (int)$iLifetime * 60 : BX_DOL_STORAGE_GHOST_LIFETIME;
+        if($iLifetime == 0)
+            return;
+
+        $iDeleted = 0;
+
+        $aGhosts = BxDolStorageQuery::getOutdatedUnusedGhosts($iLifetime);
+        foreach($aGhosts as $aGhost)
+            if(($oStorage = BxDolStorage::getObjectInstance($aGhost['object'])) !== false)
+                $iDeleted += $oStorage->deleteFile($aGhost['id']);
+
+        return $iDeleted;
+    }
 
     /**
      * Check if module has any files pending for deletion, it is supposed that all module storage object names are prefixed with module name

+ 7 - 0
inc/classes/BxDolStorageQuery.php

@@ -426,6 +426,13 @@ class BxDolStorageQuery extends BxDolDb
         $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `sys_storage_deletions` WHERE `object` LIKE ?", $sPrefix . '%');
         return $oDb->getOne($sQuery);
     }
+
+    public static function getOutdatedUnusedGhosts($iLifetime)
+    {
+        return BxDolDb::getInstance()->getAll("SELECT * FROM `sys_storage_ghosts` WHERE `content_id` = '0' AND `created` < (UNIX_TIMESTAMP() - :lifetime)", [
+            'lifetime' => $iLifetime
+        ]);
+    }
 }
 
 /** @} */

+ 3 - 1
install/sql/system.sql

@@ -684,7 +684,9 @@ INSERT INTO `sys_options`(`category_id`, `name`, `caption`, `value`, `type`, `ex
 (@iCategoryId, 'sys_storage_s3_region', '_adm_stg_cpt_option_sys_storage_s3_region', '', 'digit', '', '', '', 8),
 (@iCategoryId, 'sys_storage_s3_amz_iam_role', '_adm_stg_cpt_option_sys_storage_s3_amz_iam_role', '', 'checkbox', '', '', '', 10),
 (@iCategoryId, 'sys_storage_s3_acl_enable', '_adm_stg_cpt_option_sys_storage_s3_acl_enable', 'on', 'checkbox', '', '', '', 12),
-(@iCategoryId, 'sys_storage_s3_force_auth_urls', '_adm_stg_cpt_option_sys_storage_s3_force_auth_urls', '', 'digit', '', '', '', 14);
+(@iCategoryId, 'sys_storage_s3_force_auth_urls', '_adm_stg_cpt_option_sys_storage_s3_force_auth_urls', '', 'digit', '', '', '', 14), 
+(@iCategoryId, 'sys_storage_ghost_lifetime', '_adm_stg_cpt_option_sys_storage_ghost_lifetime', '5', 'digit', '', '', '', 20);
+
 --
 -- CATEGORY: Account
 --

+ 1 - 1
modules/boonex/english/data/langs/system/en.xml

@@ -1746,7 +1746,7 @@
     <string name="_adm_stg_cpt_option_sys_storage_s3_amz_iam_role"><![CDATA[Authenticate using Amazon  IAM Role (S3v4alt storage only)]]></string>
     <string name="_adm_stg_cpt_option_sys_storage_s3_acl_enable"><![CDATA[Enable ACLs (S3v4alt storage only)]]></string>
     <string name="_adm_stg_cpt_option_sys_storage_s3_force_auth_urls"><![CDATA[Force authenticated URLs for specific storages]]></string>
-
+    <string name="_adm_stg_cpt_option_sys_storage_ghost_lifetime"><![CDATA[Lifetime for unused ghosts, which they will be automatically deleted after (in minutes, use 0 to disable).]]></string>
     <string name="_adm_stg_cpt_option_sys_template_cache_image_enable"><![CDATA[Enable cache for images]]></string>
     <string name="_adm_stg_cpt_option_sys_template_cache_image_max_size"><![CDATA[Cached image max size (in Kb)]]></string>
     <string name="_adm_stg_cpt_option_sys_template_cache_image_max_size_err"><![CDATA[Use non-empty value less or equal to 10kb. Big values make no sense.]]></string>

+ 1 - 1
modules/boonex/russian/data/langs/system/ru.xml

@@ -1721,7 +1721,7 @@
     <string name="_adm_stg_cpt_option_sys_storage_s3_amz_iam_role"><![CDATA[Аутентификация с использованием Amazon IAM Role (только для S3v4alt)]]></string>
     <string name="_adm_stg_cpt_option_sys_storage_s3_acl_enable"><![CDATA[Включить ACLs (только для S3v4alt)]]></string>
     <string name="_adm_stg_cpt_option_sys_storage_s3_force_auth_urls"><![CDATA[Применять аутентифицированные URL-адреса для определенных хранилищ]]></string>
-
+    <string name="_adm_stg_cpt_option_sys_storage_ghost_lifetime"><![CDATA[Срок действия неиспользованных «призраков», по истечении которого они будут автоматически удалены  (в минутах, используйте 0 для отключения).]]></string>
     <string name="_adm_stg_cpt_option_sys_template_cache_image_enable"><![CDATA[Задействовать кэш для изображений]]></string>
     <string name="_adm_stg_cpt_option_sys_template_cache_image_max_size"><![CDATA[Максимальный размер кэшируемого изображения (в Кб)]]></string>
     <string name="_adm_stg_cpt_option_sys_template_cache_image_max_size_err"><![CDATA[Используйте не пустое значение не больше 10Кб. Большие заначения не имеют смысла.]]></string>