修复多线程同时修改任务导致状态有问题的bug;减小预览图像素;修改任务状态发送条件;

This commit is contained in:
chuzhongzai 2023-09-14 11:35:31 +08:00
parent fb5ff43364
commit b0f27784dc
4 changed files with 26 additions and 15 deletions

View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager"> <component name="MavenProjectsManager">

View File

@ -15,6 +15,8 @@ public class GalleryTask {
public static byte COMPRESS_COMPLETE = 4; public static byte COMPRESS_COMPLETE = 4;
public static byte COMPRESSING = 5;
public static byte DOWNLOAD_ALL = 3; public static byte DOWNLOAD_ALL = 3;
public static byte DOWNLOAD_PREVIEW = 2; public static byte DOWNLOAD_PREVIEW = 2;
@ -47,4 +49,9 @@ public class GalleryTask {
public boolean is_compress_complete(){ public boolean is_compress_complete(){
return status == COMPRESS_COMPLETE; return status == COMPRESS_COMPLETE;
} }
@JsonIgnore
public boolean is_compressing(){
return status == COMPRESSING;
}
} }

View File

@ -55,6 +55,12 @@ public class DownloadCheckService {
//扫描进度 //扫描进度
Iterator<File> fileIterator = files.iterator(); Iterator<File> fileIterator = files.iterator();
for(GalleryTask galleryTask: queue){ for(GalleryTask galleryTask: queue){
//跳过已经下载完成或者压缩完成的任务
if(galleryTask.is_download_complete()
|| galleryTask.is_compress_complete()
|| galleryTask.is_compressing())
continue;
while(fileIterator.hasNext()){ while(fileIterator.hasNext()){
File file = fileIterator.next(); File file = fileIterator.next();
if(!file.getName().contains(String.valueOf(galleryTask.getGid()))) if(!file.getName().contains(String.valueOf(galleryTask.getGid())))
@ -78,8 +84,10 @@ public class DownloadCheckService {
//转格式队列 //转格式队列
for(GalleryTask galleryTask: queue) for(GalleryTask galleryTask: queue)
if (galleryTask.is_download_complete()) if (galleryTask.is_download_complete()) {
galleryTask.setStatus(GalleryTask.COMPRESSING);
convert_queue.add(galleryTask); convert_queue.add(galleryTask);
}
return true; return true;
} }
@ -102,7 +110,7 @@ public class DownloadCheckService {
galleryTask.setStatus(ErrorCode.COMPRESS_ERROR); galleryTask.setStatus(ErrorCode.COMPRESS_ERROR);
continue; continue;
} }
//长度相同比较字典序否则比较长度 //仅比较字典序
images = Arrays.stream(images).sorted(Comparator.naturalOrder()).toArray(File[]::new); images = Arrays.stream(images).sorted(Comparator.naturalOrder()).toArray(File[]::new);
//创建文件夹 //创建文件夹
@ -135,6 +143,7 @@ public class DownloadCheckService {
operation = new IMOperation(); operation = new IMOperation();
operation.addImage(images[i].getAbsolutePath()); operation.addImage(images[i].getAbsolutePath());
operation.format("webp"); operation.format("webp");
operation.resize(2000, 2000);
operation.addImage(storagePath + galleryTask.getName() + "/" + images[i].getName().replace(".png", ".webp").replace(".jpg", ".webp")); operation.addImage(storagePath + galleryTask.getName() + "/" + images[i].getName().replace(".png", ".webp").replace(".jpg", ".webp"));
try { try {
convertCmd.run(operation); convertCmd.run(operation);
@ -144,12 +153,13 @@ public class DownloadCheckService {
break; break;
} }
} }
if ((galleryTask.getType() & GalleryTask.DOWNLOAD_SOURCE) != 0) { if ((galleryTask.getType() & GalleryTask.DOWNLOAD_SOURCE) != 0) {
ZipUtil.zip(galleryTask.getPath(), storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip"); ZipUtil.zip(galleryTask.getPath(), storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip");
log.info(galleryTask.getName() + "压缩完成" ); log.info(galleryTask.getName() + "压缩完成" );
} }
FileUtil.del(galleryTask.getPath()); FileUtil.del(galleryTask.getPath());
galleryTask.setStatus(GalleryTask.DOWNLOAD_COMPLETE); galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE);
} }
} }

View File

@ -20,7 +20,6 @@ import java.io.OutputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.ListIterator;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
@ -84,7 +83,7 @@ public class storageNode {
} }
lock.unlock(); lock.unlock();
//检查 //检查
if (!downloadCheckService.downloadCheck()) { if (!downloadCheckService.downloadCheck() && queue.isEmpty()) {
counter++; counter++;
if(server != null && server.isActive() && counter > 10) { if(server != null && server.isActive() && counter > 10) {
server.writeAndFlush(new MaintainMessage()); server.writeAndFlush(new MaintainMessage());
@ -93,19 +92,15 @@ public class storageNode {
return; return;
} }
//发送 //发送
//上锁后再发送避免出现发送完之后再下载完成
lock.lock();
DownloadStatusMessage downloadStatusMessage = new DownloadStatusMessage(); DownloadStatusMessage downloadStatusMessage = new DownloadStatusMessage();
downloadStatusMessage.setGalleryTasks(queue.toArray(GalleryTask[]::new)); downloadStatusMessage.setGalleryTasks(queue.toArray(GalleryTask[]::new));
server.writeAndFlush(downloadStatusMessage); server.writeAndFlush(downloadStatusMessage);
ListIterator<GalleryTask> listIterator = queue.listIterator();
lock.lock();
while (listIterator.hasNext()) {
GalleryTask galleryTask = listIterator.next();
if (galleryTask.is_download_complete())
listIterator.remove();
}
lock.unlock();
log.info("任务状态发送完成"); log.info("任务状态发送完成");
queue.removeIf(GalleryTask::is_compress_complete);
lock.unlock();
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
try (OutputStream outputStream = new FileOutputStream("/root/gallery/storageNode/err.txt")){ try (OutputStream outputStream = new FileOutputStream("/root/gallery/storageNode/err.txt")){