forked from finos/waltz
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add job to scheduledJobService to run and action any pending entries
#CTCTOWALTZ-2680 finos#6860
- Loading branch information
1 parent
53f0991
commit 0c261d6
Showing
14 changed files
with
384 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
waltz-data/src/main/java/org/finos/waltz/data/survey/SurveyInstanceActionQueueDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package org.finos.waltz.data.survey; | ||
|
||
import org.finos.waltz.common.DateTimeUtilities; | ||
import org.finos.waltz.model.survey.ImmutableSurveyInstanceActionQueueItem; | ||
import org.finos.waltz.model.survey.SurveyInstanceAction; | ||
import org.finos.waltz.model.survey.SurveyInstanceActionQueueItem; | ||
import org.finos.waltz.model.survey.SurveyInstanceActionStatus; | ||
import org.finos.waltz.model.survey.SurveyInstanceStatus; | ||
import org.finos.waltz.schema.tables.records.SurveyInstanceActionQueueRecord; | ||
import org.jooq.Condition; | ||
import org.jooq.DSLContext; | ||
import org.jooq.Record; | ||
import org.jooq.Record1; | ||
import org.jooq.RecordMapper; | ||
import org.jooq.SelectConditionStep; | ||
import org.jooq.impl.DSL; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.sql.Timestamp; | ||
import java.util.List; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Optional.ofNullable; | ||
import static org.finos.waltz.schema.Tables.SURVEY_INSTANCE_ACTION_QUEUE; | ||
|
||
@Repository | ||
public class SurveyInstanceActionQueueDao { | ||
|
||
private final DSLContext dsl; | ||
|
||
public static final RecordMapper<Record, SurveyInstanceActionQueueItem> TO_DOMAIN_MAPPER = r -> { | ||
SurveyInstanceActionQueueRecord record = r.into(SURVEY_INSTANCE_ACTION_QUEUE); | ||
return ImmutableSurveyInstanceActionQueueItem.builder() | ||
.id(record.getId()) | ||
.action(SurveyInstanceAction.valueOf(record.getAction())) | ||
.surveyInstanceId(record.getSurveyInstanceId()) | ||
.actionParams(record.getActionParams()) | ||
.initialState(SurveyInstanceStatus.valueOf(record.getInitialState())) | ||
.submittedAt(DateTimeUtilities.toLocalDateTime(record.getSubmittedAt())) | ||
.submittedBy(record.getSubmittedBy()) | ||
.actionedAt(ofNullable(record.getActionedAt()).map(Timestamp::toLocalDateTime).orElse(null)) | ||
.status(SurveyInstanceActionStatus.valueOf(record.getStatus())) | ||
.message(record.getMessage()) | ||
.provenance(record.getProvenance()) | ||
.build(); | ||
}; | ||
|
||
|
||
@Autowired | ||
SurveyInstanceActionQueueDao(DSLContext dsl) { | ||
this.dsl = dsl; | ||
} | ||
|
||
|
||
public List<SurveyInstanceActionQueueItem> findPendingActions() { | ||
Condition isPending = SURVEY_INSTANCE_ACTION_QUEUE.STATUS.eq(SurveyInstanceActionStatus.PENDING.name()); | ||
return mkSelectByCondition(dsl, isPending) | ||
.orderBy(SURVEY_INSTANCE_ACTION_QUEUE.SUBMITTED_AT) | ||
.fetch(TO_DOMAIN_MAPPER); | ||
} | ||
|
||
|
||
public SurveyInstanceActionQueueItem getById(Long id) { | ||
Condition idCondition = SURVEY_INSTANCE_ACTION_QUEUE.ID.eq(id); | ||
return mkSelectByCondition(dsl, idCondition) | ||
.fetchOne(TO_DOMAIN_MAPPER); | ||
} | ||
|
||
|
||
private SelectConditionStep<Record> mkSelectByCondition(DSLContext dslContext, Condition condition) { | ||
return dslContext | ||
.select(SURVEY_INSTANCE_ACTION_QUEUE.fields()) | ||
.from(SURVEY_INSTANCE_ACTION_QUEUE) | ||
.where(condition); | ||
} | ||
|
||
|
||
public void updateActionStatus(DSLContext tx, Long actionId, SurveyInstanceActionStatus instanceActionStatus, String msg) { | ||
int updated = tx | ||
.update(SURVEY_INSTANCE_ACTION_QUEUE) | ||
.set(SURVEY_INSTANCE_ACTION_QUEUE.ACTIONED_AT, DateTimeUtilities.nowUtcTimestamp()) | ||
.set(SURVEY_INSTANCE_ACTION_QUEUE.STATUS, instanceActionStatus.name()) | ||
.set(SURVEY_INSTANCE_ACTION_QUEUE.MESSAGE, msg) | ||
.where(SURVEY_INSTANCE_ACTION_QUEUE.ID.eq(actionId) | ||
.and(SURVEY_INSTANCE_ACTION_QUEUE.STATUS.eq(SurveyInstanceActionStatus.IN_PROGRESS.name()))) | ||
.execute(); | ||
|
||
if (updated != 1) { | ||
String messageString = "Unable to update action queue item with id: %d as %d records were updated. " + | ||
"Reverting all action changes, this action will be attempted again in future as will be rolled back to 'PENDING'"; | ||
|
||
throw new IllegalStateException(format( | ||
messageString, | ||
actionId, | ||
updated)); | ||
} | ||
} | ||
|
||
|
||
public void markActionInProgress(DSLContext tx, Long actionId) { | ||
|
||
SelectConditionStep<Record1<Long>> inProgressAction = DSL | ||
.select(SURVEY_INSTANCE_ACTION_QUEUE.ID) | ||
.from(SURVEY_INSTANCE_ACTION_QUEUE) | ||
.where(SURVEY_INSTANCE_ACTION_QUEUE.STATUS.eq(SurveyInstanceActionStatus.IN_PROGRESS.name())); | ||
|
||
int updated = tx | ||
.update(SURVEY_INSTANCE_ACTION_QUEUE) | ||
.set(SURVEY_INSTANCE_ACTION_QUEUE.STATUS, SurveyInstanceActionStatus.IN_PROGRESS.name()) | ||
.where(SURVEY_INSTANCE_ACTION_QUEUE.ID.eq(actionId) | ||
.and(SURVEY_INSTANCE_ACTION_QUEUE.STATUS.eq(SurveyInstanceActionStatus.PENDING.name())) | ||
.and(DSL.notExists(inProgressAction))) | ||
.execute(); | ||
|
||
if (updated != 1) { | ||
|
||
String messageString = "Unable to mark action %d as 'IN_PROGRESS', either the action id was not found, the action is no longer pending or there is another action currently marked 'IN_PROGRESS'"; | ||
|
||
throw new IllegalStateException(format( | ||
messageString, | ||
actionId, | ||
updated)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 4 additions & 10 deletions
14
waltz-jobs/src/main/java/org/finos/waltz/jobs/harness/SurveyInstanceHarness.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,18 @@ | ||
package org.finos.waltz.jobs.harness; | ||
|
||
import org.finos.waltz.data.survey.SurveyInstanceDao; | ||
import org.finos.waltz.data.survey.SurveyQuestionResponseDao; | ||
import org.finos.waltz.model.attestation.SyncRecipientsResponse; | ||
import org.finos.waltz.model.survey.SurveyInstanceFormDetails; | ||
import org.finos.waltz.service.DIConfiguration; | ||
import org.finos.waltz.service.survey.SurveyInstanceEvaluator; | ||
import org.finos.waltz.service.survey.SurveyInstanceActionQueueService; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
|
||
public class SurveyInstanceHarness { | ||
|
||
public static void main(String[] args) { | ||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class); | ||
|
||
SurveyInstanceDao dao = ctx.getBean(SurveyInstanceDao.class); | ||
SurveyInstanceActionQueueService svc = ctx.getBean(SurveyInstanceActionQueueService.class); | ||
|
||
SyncRecipientsResponse reassignRecipientsCounts = dao.getReassignRecipientsCounts(); | ||
svc.performActions(); | ||
|
||
dao.reassignRecipients(); | ||
|
||
System.out.println("-------------"); | ||
System.out.println("------------- Done!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
waltz-model/src/main/java/org/finos/waltz/model/survey/SurveyInstanceActionQueueItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.finos.waltz.model.survey; | ||
|
||
import org.finos.waltz.model.IdProvider; | ||
import org.finos.waltz.model.Nullable; | ||
import org.immutables.value.Value; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Value.Immutable | ||
public abstract class SurveyInstanceActionQueueItem implements IdProvider { | ||
|
||
public abstract SurveyInstanceAction action(); | ||
public abstract Long surveyInstanceId(); | ||
@Nullable | ||
public abstract String actionParams(); | ||
public abstract SurveyInstanceStatus initialState(); | ||
public abstract LocalDateTime submittedAt(); | ||
public abstract String submittedBy(); | ||
@Nullable | ||
public abstract LocalDateTime actionedAt(); | ||
public abstract SurveyInstanceActionStatus status(); | ||
@Nullable | ||
public abstract String message(); | ||
public abstract String provenance(); | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
waltz-model/src/main/java/org/finos/waltz/model/survey/SurveyInstanceActionStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.finos.waltz.model.survey; | ||
|
||
public enum SurveyInstanceActionStatus { | ||
|
||
PENDING, | ||
IN_PROGRESS, | ||
PRECONDITION_FAILURE, | ||
EXECUTION_FAILURE, | ||
SUCCESS | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.