【问题标题】:Delete past versions of Web Contents that have more than N versions删除具有 N 个以上版本的 Web 内容的过去版本
【发布时间】:2019-07-20 22:24:26
【问题描述】:

我有一个 Liferay 6.2 服务器,其站点包含许多 Web 内容。

问题

多年来,服务器有时变得非常缓慢,甚至最近触发了 OutOfMemoryException。 MySQL 慢查询日志显示,这是由于某些 Web 内容具有数千个版本。

目标

由于我们不太关心版本,我们正在考虑淘汰所有旧版本,但作为避免进一步出现 OutOfMemory 异常的首要补救措施,我们希望这样做:

删除所有过去版本超过 100 个的网络内容

怎么办?

【问题讨论】:

    标签: liferay liferay-6


    【解决方案1】:

    转到脚本控制台(在服务器管理中),将其设置为“Groovy”,粘贴下面的脚本,插入相关站点的组 ID(又名站点 ID,使用 Liferay Web 界面找到),然后执行:

    import com.liferay.portlet.journal.service.JournalArticleServiceUtil
    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil
    import com.liferay.portlet.journal.model.JournalArticle
    
    /**
     * Delete past versions of articles with too many versions.
     */
    
    /**
     * Configuration.
     */
    MAX_NUMBER_OF_VERSIONS = 100
    DELETIONS_LIMIT=-1 // Only delete this number of versions, in total. Useful for tests. -1 = no limit
    CHUNK=10 // In order to avoid memory load, everything is performed chunk by chunk, with this size.
    sites = [20182, 21987]
    
    /**
     * Main.
     */
    sites.each {
      println "\n# Site " + it
      deletePastVersionsOfArticlesWithTooManyVersions(it)
    }
    
    /**
     * Delete past versions of articles with too many versions in the given site.
     */
    def deletePastVersionsOfArticlesWithTooManyVersions(groupId) {
      List<JournalArticle> articlesWithTooManyVersions = findArticlesWithTooManyVersions(groupId);
      println "\nDeleting all past versions of " + articlesWithTooManyVersions.size() + " articles with too many versions"
      articlesWithTooManyVersions.each {
        deletePastVersions(groupId, it);
      }
    }
    
    /**
      * Returns the list of articles with to many versions in the given site, as a List<JournalArticle>
      */
    def findArticlesWithTooManyVersions(groupId) {
      List<JournalArticle> result = new ArrayList<JournalArticle>()
    
      int count = JournalArticleLocalServiceUtil.getArticlesCount(groupId)
      for (int start=0; start<count; start+=CHUNK) {
        List<JournalArticle> versions = JournalArticleLocalServiceUtil.getArticles(groupId, start, start+CHUNK, null)
        println "Page start:" + start + " size:" + versions.size()
    
        // Process this page
        versions.each { version ->
          // Ignore documents that are in the recycle bin.
          if (version.isInTrash()) {
            return // Equivalent of continue for closures.
          }
          // We only need to process each Web Content once, but we get one JournalArticle per version.
          // The trick here is to only process the JournalArticle if it is the latest version, as each Web Content has exactly one latest version.
          if(JournalArticleLocalServiceUtil.isLatestVersion(groupId,version.getArticleId(), version.getVersion())){
            println "Checking article " + version.getArticleId()
            numberOfVersions = JournalArticleServiceUtil.getArticlesCountByArticleId(groupId, version.getArticleId());
            if(numberOfVersions > MAX_NUMBER_OF_VERSIONS) {
              println "JournalArticle " + version.getArticleId() + " has more than " + MAX_NUMBER_OF_VERSIONS + " versions."
              result.add(version);
            }
          }
        }
      }
      return result
    }
    
    /**
      * Delete all past versions of the given article.
      */
    def deletePastVersions(groupId, article) {
      int count = JournalArticleLocalServiceUtil.getArticlesCount(groupId, article.getArticleId())
      int numberOfNecessaryIterations = 1 + count/(CHUNK-1) // Plus 1 for the remainder. Minus 1 to account for the worse case where the latest version appears in all chunks.
      println "Deleting " + (count - 1) + " versions of article " + article.getArticleId() + " in " + numberOfNecessaryIterations + " iterations."
      for (int iteration=0; iteration<=numberOfNecessaryIterations; iteration++) {
      // Paging is refreshed after deletion so we must always get the first elements.
      List<JournalArticle> versions = JournalArticleServiceUtil.getArticlesByArticleId(groupId, article.getArticleId(), 0, CHUNK, null)
    
        println "Iteration " + iteration + " Deleting " + versions.size() + " past versions of article " + article.getArticleId() // Actually one less if this chunk contains the latest version.
        for(version in versions) {
           if(JournalArticleLocalServiceUtil.isLatestVersion(groupId, version.getArticleId(), version.getVersion())) {
             println "Skipping the latest version." // The latest version must NOT be deleted.
             continue; // Proceed to the next version of this chunk.
          }
          // Delete this version
          println "Deleting article " + version.getArticleId() + " version " + version.getVersion() + " " + new Date()
          if (DELETIONS_LIMIT!=0) {
            DELETIONS_LIMIT--
            JournalArticleLocalServiceUtil.deleteArticle(groupId, version.getArticleId(), version.getVersion(), null,null)
            println "Deletion performed"
          }
          if (DELETIONS_LIMIT==0){
            return // Exit the method
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-07
      • 2016-01-07
      • 2018-12-26
      • 2020-04-13
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多