Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues in SitemapService #243

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

matsbov
Copy link
Contributor

@matsbov matsbov commented Sep 13, 2024

This PR fixes the following issues in SitemapService (#244):

  • The sitemap index file creates <url> tags that should be <loc>
  • id is used instead of uid when creating url:s which is incorrect
  • Scheduled (daily) generation isn't enabled unless the sitemap is generated manually from the admin UI first
  • Scheduled runs crash with an exception (manual runs work fine)
  • Repeated runs of sitemap generation will create sitemap files that don't match the sitemap index file

See code comments below for details.

import org.springframework.scheduling.annotation.Scheduled

import java.text.SimpleDateFormat

class SitemapService {

static lazyInit = !Holders.getConfig().getProperty("sitemap.enabled", Boolean.TYPE, false)
Copy link
Contributor Author

@matsbov matsbov Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If sitemap is enabled the SitemapService should be loaded on application startup thereby enabling scheduled runs. Otherwise scheduled runs are enabled only after a manual run from admin UI.

@@ -40,6 +44,8 @@ class SitemapService {
// run daily, initial delay 1hr
@Scheduled(fixedDelay = 86400000L, initialDelay = 3600000L)
def build() throws Exception {
fileCount = 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fileCount needs to be reset each time the method is run to prevent mismatch between entries in the sitemap index file and the actual sitemap files created

@@ -63,7 +69,7 @@ class SitemapService {
new File(grailsApplication.config.sitemap.dir + "/sitemap" + i + ".xml.tmp").renameTo(newFile)

// add an entry for this new file
fw.write("<sitemap><url>" + grailsApplication.config.grails.serverURL + "/sitemap" + i + ".xml" + "</url>")
fw.write("<sitemap><loc>" + grailsApplication.config.grails.serverURL + "/sitemap" + i + ".xml" + "</loc>")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct syntax is <loc> and not <url>

@@ -106,22 +112,23 @@ class SitemapService {
countUrls++
}

@Transactional
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scheduled runs crash with org.springframework.dao.DataAccessResourceFailureException: Could not obtain current Hibernate Session; nested exception is org.hibernate.HibernateException: No Session found for current thread. Manual runs from admin UI are ok. Adding @Transactional fixes the crash. But there might be other/better solutions?

def buildSitemap() throws Exception {

Collection.findAll().each {Collection it ->
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/co" + it.id)
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/" + it.uid)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be uid and not id. SELECT id, uid FROM collection order by 1; will show that they are not the same (at least not in a database where items have been deleted).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant