刘光辉
5 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package jnpf.dmsService.expiry;
 
import jnpf.dmsEntity.storage.expiry.DmsFileExpiryItem;
import jnpf.dmsMapper.DmsFileExpiryMapper;
import jnpf.dmsService.search.meili.DmsMeilisearchClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
@Slf4j
@Service
@RequiredArgsConstructor
public class DmsFileExpirySearchCleanup {
    private final DmsFileExpiryMapper mapper;
    private final DmsMeilisearchClient client;
    private final DmsFileExpiryProperties properties;
 
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void process(DmsFileExpiryItem candidate) {
        DmsFileExpiryItem item = mapper.lockCleanup(candidate.getTenantId(), candidate.getFileVersionId());
        if (item == null) return;
        boolean done = false;
        String error = null;
        try {
            if (item.getSearchTaskId() == null) {
                item.setSearchTaskId(client.deleteDocument(item.getTenantId(), item.getFileVersionId()));
                done = item.getSearchTaskId() == null; // Index does not exist.
            } else {
                String status = client.taskStatus(item.getSearchTaskId());
                done = "succeeded".equals(status);
                if ("failed".equals(status) || "canceled".equals(status) || "missing".equals(status)) {
                    item.setSearchTaskId(null); // Resubmit idempotent deletion on the next scan.
                    error = "Meilisearch task " + status;
                }
            }
        } catch (Exception failure) {
            error = failure.getClass().getSimpleName() + ": " + failure.getMessage();
            log.warn("DMS expiry index cleanup pending tenant={} version={}", item.getTenantId(), item.getFileVersionId(), failure);
        }
        mapper.updateCleanup(item, done, error == null ? null : error.substring(0, Math.min(1000, error.length())),
                Math.max(1, properties.getCleanupRetrySeconds()));
    }
}