Files
team/server/channels/jobs/batch_report_worker.go
77dd5cc0af MM-68150: Upgrade golangci-lint to v2.12.2 (#36554)
* Simplify invite_people email parsing

Replace backwards in-place mutation loop with a straightforward forward
filter into a new slice. Extract into parseEmailList so the logic can be
unit tested directly.

* MM-68150: Upgrade golangci-lint to v2.12.2

Remove //go:fix inline from NewPointer, which is a generic function not
yet supported by the inline analyzer, and fix 11 slicesbackward
modernize issues flagged by the new version.

* MM-68150: Enable all linters by default; disable those with >20 existing issues

Switch from opt-in (default: none) to opt-out (default: all) so new
linters added to golangci-lint are evaluated automatically. Explicitly
disable every linter that has more than 20 pre-existing violations,
deferring those for later cleanup. Also disable a handful of linters
whose violations are intentional patterns in this codebase (nilerr,
dogsled, sqlclosecheck, iotamixing, predeclared, containedctx, iface,
gocheckcompilerdirectives, promlinter, goprintffuncname, gomoddirectives).

* MM-68150: Fix mirror linter issues

Replace Write([]byte(s)) with WriteString(s), and FindIndex([]byte(s))
with FindStringIndex(s), to avoid unnecessary allocations.

* MM-68150: Fix nosprintfhostport linter issue

Use net.JoinHostPort to construct host:port strings instead of
fmt.Sprintf with a manually formatted pattern.

* MM-68150: Fix rowserrcheck and sqlclosecheck linter issues

Check rows.Err() after iteration loops in schema_dump.go. In the
sqlx_wrapper test, defer rows.Close() rather than closing inline.

* MM-68150: Fix nilnesserr linter issues — wrong variable in error handlers

In 11 places, a stale variable (often the outer err from a prior
assignment) was used instead of the freshly-checked error variable
(appErr, rowErr, jsonErr, writeErr, esErr). Each produces a typed-nil
wrapped in a non-nil interface, silently discarding the real error.

* MM-68150: Add i18n string for app.compile_csv_chunks.write_error

---------

Co-authored-by: Mattermost Build <[email protected]>
2026-05-14 17:29:37 -04:00

141 lines
4.4 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package jobs
import (
"net/http"
"strconv"
"time"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/store"
"github.com/pkg/errors"
)
type BatchReportWorkerAppIFace interface {
SaveReportChunk(format string, prefix string, count int, reportData []model.ReportableObject) *model.AppError
CompileReportChunks(format string, prefix string, numberOfChunks int, headers []string) *model.AppError
SendReportToUser(rctx request.CTX, job *model.Job, format string) *model.AppError
CleanupReportChunks(format string, prefix string, numberOfChunks int) *model.AppError
}
type BatchReportWorker struct {
*BatchWorker
app BatchReportWorkerAppIFace
reportFormat string
headers []string
getData func(jobData model.StringMap) ([]model.ReportableObject, model.StringMap, bool, error)
}
func MakeBatchReportWorker(
jobServer *JobServer,
store store.Store,
app BatchReportWorkerAppIFace,
timeBetweenBatches time.Duration,
reportFormat string,
headers []string,
getData func(jobData model.StringMap) ([]model.ReportableObject, model.StringMap, bool, error),
) *BatchReportWorker {
worker := &BatchReportWorker{
app: app,
reportFormat: reportFormat,
headers: headers,
getData: getData,
}
worker.BatchWorker = MakeBatchWorker(jobServer, store, timeBetweenBatches, worker.doBatch)
return worker
}
func (worker *BatchReportWorker) doBatch(rctx request.CTX, job *model.Job) bool {
reportData, nextData, done, err := worker.getData(job.Data)
if err != nil {
worker.logger.Error("Worker: Failed to get data for report batch. Exiting", mlog.Err(err))
worker.setJobError(worker.logger, job, model.NewAppError("doBatch", model.NoTranslation, nil, "", http.StatusInternalServerError).Wrap(err))
return true
} else if done {
if err = worker.complete(rctx, job); err != nil {
worker.logger.Error("Worker: Failed to finish the batch report. Exiting", mlog.Err(err))
worker.setJobError(worker.logger, job, model.NewAppError("doBatch", model.NoTranslation, nil, "", http.StatusInternalServerError).Wrap(err))
} else {
worker.logger.Info("Worker: Report job complete")
worker.setJobSuccess(worker.logger, job)
}
return true
}
err = worker.processChunk(job, reportData)
if err != nil {
worker.logger.Error("Worker: Failed to save report batch. Exiting", mlog.Err(err))
worker.setJobError(worker.logger, job, model.NewAppError("doBatch", model.NoTranslation, nil, "", http.StatusInternalServerError).Wrap(err))
return true
}
job.Data = nextData
// We might be able to add progress for this type of job in the future
// But for now we can just set to 0
if err := worker.jobServer.SetJobProgress(job, 0); err != nil {
worker.logger.Error("Worker: Failed to set job progress", mlog.Err(err))
return false
}
return false
}
func getFileCount(jobData model.StringMap) (int, error) {
if jobData["file_count"] != "" {
parsedFileCount, parseErr := strconv.Atoi(jobData["file_count"])
if parseErr != nil {
return 0, errors.Wrap(parseErr, "failed to parse file_count")
}
return parsedFileCount, nil
}
// Assume it hasn't been set
return 0, nil
}
func (worker *BatchReportWorker) processChunk(job *model.Job, reportData []model.ReportableObject) error {
fileCount, err := getFileCount(job.Data)
if err != nil {
return err
}
appErr := worker.app.SaveReportChunk(worker.reportFormat, job.Id, fileCount, reportData)
if appErr != nil {
return appErr
}
fileCount++
job.Data["file_count"] = strconv.Itoa(fileCount)
return nil
}
func (worker *BatchReportWorker) complete(rctx request.CTX, job *model.Job) error {
fileCount, err := getFileCount(job.Data)
if err != nil {
return err
}
appErr := worker.app.CompileReportChunks(worker.reportFormat, job.Id, fileCount, worker.headers)
if appErr != nil {
return appErr
}
defer func() {
if err := worker.app.CleanupReportChunks(worker.reportFormat, job.Id, fileCount); err != nil {
worker.logger.Error("Worker: Failed to cleanup report chunks", mlog.Err(err))
}
}()
if appErr = worker.app.SendReportToUser(rctx, job, worker.reportFormat); appErr != nil {
return appErr
}
return nil
}