-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add async job event bus publisher #12971
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
base: 4.22
Are you sure you want to change the base?
Changes from 3 commits
68c69df
26e5d3c
8f996ac
97b15fc
1848fb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -184,6 +184,7 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, | |||||||||||||||||||
| private volatile long _executionRunNumber = 1; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private final ScheduledExecutorService _heartbeatScheduler = Executors.newScheduledThreadPool(1, new NamedThreadFactory("AsyncJobMgr-Heartbeat")); | ||||||||||||||||||||
| private final ExecutorService _eventBusPublisher = Executors.newSingleThreadExecutor(new NamedThreadFactory("AsyncJobMgr-EventBus")); | ||||||||||||||||||||
| private ExecutorService _apiJobExecutor; | ||||||||||||||||||||
| private ExecutorService _workerJobExecutor; | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -1378,6 +1379,7 @@ public boolean start() { | |||||||||||||||||||
| @Override | ||||||||||||||||||||
| public boolean stop() { | ||||||||||||||||||||
| _heartbeatScheduler.shutdown(); | ||||||||||||||||||||
| _eventBusPublisher.shutdown(); | ||||||||||||||||||||
| _apiJobExecutor.shutdown(); | ||||||||||||||||||||
| _workerJobExecutor.shutdown(); | ||||||||||||||||||||
|
Comment on lines
1379
to
1384
|
||||||||||||||||||||
| return true; | ||||||||||||||||||||
|
|
@@ -1397,8 +1399,17 @@ protected AsyncJobManagerImpl() { | |||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private void publishOnEventBus(AsyncJob job, String jobEvent) { | ||||||||||||||||||||
| _messageBus.publish(null, AsyncJob.Topics.JOB_EVENT_PUBLISH, PublishScope.LOCAL, | ||||||||||||||||||||
| new Pair<AsyncJob, String>(job, jobEvent)); | ||||||||||||||||||||
| try { | ||||||||||||||||||||
| _eventBusPublisher.submit(new ManagedContextRunnable() { | ||||||||||||||||||||
| @Override | ||||||||||||||||||||
| protected void runInContext() { | ||||||||||||||||||||
| _messageBus.publish(null, AsyncJob.Topics.JOB_EVENT_PUBLISH, PublishScope.LOCAL, | ||||||||||||||||||||
| new Pair<AsyncJob, String>(job, jobEvent)); | ||||||||||||||||||||
|
||||||||||||||||||||
| _messageBus.publish(null, AsyncJob.Topics.JOB_EVENT_PUBLISH, PublishScope.LOCAL, | |
| new Pair<AsyncJob, String>(job, jobEvent)); | |
| try { | |
| _messageBus.publish(null, AsyncJob.Topics.JOB_EVENT_PUBLISH, PublishScope.LOCAL, | |
| new Pair<AsyncJob, String>(job, jobEvent)); | |
| } catch (Throwable t) { | |
| logger.warn("Failed to publish async job event on message bus. jobId={}, jobEvent={}", | |
| job != null ? job.getId() : null, jobEvent, t); | |
| } |
Copilot
AI
Apr 7, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
publishOnEventBus now captures and publishes the live AsyncJob object asynchronously. Callers frequently mutate the same AsyncJobVO instance after calling publishOnEventBus (e.g., updateAsyncJobStatus mutates job inside the following transaction), so subscribers may observe a job state that no longer corresponds to the jobEvent being emitted (notably the "submit" event can be delivered with a job already in-progress or completed). To preserve event semantics, publish an immutable snapshot (e.g., jobId + jobEvent + selected fields copied at enqueue time, or re-fetch a fresh immutable view inside the publisher thread).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nested try catch block, can you make a new method for the inner one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Executors.newSingleThreadExecutor(...)uses an unbounded LinkedBlockingQueue. If_messageBus.publish()blocks (the original issue) for extended periods, this queue can grow without bound and increase memory pressure or OOM under sustained load. Consider using a boundedThreadPoolExecutor(still single-threaded) with an explicit queue size and a rejection policy (e.g., log+drop or CallerRuns as a backpressure mechanism) that matches desired guarantees for job event delivery.