Skip to content

Commit e0169ca

Browse files
author
YangSen-qn
committed
pfop
1 parent 109f868 commit e0169ca

File tree

6 files changed

+115
-14
lines changed

6 files changed

+115
-14
lines changed

src/main/java/com/qiniu/processing/OperationManager.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public String pfop(String bucket, String key, String fops) throws QiniuException
8080
* @param bucket 空间名
8181
* @param key 文件名
8282
* @param fops fops指令,如果有多个指令,需要使用分号(;)进行拼接,例如 avthumb/mp4/xxx|saveas/xxx;vframe/jpg/xxx|saveas/xxx
83-
* @param params notifyURL、force、pipeline 等参数
83+
* @param params notifyURL、force、pipeline、type等参数
8484
* @return persistentId 请求返回的任务ID,可以根据该ID查询任务状态
8585
* @throws QiniuException 触发失败异常,包含错误响应等信息
8686
* <a href="http://developer.qiniu.com/dora/api/persistent-data-processing-pfop"> 相关链接 </a>
@@ -154,8 +154,35 @@ public String pfop(String bucket, String key, String fops, String pipeline, bool
154154
*/
155155
public String pfop(String bucket, String key, String fops, String pipeline, String notifyURL, boolean force)
156156
throws QiniuException {
157-
StringMap params = new StringMap().putNotEmpty("pipeline", pipeline).
158-
putNotEmpty("notifyURL", notifyURL).putWhen("force", 1, force);
157+
StringMap params = new StringMap()
158+
.putNotEmpty("type", "1")
159+
.putNotEmpty("pipeline", pipeline)
160+
.putNotEmpty("notifyURL", notifyURL)
161+
.putWhen("force", 1, force);
162+
return pfop(bucket, key, fops, params);
163+
}
164+
165+
/**
166+
* 发送请求对空间中的文件进行持久化处理
167+
*
168+
* @param bucket 空间名
169+
* @param key 文件名
170+
* @param fops fop指令
171+
* @param pipeline 持久化数据处理队列名称
172+
* @param notifyURL 处理结果通知地址,任务完成后自动以POST方式将处理结果提交到指定的地址
173+
* @param type 任务类型,0:非闲时任务,1:闲时任务
174+
* @param force 用于对同一个指令进行强制处理时指定,一般用于覆盖空间已有文件或者重试失败的指令
175+
* @return persistentId 请求返回的任务ID,可以根据该ID查询任务状态
176+
* @throws QiniuException 触发失败异常,包含错误响应等信息
177+
* <a href="http://developer.qiniu.com/dora/api/persistent-data-processing-pfop"> 相关链接 </a>
178+
*/
179+
public String pfop(String bucket, String key, String fops, String pipeline, String notifyURL, String type, boolean force)
180+
throws QiniuException {
181+
StringMap params = new StringMap()
182+
.putNotEmpty("type", type)
183+
.putNotEmpty("pipeline", pipeline)
184+
.putNotEmpty("notifyURL", notifyURL)
185+
.putWhen("force", 1, force);
159186
return pfop(bucket, key, fops, params);
160187
}
161188

src/main/java/com/qiniu/processing/OperationStatus.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,49 @@ public class OperationStatus {
99
* 持久化处理的进程ID,即 persistentId
1010
*/
1111
public String id;
12+
1213
/**
1314
* 状态码 0 成功,1 等待处理,2 正在处理,3 处理失败,4 通知提交失败
1415
*/
1516
public int code;
17+
1618
/**
1719
* 与状态码相对应的详细描述
1820
*/
1921
public String desc;
22+
2023
/**
2124
* 处理源文件的文件名
2225
*/
2326
public String inputKey;
27+
2428
/**
2529
* 处理源文件所在的空间名
2630
*/
2731
public String inputBucket;
32+
2833
/**
2934
* 云处理操作的处理队列
3035
*/
3136
public String pipeline;
37+
3238
/**
3339
* 云处理请求的请求id,主要用于七牛技术人员的问题排查
3440
*/
3541
public String reqid;
42+
43+
/**
44+
* 是否是闲时任务
45+
* 0:非闲时任务
46+
* 1:显示任务
47+
*/
48+
public String type;
49+
50+
/**
51+
* 任务创建时间
52+
*/
53+
public String creationDate;
54+
3655
/**
3756
* 云处理操作列表,包含每个云处理操作的状态信息
3857
*/

src/main/java/com/qiniu/storage/Api.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ public static class Request implements Cloneable {
395395
* @param urlPrefix 请求的 urlPrefix, scheme + host
396396
*/
397397
protected Request(String urlPrefix) {
398+
if (StringUtils.isNullOrEmpty(urlPrefix)) {
399+
return;
400+
}
401+
398402
try {
399403
URL url = new URL(urlPrefix);
400404
this.scheme = url.getProtocol();
@@ -634,6 +638,15 @@ public StringMap getHeader() throws QiniuException {
634638
return header;
635639
}
636640

641+
/**
642+
* 构造 header 信息,如果需要设置请求体,子类需要重写
643+
*
644+
* @throws QiniuException 组装 header 时的异常,一般为缺失必要参数的异常
645+
*/
646+
protected void buildHeader() throws QiniuException {
647+
648+
}
649+
637650
/**
638651
* 获取 URL
639652
*
@@ -804,6 +817,7 @@ boolean canRetry() {
804817
protected void prepareToRequest() throws QiniuException {
805818
buildPath();
806819
buildQuery();
820+
buildHeader();
807821
buildBodyInfo();
808822
}
809823

@@ -1052,14 +1066,18 @@ protected RequestBody get() {
10521066
}
10531067

10541068
final MultipartBody.Builder b = new MultipartBody.Builder();
1055-
b.addFormDataPart(name, fileName, body);
1056-
1057-
fields.forEach(new StringMap.Consumer() {
1058-
@Override
1059-
public void accept(String key, Object value) {
1060-
b.addFormDataPart(key, value.toString());
1061-
}
1062-
});
1069+
if (StringUtils.isNullOrEmpty(name)) {
1070+
b.addFormDataPart(name, fileName, body);
1071+
}
1072+
1073+
if (fields != null) {
1074+
fields.forEach(new StringMap.Consumer() {
1075+
@Override
1076+
public void accept(String key, Object value) {
1077+
b.addFormDataPart(key, value.toString());
1078+
}
1079+
});
1080+
}
10631081

10641082
b.setType(MediaType.parse("multipart/form-data"));
10651083

src/main/java/com/qiniu/storage/ApiInterceptorRetryHosts.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ Api.Response intercept(Api.Request request, Api.Handler handler) throws QiniuExc
3737
}
3838

3939
String reqHost = request.getHost();
40-
String firstHost = hostProvider.provider();
41-
if (!hostProvider.isHostValid(reqHost) && !StringUtils.isNullOrEmpty(firstHost)) {
42-
request.setHost(firstHost);
40+
if (!hostProvider.isHostValid(reqHost)) {
41+
// 支持不配置默认的 host,未配置则从 provider 中获取
42+
String firstHost = hostProvider.provider();
43+
if (!StringUtils.isNullOrEmpty(firstHost)) {
44+
request.setHost(firstHost);
45+
} else {
46+
throw QiniuException.unrecoverable("no host provide");
47+
}
4348
}
4449

4550
if (retryMax == 0) {

src/main/java/com/qiniu/util/Auth.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public final class Auth {
4848
"fsizeMin",
4949
"trafficLimit",
5050

51+
"persistentType",
5152
"persistentOps",
5253
"persistentNotifyUrl",
5354
"persistentPipeline",

src/test/java/test/com/qiniu/processing/PfopTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.qiniu.processing.OperationStatus;
88
import com.qiniu.storage.Configuration;
99
import com.qiniu.storage.Region;
10+
import com.qiniu.util.Auth;
11+
import com.qiniu.util.StringMap;
1012
import com.qiniu.util.StringUtils;
1113
import com.qiniu.util.UrlSafeBase64;
1214
import org.junit.jupiter.api.Tag;
@@ -138,4 +140,33 @@ private void testPfopIsSuccess(String jobid) {
138140
assertEquals(0, status.code);
139141
}
140142

143+
@Test
144+
@Tag("IntegrationTest")
145+
public void testPfopA() {
146+
try {
147+
Auth auth = TestConfig.testAuth;
148+
Map<String, Region> bucketKeyMap = new HashMap<String, Region>();
149+
TestConfig.TestFile[] files = TestConfig.getTestFileArray();
150+
for (TestConfig.TestFile testFile : files) {
151+
bucketKeyMap.put(testFile.getBucketName(), testFile.getRegion());
152+
}
153+
for (Map.Entry<String, Region> entry : bucketKeyMap.entrySet()) {
154+
String bucket = entry.getKey();
155+
Region region = entry.getValue();
156+
157+
Configuration cfg = new Configuration(region);
158+
OperationManager operationManager = new OperationManager(auth, cfg);
159+
String jobID = operationManager.pfop(bucket, TestConfig.testMp4FileKey, "avinfo", "", "", "1", true);
160+
161+
OperationStatus status = operationManager.prefop(bucket, jobID);
162+
assertNotNull(status, "1. prefop type error");
163+
assertTrue(status.code == 0 || status.code == 1 || status.code == 3, "2. prefop type error");
164+
assertEquals("1", status.type, "3. prefop type error");
165+
}
166+
167+
} catch (QiniuException ex) {
168+
ex.printStackTrace();
169+
assertTrue(ResCode.find(ex.code(), ResCode.getPossibleResCode(612)));
170+
}
171+
}
141172
}

0 commit comments

Comments
 (0)