Skip to content

O3-5765: Automatically clear queue entries on a schedule - #119

Open
UjjawalPrabhat wants to merge 9 commits into
openmrs:mainfrom
UjjawalPrabhat:O3-5765-scheduled-queue-clear
Open

O3-5765: Automatically clear queue entries on a schedule#119
UjjawalPrabhat wants to merge 9 commits into
openmrs:mainfrom
UjjawalPrabhat:O3-5765-scheduled-queue-clear

Conversation

@UjjawalPrabhat

@UjjawalPrabhat UjjawalPrabhat commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds AutoCloseQueueEntryTask. Each minute it ends active entries whose startedAt is on or before the most recent occurrence of the configured time of day. So a run missed at that time is caught up by the next one, and entries started since then stay in the queue.

Both this and the existing visit-close task are now registered with core's scheduler as TaskDefinitions from QueueModuleActivator, replacing the module's own QueueTaskExecutor and QueueTimerTask. They appear on the Manage Scheduler page, where the interval can be changed or a task stopped.

Two new global properties:

queue.autoCloseQueueEntriesAtTime - HH:mm, blank by default, which disables clearing. An outpatient clinic would set 23:59.
queue.autoCloseQueueEntriesForQueues - comma-separated queue uuids; blank means all queues.

Blank by default so nothing changes for an existing deployment until an implementer opts in (O3-2443).

Related Issue

O3-5765

Comment thread api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseQueueEntryTask.java Outdated
Comment thread api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseQueueEntryTask.java Outdated
Comment thread omod/src/main/resources/config.xml
@UjjawalPrabhat
UjjawalPrabhat force-pushed the O3-5765-scheduled-queue-clear branch from b9b4520 to fb2a13d Compare July 24, 2026 13:23
@UjjawalPrabhat
UjjawalPrabhat requested a review from dkayiwa July 24, 2026 13:24
Comment thread omod/src/main/resources/config.xml
Comment thread api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseQueueEntryTask.java Outdated
Comment thread api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseQueueEntryTask.java Outdated
@UjjawalPrabhat
UjjawalPrabhat requested a review from dkayiwa July 27, 2026 07:23

@dkayiwa dkayiwa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The PR description still describes an earlier version of this. It says the default is 23:59, warns that existing deployments will begin clearing all queues at end of day once deployed, and describes the task as acting once the clock passes the configured time. None of that holds any more: the property defaults to blank, and the task works back from the most recent occurrence of the configured time. Could you refresh it? It is the first thing anyone coming to this PR reads, and as written it advertises the behaviour that got changed.

@UjjawalPrabhat
UjjawalPrabhat requested a review from dkayiwa July 28, 2026 06:06
Comment thread api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseQueueEntryTask.java Outdated
@UjjawalPrabhat
UjjawalPrabhat requested a review from dkayiwa July 28, 2026 12:26

@ibacher ibacher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While I realize this wasn't added by this module, it would be better to use core's task scheduling features rather than ScheduledExecutorTask. That said, if we are going to use Spring scheduling, then do not call Context.getService() but inject the service from Spring.

Both tasks are now AbstractTasks registered as TaskDefinitions when the module
starts, following the pattern the reference application and chartsearchai
activators use. That retires QueueTaskExecutor and QueueTimerTask along with
the daemon token plumbing, since core runs scheduled tasks as the daemon user
itself, and replaces the two hand rolled re-entrancy flags with the isExecuting
guard AbstractTask provides.

Implementers get both tasks on the Manage Scheduler page, where the interval
can be changed or a task stopped. Nothing here overrides that afterwards: the
scheduler starts tasks with startOnStartup at server startup and restores them
across a module being started or stopped, so registration only happens once.
The definition is scheduled as it is created, which is the one case the
scheduler does not cover, of this module being installed into a running server.

Services stay behind Context lookups because a task built by TaskFactory is not
a Spring bean, which is what core's own AutoCloseVisitsTask does.
@UjjawalPrabhat

Copy link
Copy Markdown
Contributor Author

it would be better to use core's task scheduling features rather than ScheduledExecutorTask.

@ibacher Tried out using core's scheduling feat....have a look!!

@UjjawalPrabhat
UjjawalPrabhat requested a review from ibacher August 1, 2026 10:12
The unit tests stub the services the task talks through, so nothing checked
that the search criteria filter the way they assume, that the save survives
validation, or that endedAt reaches the database. This builds the task the way
the scheduler does, from the class name on a TaskDefinition, and reads the
entry back from the database rather than from the session the task used.
@jwnasambu

Copy link
Copy Markdown

@claude review

@NethmiRodrigo NethmiRodrigo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks @UjjawalPrabhat! A few suggestions -

Comment thread README.md Outdated
Comment thread api/src/main/java/org/openmrs/module/queue/QueueModuleActivator.java Outdated
Comment thread api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseQueueEntryTask.java Outdated
Comment thread api/src/main/java/org/openmrs/module/queue/tasks/AutoCloseQueueEntryTask.java Outdated
Close queue entries through a new QueueEntryService.closeQueueEntry,
which reloads the entry and writes it with optimistic locking, so an
entry transitioned between the query and its turn in the loop is left
alone instead of being overwritten from a stale snapshot. Both tasks
use it, and the unreachable closeActiveQueueEntries is removed.

Also flush and clear the session every 250 entries so the first sweep
after a close time is configured stays linear, wrap the whole task
registration so a lookup failure cannot skip the second task, and
tighten a couple of log messages.
@sonarqubecloud

Copy link
Copy Markdown

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.

5 participants