remove study work queue debugging

pull/5938/head
Thibault Duplessis 2020-01-18 13:12:44 -06:00
parent 62093ca4f8
commit ebf7fb9bc3
3 changed files with 61 additions and 69 deletions

View File

@ -47,7 +47,7 @@ object ServerEval {
def apply(analysis: Analysis, complete: Boolean): Funit = analysis.studyId.map(Study.Id.apply) ?? {
studyId =>
sequencer.sequenceStudyWithChapter("merge")(studyId, Chapter.Id(analysis.id)) {
sequencer.sequenceStudyWithChapter(studyId, Chapter.Id(analysis.id)) {
case Study.WithChapter(_, chapter) =>
(complete ?? chapterRepo.completeServerEval(chapter)) >> {
lila.common.Future

View File

@ -78,7 +78,7 @@ final class StudyApi(
}
private def fixNoChapter(study: Study): Fu[Option[Study.WithChapter]] =
sequenceStudy("fixNoChapter")(study.id) { study =>
sequenceStudy(study.id) { study =>
chapterRepo existsByStudy study.id flatMap {
case true => funit
case _ =>
@ -169,7 +169,7 @@ final class StudyApi(
}
def setPath(studyId: Study.Id, position: Position.Ref)(who: Who): Funit =
sequenceStudy("setPath")(studyId) { study =>
sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
chapterRepo.byId(position.chapterId).map {
_ filter { c =>
@ -193,7 +193,7 @@ final class StudyApi(
opts: MoveOpts,
relay: Option[Chapter.Relay] = None
)(who: Who) =
sequenceStudyWithChapter("addNode")(studyId, position.chapterId) {
sequenceStudyWithChapter(studyId, position.chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(who.u, study) {
doAddNode(study, Position(chapter, position.path), node, opts, relay)(who).void
@ -255,7 +255,7 @@ final class StudyApi(
}
def deleteNodeAt(studyId: Study.Id, position: Position.Ref)(who: Who) =
sequenceStudyWithChapter("deleteNodeAt")(studyId, position.chapterId) {
sequenceStudyWithChapter(studyId, position.chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(who.u, study) {
chapter.updateRoot { root =>
@ -272,7 +272,7 @@ final class StudyApi(
}
def clearAnnotations(studyId: Study.Id, chapterId: Chapter.Id)(who: Who) =
sequenceStudyWithChapter("clearAnnotations")(studyId, chapterId) {
sequenceStudyWithChapter(studyId, chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(who.u, study) {
chapterRepo.update(chapter.updateRoot { root =>
@ -282,7 +282,7 @@ final class StudyApi(
}
def promote(studyId: Study.Id, position: Position.Ref, toMainline: Boolean)(who: Who) =
sequenceStudyWithChapter("promote")(studyId, position.chapterId) {
sequenceStudyWithChapter(studyId, position.chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(who.u, study) {
chapter.updateRoot { root =>
@ -312,7 +312,7 @@ final class StudyApi(
}
def forceVariation(studyId: Study.Id, position: Position.Ref, force: Boolean)(who: Who): Funit =
sequenceStudyWithChapter("forceVariation")(studyId, position.chapterId) { sc =>
sequenceStudyWithChapter(studyId, position.chapterId) { sc =>
Contribute(who.u, sc.study) {
doForceVariation(sc, position.path, force, who)
}
@ -329,7 +329,7 @@ final class StudyApi(
}
def setRole(studyId: Study.Id, userId: User.ID, roleStr: String)(who: Who) =
sequenceStudy("setRole")(studyId) { study =>
sequenceStudy(studyId) { study =>
(study isOwner who.u) ?? {
val role = StudyMember.Role.byId.getOrElse(roleStr, StudyMember.Role.Read)
study.members.get(userId) ifTrue study.isPublic foreach { member =>
@ -349,14 +349,14 @@ final class StudyApi(
username: String,
isPresent: User.ID => Fu[Boolean],
onError: String => Unit
) = sequenceStudy("invite")(studyId) { study =>
) = sequenceStudy(studyId) { study =>
inviter(byUserId, study, username, isPresent).addEffects(
err => onError(err.getMessage),
_ => onMembersChange(study)
)
}
def kick(studyId: Study.Id, userId: User.ID)(who: Who) = sequenceStudy("kick")(studyId) { study =>
def kick(studyId: Study.Id, userId: User.ID)(who: Who) = sequenceStudy(studyId) { study =>
(study.isMember(userId) && (study.isOwner(who.u) ^ (who.u == userId))) ?? {
if (study.isPublic && study.canContribute(userId))
Bus.publish(lila.hub.actorApi.study.StudyMemberLostWriteAccess(userId, studyId.value), "study")
@ -378,7 +378,7 @@ final class StudyApi(
}
def setShapes(studyId: Study.Id, position: Position.Ref, shapes: Shapes)(who: Who) =
sequenceStudy("setShapes")(studyId) { study =>
sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
chapterRepo.byIdAndStudy(position.chapterId, study.id) flatMap {
_ ?? { chapter =>
@ -397,7 +397,7 @@ final class StudyApi(
}
def setClock(studyId: Study.Id, position: Position.Ref, clock: Option[Centis])(who: Who): Funit =
sequenceStudyWithChapter("setClock")(studyId, position.chapterId) {
sequenceStudyWithChapter(studyId, position.chapterId) {
doSetClock(_, position, clock)(who)
}
@ -415,7 +415,7 @@ final class StudyApi(
}
def setTag(studyId: Study.Id, setTag: actorApi.SetTag)(who: Who) =
sequenceStudyWithChapter("setTag")(studyId, setTag.chapterId) {
sequenceStudyWithChapter(studyId, setTag.chapterId) {
case Study.WithChapter(study, chapter) =>
logger.info(s"setTag $studyId $setTag")
Contribute(who.u, study) {
@ -424,7 +424,7 @@ final class StudyApi(
}
def setTags(studyId: Study.Id, chapterId: Chapter.Id, tags: Tags)(who: Who) =
sequenceStudyWithChapter("setTags")(studyId, chapterId) {
sequenceStudyWithChapter(studyId, chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(who.u, study) {
doSetTags(study, chapter, tags, who)
@ -444,7 +444,7 @@ final class StudyApi(
}
def setComment(studyId: Study.Id, position: Position.Ref, text: Comment.Text)(who: Who) =
sequenceStudyWithChapter("setComment")(studyId, position.chapterId) {
sequenceStudyWithChapter(studyId, position.chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(who.u, study) {
lightUserApi.async(who.u) flatMap {
@ -479,7 +479,7 @@ final class StudyApi(
}
def deleteComment(studyId: Study.Id, position: Position.Ref, id: Comment.Id)(who: Who) =
sequenceStudyWithChapter("deleteComment")(studyId, position.chapterId) {
sequenceStudyWithChapter(studyId, position.chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(who.u, study) {
chapter.deleteComment(id, position.path) match {
@ -495,7 +495,7 @@ final class StudyApi(
}
def toggleGlyph(studyId: Study.Id, position: Position.Ref, glyph: Glyph)(who: Who) =
sequenceStudyWithChapter("toggleGlyph")(studyId, position.chapterId) {
sequenceStudyWithChapter(studyId, position.chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(who.u, study) {
chapter.toggleGlyph(glyph, position.path) match {
@ -515,7 +515,7 @@ final class StudyApi(
}
def setGamebook(studyId: Study.Id, position: Position.Ref, gamebook: Gamebook)(who: Who) =
sequenceStudyWithChapter("setGamebook")(studyId, position.chapterId) {
sequenceStudyWithChapter(studyId, position.chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(who.u, study) {
chapter.setGamebook(gamebook, position.path) match {
@ -533,7 +533,7 @@ final class StudyApi(
}
def explorerGame(studyId: Study.Id, data: actorApi.ExplorerGame)(who: Who) =
sequenceStudyWithChapter("explorerGame")(studyId, data.position.chapterId) {
sequenceStudyWithChapter(studyId, data.position.chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(who.u, study) {
if (data.insert)
@ -560,7 +560,7 @@ final class StudyApi(
}
def addChapter(studyId: Study.Id, data: ChapterMaker.Data, sticky: Boolean)(who: Who) =
sequenceStudy("addChapter")(studyId) { study =>
sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
chapterRepo.countByStudyId(study.id) flatMap { count =>
if (count >= Study.maxChapters) funit
@ -596,9 +596,8 @@ final class StudyApi(
studyRepo.updateNow(study) >>-
indexStudy(study)
def setChapter(studyId: Study.Id, chapterId: Chapter.Id)(who: Who) = sequenceStudy("setChapter")(studyId) {
study =>
study.canContribute(who.u) ?? doSetChapter(study, chapterId, who)
def setChapter(studyId: Study.Id, chapterId: Chapter.Id)(who: Who) = sequenceStudy(studyId) { study =>
study.canContribute(who.u) ?? doSetChapter(study, chapterId, who)
}
private def doSetChapter(study: Study, chapterId: Chapter.Id, who: Who) =
@ -613,7 +612,7 @@ final class StudyApi(
}
def editChapter(studyId: Study.Id, data: ChapterMaker.EditData)(who: Who) =
sequenceStudy("editChapter")(studyId) { study =>
sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
chapterRepo.byIdAndStudy(data.id, studyId) flatMap {
_ ?? { chapter =>
@ -658,7 +657,7 @@ final class StudyApi(
}
def descChapter(studyId: Study.Id, data: ChapterMaker.DescData)(who: Who) =
sequenceStudy("descChapter")(studyId) { study =>
sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
chapterRepo.byIdAndStudy(data.id, studyId) flatMap {
_ ?? { chapter =>
@ -677,7 +676,7 @@ final class StudyApi(
}
def deleteChapter(studyId: Study.Id, chapterId: Chapter.Id)(who: Who) =
sequenceStudy("deleteChapter")(studyId) { study =>
sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
chapterRepo.byIdAndStudy(chapterId, studyId) flatMap {
_ ?? { chapter =>
@ -701,13 +700,13 @@ final class StudyApi(
}
def sortChapters(studyId: Study.Id, chapterIds: List[Chapter.Id])(who: Who) =
sequenceStudy("sortChapters")(studyId) { study =>
sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
chapterRepo.sort(study, chapterIds) >>- reloadChapters(study)
}
}
def descStudy(studyId: Study.Id, desc: String)(who: Who) = sequenceStudy("descStudy")(studyId) { study =>
def descStudy(studyId: Study.Id, desc: String)(who: Who) = sequenceStudy(studyId) { study =>
Contribute(who.u, study) {
val newStudy = study.copy(description = desc.nonEmpty option desc)
(study != newStudy) ?? {
@ -718,38 +717,37 @@ final class StudyApi(
}
}
def editStudy(studyId: Study.Id, data: Study.Data)(who: Who) = sequenceStudy("editStudy")(studyId) {
study =>
data.settings.ifTrue(study isOwner who.u) ?? { settings =>
val newStudy = study.copy(
name = Study toName data.name,
settings = settings,
visibility = data.vis,
description = settings.description option {
study.description.filter(_.nonEmpty) | "-"
}
def editStudy(studyId: Study.Id, data: Study.Data)(who: Who) = sequenceStudy(studyId) { study =>
data.settings.ifTrue(study isOwner who.u) ?? { settings =>
val newStudy = study.copy(
name = Study toName data.name,
settings = settings,
visibility = data.vis,
description = settings.description option {
study.description.filter(_.nonEmpty) | "-"
}
)
if (!study.isPublic && newStudy.isPublic) {
Bus.publish(
lila.hub.actorApi.study.StudyBecamePublic(studyId.value, study.members.contributorIds),
"study"
)
} else if (study.isPublic && !newStudy.isPublic) {
Bus.publish(
lila.hub.actorApi.study.StudyBecamePrivate(studyId.value, study.members.contributorIds),
"study"
)
if (!study.isPublic && newStudy.isPublic) {
Bus.publish(
lila.hub.actorApi.study.StudyBecamePublic(studyId.value, study.members.contributorIds),
"study"
)
} else if (study.isPublic && !newStudy.isPublic) {
Bus.publish(
lila.hub.actorApi.study.StudyBecamePrivate(studyId.value, study.members.contributorIds),
"study"
)
}
(newStudy != study) ?? {
studyRepo.updateSomeFields(newStudy) >>-
sendTo(study.id)(_.reloadAll) >>-
indexStudy(study) >>-
lightStudyCache.put(studyId, fuccess(newStudy.light.some))
}
}
(newStudy != study) ?? {
studyRepo.updateSomeFields(newStudy) >>-
sendTo(study.id)(_.reloadAll) >>-
indexStudy(study) >>-
lightStudyCache.put(studyId, fuccess(newStudy.light.some))
}
}
}
def delete(study: Study) = sequenceStudy("delete")(study.id) { study =>
def delete(study: Study) = sequenceStudy(study.id) { study =>
studyRepo.delete(study) >>
chapterRepo.deleteByStudy(study) >>-
Bus.publish(lila.hub.actorApi.study.RemoveStudy(study.id.value, study.members.contributorIds), "study") >>-
@ -785,14 +783,14 @@ final class StudyApi(
}
def analysisRequest(studyId: Study.Id, chapterId: Chapter.Id, userId: User.ID): Funit =
sequenceStudyWithChapter("analysisRequest")(studyId, chapterId) {
sequenceStudyWithChapter(studyId, chapterId) {
case Study.WithChapter(study, chapter) =>
Contribute(userId, study) {
serverEvalRequester(study, chapter, userId)
}
}
def deleteAllChapters(studyId: Study.Id, by: User) = sequenceStudy("deleteAllChapters")(studyId) { study =>
def deleteAllChapters(studyId: Study.Id, by: User) = sequenceStudy(studyId) { study =>
Contribute(by.id, study) {
chapterRepo deleteByStudy study
}

View File

@ -1,7 +1,6 @@
package lila.study
import scala.concurrent.duration._
import java.util.concurrent.TimeoutException
import lila.common.WorkQueues
@ -17,26 +16,21 @@ final private class StudySequencer(
private val workQueue =
new WorkQueues(buffer = 256, expiration = 5 minutes, timeout = 10 seconds, name = "study")
def sequenceStudy(name: String)(studyId: Study.Id)(f: Study => Funit): Funit =
def sequenceStudy(studyId: Study.Id)(f: Study => Funit): Funit =
workQueue(studyId.value) {
studyRepo.byId(studyId) flatMap {
_ ?? { f(_) }
}
} addFailureEffect logName(name)
}
def sequenceStudyWithChapter(name: String)(studyId: Study.Id, chapterId: Chapter.Id)(
def sequenceStudyWithChapter(studyId: Study.Id, chapterId: Chapter.Id)(
f: Study.WithChapter => Funit
): Funit =
sequenceStudy(name)(studyId) { study =>
sequenceStudy(studyId) { study =>
chapterRepo.byId(chapterId) flatMap {
_ ?? { chapter =>
f(Study.WithChapter(study, chapter))
}
}
}
private def logName(name: String): Throwable => Unit = {
case _: TimeoutException => logger.warn(s"$name timed out")
case _ =>
}
}