Skip to content

Commit 808b247

Browse files
authored
Merge pull request #177 from AtlasOfLivingAustralia/develop
Merging to master for release 2.0.3
2 parents e5f93e0 + b8b682a commit 808b247

File tree

3 files changed

+74
-48
lines changed

3 files changed

+74
-48
lines changed

build.gradle

+4-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ plugins {
3030
}
3131

3232

33-
version "2.0.2"
33+
version "2.1.0-SNAPSHOT"
3434
group "au.org.ala"
3535

3636
apply plugin:"eclipse"
@@ -43,8 +43,8 @@ apply plugin:"com.bertramlabs.asset-pipeline"
4343
//apply from: 'https://raw.githubusercontent.com/AtlasOfLivingAustralia/travis-build-configuration/master/travis_grails_publish.gradle'
4444
apply from: "${project.projectDir}/gradle/publish.gradle"
4545

46-
sourceCompatibility = 1.8
47-
targetCompatibility = 1.8
46+
sourceCompatibility = 1.11
47+
targetCompatibility = 1.11
4848

4949
repositories {
5050
mavenLocal()
@@ -104,10 +104,9 @@ dependencies {
104104
// Grails plugin dependencies
105105
runtime "org.grails.plugins:ala-bootstrap3:4.1.0", noCache
106106
compile "org.grails.plugins:ala-ws-security-plugin:4.1.1", noCache
107-
compile "org.grails.plugins:ala-ws-plugin:3.1.1", noCache
107+
compile "org.grails.plugins:ala-ws-plugin:3.1.2", noCache
108108
compile "org.grails.plugins:ala-auth:5.1.1", noCache
109109
compile "org.grails.plugins:ala-admin-plugin:2.3.0"
110-
//compile 'au.org.ala:userdetails-service-client:1.4.0' // TODO remove this line once updated version is included in ala-auth
111110

112111
compile 'org.grails.plugins:external-config:2.0.0'
113112
compile 'org.grails.plugins:mail:3.0.0'

grails-app/jobs/ala/postie/UpdateEmailsJob.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ala.postie
33
class UpdateEmailsJob {
44

55
static triggers = {
6-
cron name: 'twiceDaily', startDelay: 10000, cronExpression: '0 32 8,16 * * ?'
6+
cron name: 'dailyEmails', startDelay: 10000, cronExpression: '0 30 4 * * ?' //4.30am
77
}
88

99
def userService

grails-app/services/au/org/ala/alerts/UserService.groovy

+69-42
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package au.org.ala.alerts
1515

1616
import au.org.ala.userdetails.UserDetailsFromIdListRequest
17+
import au.org.ala.userdetails.UserDetailsFromIdListResponse
1718
import au.org.ala.web.UserDetails
1819
import grails.converters.JSON
1920
import grails.plugin.cache.Cacheable
@@ -71,64 +72,90 @@ class UserService {
7172

7273
/**
7374
* Sync User table with UserDetails app via webservice
74-
* TODO batch requests to userDetails in 100 lots (see @AuthService.getUserDetailsById )
7575
*
76-
* @return
76+
* @return total number of updates
7777
*/
78+
// @Transactional // transactions handled manually
7879
int updateUserEmails() {
80+
final int pageSize = grailsApplication.config.getProperty('alerts.user-sync.batch-size', Integer, 1000)
7981
def toUpdate = []
80-
log.warn "Checking all ${User.count()} users in Alerts user table."
82+
def total = User.count()
83+
log.warn "Checking all ${total} users in Alerts user table."
8184
def count = 0
8285

83-
User.findAll().each { user ->
84-
count++
85-
UserDetails userDetails = authService.getUserForUserId(user.userId, false) // under @Cacheable
86-
Boolean userHasChanged = false
87-
88-
if (userDetails) {
89-
// update email
90-
if (userDetails != null && user.email != userDetails.userName) {
91-
user.email = userDetails.userName
92-
log.debug "Updating email address for user ${user.userId}: ${userDetails.userName}"
93-
userHasChanged = true
94-
}
86+
def page = 0
87+
88+
boolean done = false
9589

96-
// update locked property
97-
if (userDetails?.hasProperty("locked") && userDetails.locked != null) {
98-
log.debug "Checking locked user: ${user.userId} -> ${userDetails.locked} vs ${user.locked}"
90+
while (!done) {
91+
User.withTransaction {
92+
List<User> users = User.findAll([sort: 'id', max: (page+1) * pageSize, offset: page * pageSize])
93+
done = users.size() < pageSize
94+
List<User> updates = []
9995

100-
if ((user.locked == null && userDetails.locked == true) ||
101-
(user.locked != null && user.locked != userDetails.locked)) {
102-
user.locked = userDetails.locked
103-
log.debug "Updating locked status for user ${user.userId}: ${userDetails.locked}"
104-
userHasChanged = true
96+
def ids = users*.userId
97+
UserDetailsFromIdListResponse results
98+
if (ids) {
99+
try {
100+
results = authService.getUserDetailsById(ids, false)
101+
} catch (Exception e) {
102+
log.warn("couldn't get user details from web service", e)
105103
}
106104
}
107-
} else {
108-
// we can't find a user in userdetails using userId - lock their account in alerts DB
109-
if ((user.locked == null || user.locked != true) && Environment.current == Environment.PRODUCTION) {
110-
user.locked = true
111-
log.warn "Updating locked status for missing user ${user.userId}: true"
112-
userHasChanged = true
113-
}
114-
}
115105

106+
if (results && results.success) {
107+
users.each {user ->
108+
UserDetails userDetails = results.users[user.userId]
109+
if (userDetails) {
110+
// update email
111+
boolean update = false
112+
if (user.email != userDetails.userName) {
113+
user.email = userDetails.userName
114+
log.debug "Updating email address for user ${user.userId}: ${userDetails.userName}"
115+
update = true
116+
}
117+
118+
// update locked property
119+
if (userDetails.locked != null) {
120+
log.debug "Checking locked user: ${user.userId} -> ${userDetails.locked} vs ${user.locked}"
121+
122+
if ((user.locked == null && userDetails.locked == true) ||
123+
(user.locked != null && user.locked != userDetails.locked)) {
124+
user.locked = userDetails.locked
125+
log.debug "Updating locked status for user ${user.userId}: ${userDetails.locked}"
126+
update = true
127+
}
128+
}
129+
if (update) {
130+
updates << user
131+
}
132+
} else {
133+
// we can't find a user in userdetails using userId - lock their account in alerts DB
134+
if ((user.locked == null || user.locked != true) && Environment.current == Environment.PRODUCTION) {
135+
user.locked = true
136+
log.warn "Updating locked status for missing user ${user.userId}: true"
137+
updates << user
138+
}
139+
}
140+
}
141+
} else if (results && !results.success) {
142+
log.warn("Unsuccessful response from userdetails: {}", results)
143+
}
116144

117-
if (userHasChanged) {
118-
toUpdate << user
145+
if (updates) {
146+
updates.each {
147+
log.warn "Modifying user: ${it as JSON}"
148+
}
149+
count += updates.size()
150+
updates*.save()
151+
}
119152
}
120153

121-
if (count % 100 == 0) {
122-
log.warn "Checked ${count} users with ${toUpdate.size()} changes, so far"
123-
}
124-
}
154+
page++
155+
log.warn "Checked ${Math.min(total, page * pageSize)} users with ${count} changes, so far"
125156

126-
toUpdate.each {
127-
log.warn "Modifying user: ${it as JSON}"
128-
it.save(flush: true)
129157
}
130-
131-
toUpdate.size()
158+
return count
132159
}
133160

134161
User getUser(userDetailsParam = null) {

0 commit comments

Comments
 (0)