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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions grails-app/services/au/org/ala/collectory/SitemapService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
*/
package au.org.ala.collectory

import grails.gorm.transactions.Transactional
import grails.util.Holders
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.


def grailsApplication


Expand All @@ -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


initWriter()
buildSitemap()
closeWriter()
Expand All @@ -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>

fw.write("<lastmod>" + simpleDateFormat.format(new Date()) + "</lastmod></sitemap>")
}

Expand Down Expand Up @@ -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).

}

Institution.findAll().each {Institution it ->
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/in" + it.id)
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/" + it.uid)
}

DataProvider.findAll().each {DataProvider it ->
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/dp" + it.id)
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/" + it.uid)
}

DataResource.findAllByIsPrivate(false).each {DataResource it ->
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/dr" + it.id)
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/" + it.uid)
}
}
}