[MM-63314] Fix ClaimJob in HA environments (#30383)

* ClaimJob now returns newly claimed job

* internal code affected by change

* test changes required

* two branches: for mysql, use transaction; for postgres, use returning

* two branches: for mysql, use transaction; for postgres, use returning

* use same millis value for LastActivityAt and StartAt

* blank commit

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Christopher Poile
2025-03-14 10:24:26 -04:00
коммит произвёл GitHub
родитель c9504925e6
Коммит c049748b88
25 изменённых файлов: 292 добавлений и 183 удалений

Просмотреть файл

@@ -142,13 +142,12 @@ func (w *MessageExportWorker) DoJob(job *model.Job) {
logger.Debug("Worker: Received a new candidate job.")
defer w.jobServer.HandleJobPanic(logger, job)
claimed, appErr := w.jobServer.ClaimJob(job)
var appErr *model.AppError
job, appErr = w.jobServer.ClaimJob(job)
if appErr != nil {
logger.Info("Worker: Error occurred while trying to claim job", mlog.Err(appErr))
logger.Warn("Worker: Error occurred while trying to claim job", mlog.Err(appErr))
return
}
if !claimed {
} else if job == nil {
return
}

Просмотреть файл

@@ -263,9 +263,13 @@ func TestDoJobNoPostsToExport(t *testing.T) {
Status: model.JobStatusPending,
Type: model.JobTypeMessageExport,
}
retJob := *job
retJob.Status = model.JobStatusInProgress
// claim job succeeds
mockStore.JobStore.On("UpdateStatusOptimistically", job.Id, model.JobStatusPending, model.JobStatusInProgress).Return(true, nil)
mockStore.JobStore.
On("UpdateStatusOptimistically", job.Id, model.JobStatusPending, model.JobStatusInProgress).
Return(&retJob, nil)
mockMetrics.On("IncrementJobActive", model.JobTypeMessageExport)
// no previous job, data will be loaded from config
@@ -285,7 +289,7 @@ func TestDoJobNoPostsToExport(t *testing.T) {
)
// job completed successfully
mockStore.JobStore.On("UpdateOptimistically", job, model.JobStatusInProgress).Return(true, nil)
mockStore.JobStore.On("UpdateOptimistically", mock.AnythingOfType("*model.Job"), model.JobStatusInProgress).Return(true, nil)
mockStore.JobStore.On("UpdateStatus", job.Id, model.JobStatusSuccess).Return(job, nil)
mockMetrics.On("DecrementJobActive", model.JobTypeMessageExport)
@@ -342,9 +346,13 @@ func TestDoJobWithDedicatedExportBackend(t *testing.T) {
Status: model.JobStatusPending,
Type: model.JobTypeMessageExport,
}
retJob := *job
retJob.Status = model.JobStatusInProgress
// claim job succeeds
mockStore.JobStore.On("UpdateStatusOptimistically", job.Id, model.JobStatusPending, model.JobStatusInProgress).Return(true, nil)
mockStore.JobStore.
On("UpdateStatusOptimistically", job.Id, model.JobStatusPending, model.JobStatusInProgress).
Return(&retJob, nil)
mockMetrics.On("IncrementJobActive", model.JobTypeMessageExport)
// no previous job, data will be loaded from config
@@ -395,7 +403,7 @@ func TestDoJobWithDedicatedExportBackend(t *testing.T) {
)
// job completed successfully
mockStore.JobStore.On("UpdateOptimistically", job, model.JobStatusInProgress).Return(true, nil)
mockStore.JobStore.On("UpdateOptimistically", mock.AnythingOfType("*model.Job"), model.JobStatusInProgress).Return(true, nil)
mockStore.JobStore.On("UpdateStatus", job.Id, model.JobStatusSuccess).Return(job, nil)
mockMetrics.On("DecrementJobActive", model.JobTypeMessageExport)
@@ -503,8 +511,13 @@ func TestDoJobCancel(t *testing.T) {
worker, ok := impl.MakeWorker().(*MessageExportWorker)
require.True(t, ok)
retJob := *job
retJob.Status = model.JobStatusInProgress
// Claim job succeeds
mockStore.JobStore.On("UpdateStatusOptimistically", job.Id, model.JobStatusPending, model.JobStatusInProgress).Return(true, nil)
mockStore.JobStore.
On("UpdateStatusOptimistically", job.Id, model.JobStatusPending, model.JobStatusInProgress).
Return(&retJob, nil)
mockMetrics.On("IncrementJobActive", model.JobTypeMessageExport)
// No previous job, data will be loaded from config