Skip to content

Commit

Permalink
feature: update http sync jst job
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqi committed Nov 12, 2024
1 parent 42ca066 commit 38036d3
Show file tree
Hide file tree
Showing 26 changed files with 646 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum DataSourceConstants {

public static final String MAPPER_FRAMEWORK_DAG_PACKAGE = "cn.sliew.carp.framework.dag.repository.mapper";
public static final String MAPPER_MODULE_DATASOURCE_PACKAGE = "cn.sliew.carp.module.datasource.repository.mapper";
public static final String MAPPER_MODULE_HTTP_SYNC_PACKAGE = "cn.sliew.carp.module.http.sync.repository.mapper";
public static final String MAPPER_MODULE_HTTP_SYNC_PACKAGE = "cn.sliew.carp.module.http.sync.framework.repository.mapper";
public static final String MAPPER_MODULE_KUBERNETES_PACKAGE = "cn.sliew.carp.module.kubernetes.repository.mapper";
public static final String MAPPER_MODULE_PLUGIN_PACKAGE = "cn.sliew.carp.module.plugin.repository.mapper";
public static final String MAPPER_MODULE_SCHEDULER_PACKAGE = "cn.sliew.carp.module.scheduler.repository.mapper";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.http.sync.framework.model;

import cn.sliew.carp.module.http.sync.framework.model.internal.ProcessResult;
import cn.sliew.carp.module.http.sync.framework.model.internal.SimpleJobContext;
import cn.sliew.milky.common.exception.Rethrower;
import cn.sliew.milky.common.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.pekko.Done;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.typed.ActorSystem;
import org.apache.pekko.actor.typed.SpawnProtocol;
import org.apache.pekko.japi.Pair;
import org.apache.pekko.stream.*;
import org.apache.pekko.stream.javadsl.*;

import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;

@Slf4j
public abstract class AbstractJob implements Job {

protected ActorSystem<SpawnProtocol.Command> actorSystem;

public AbstractJob(ActorSystem<SpawnProtocol.Command> actorSystem) {
this.actorSystem = actorSystem;
}

@Override
public void process(String param) {
doExecute(param);
}

protected void doExecute(Object param) {
SimpleJobContext context = buildJobContext();
JobProcessor processor = buildJobProcessor(context);
RootTask rootTask = buildRootTask(param);

Source<SubTask, UniqueKillSwitch> source = Source.single(rootTask)
.mapConcat(root -> processor.map(root))
.viaMat(KillSwitches.single(), Keep.right());

Flow<SubTask, ProcessResult, NotUsed> process = Flow.<SubTask>create()
.map(subTask -> processor.process(subTask)).mapAsync(1, future -> future);

Flow<SubTask, ProcessResult, NotUsed> subTasks =
Flow.fromGraph(
GraphDSL.create(
b -> {
int concurrency = context.getSubTaskParallelism();
UniformFanOutShape<SubTask, SubTask> partition =
b.add(Partition.create(concurrency, subTask -> Math.toIntExact(subTask.getIdentifier()) % concurrency));
UniformFanInShape<ProcessResult, ProcessResult> merge =
b.add(MergeSequence.create(concurrency, result -> result.getSubTask().getIdentifier()));

for (int i = 0; i < concurrency; i++) {
b.from(partition.out(i))
.via(b.add(process.async()))
.viaFanIn(merge);
}

return FlowShape.of(partition.in(), merge.out());
}));

Pair<UniqueKillSwitch, CompletionStage<Done>> pair = source.via(subTasks)
.log(getJobName())
.toMat(Sink.foreach(result -> processor.reduce(result)), Keep.both())
.run(actorSystem);
UniqueKillSwitch killSwitch = pair.first();
try {
pair.second().toCompletableFuture().get(1, TimeUnit.HOURS);
} catch (Exception e) {
log.error("job 执行异常, job: {}, param: {}", getJobName(), JacksonUtil.toJsonString(param));
killSwitch.abort(e);
Rethrower.throwAs(e);
}
}

public abstract String getJobName();

protected abstract SimpleJobContext buildJobContext();

protected JobProcessor buildJobProcessor(SimpleJobContext context) {
return new DefaultJobProcessor(context);
}

protected abstract RootTask buildRootTask(Object param);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.http.sync.framework.model;

public interface Job {

void process(String param);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<artifactId>carp-framework-exception</artifactId>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>carp-framework-web</artifactId>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>carp-module-http-framework</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@EnableFeignClients(basePackages = {
"cn.sliew.carp.module.http.sync"
})
public class DefaultFeignConfig {
public class HttpSyncFeignConfig {

@Autowired
private MappingJackson2HttpMessageConverter jacksonConverter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.http.sync.job.config;

import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpSyncOpenAPIConfig {

@Bean
public GroupedOpenApi carpHttpSyncModuleOpenApi() {
return GroupedOpenApi.builder().group("Http同步模块")
.pathsToMatch("/api/carp/http-sync/**")
.packagesToScan("cn.sliew.carp.module.http.sync").build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.http.sync.job.controller.job;

import cn.sliew.carp.framework.common.security.annotations.AnonymousAccess;
import cn.sliew.carp.framework.web.response.ApiResponseWrapper;
import cn.sliew.carp.module.http.sync.job.jst.order.JstOrderJob;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@AnonymousAccess
@RestController
@ApiResponseWrapper
@RequestMapping("/api/carp/http-sync/job/jst")
@Tag(name = "Http同步管理-聚水潭")
public class CarpJstController {

@Autowired
private JstOrderJob jstOrderJob;

@GetMapping("jstOrderJob")
@Operation(summary = "订单", description = "订单")
public void jstOrderJob(@RequestParam("param") String param) {
jstOrderJob.process(param);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.http.sync.job.enums;

import lombok.Getter;

@Getter
public enum JobGroup {

JST("jst", "聚水潭"),
;

private String group;
private String desc;

JobGroup(String group, String desc) {
this.group = group;
this.desc = desc;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.http.sync.job.enums;

import lombok.Getter;

@Getter
public enum JobType {

NORMAL("normal", "普通任务"),
;

private String type;
private String desc;

JobType(String type, String desc) {
this.type = type;
this.desc = desc;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.http.sync.job.enums;

import lombok.Getter;

@Getter
public enum JstApiEnum {

SHOPS_QUERY("shops.query", "店铺查询"),
WMS_PARTNER_QUERY("wms.partner.query", "仓库查询"),
CATEGORY_QUERY("category.query", "商品类目查询"),
LOGISTIC_QUERY("logistic.query", "发货信息查询"),
COMBINE_SKU_QUERY("combine.sku.query", "组合商品查询"),
INVENTORY_COUNT_QUERY("inventory.count.query", "库存盘点查询"),
INVENTORY_QUERY("inventory.query", "库存查询"),
PACK_QUERY("pack.query", "箱及仓位库存查询-按照修改时间查询"),
MALL_ITEM_QUERY("mall.item.query", "普通商品查询(按款查询)"),
PURCHASEIN_QUERY("purchasein.query", "采购入库查询"),
PURCHASEOUT_QUERY("purchaseout.query", "采购退货查询"),
PURCHASE_QUERY("purchase.query", "采购单查询"),
SKUMAP_QUERY("skumap.query", "商品映射查询"),
SKU_QUERY("sku.query", "普通商品资料查询(按sku查询)"),
SUPPLIER_QUERY("supplier.query", "供应商查询"),
AFTERSALE_RECEIVED_QUERY("aftersale.received.query", "实际收货查询"),
ALLOCATE_QUERY("allocate.query", "调拨单查询"),
ORDER_ACTION_QUERY("order.action.query", "订单操作日志查询"),
OTHER_INOUT_QUERY("other.inout.query", "其它出入库查询"),
JUSHUITAN_INOUT_WATER_QUERY("jushuitan.inout.water.query", "进出仓流水"),
JUSHUITAN_TRACKINFO_QUERY("jushuitan.trackinfo.query", "通过唯一码查询物流日志"),

ORDERS_SINGLE_QUERY("orders.single.query", "订单查询"),
REFUND_SINGLE_QUERY("refund.single.query", "退货退款查询"),
ORDERS_OUT_SIMPLE_QUERY("orders.out.simple.query", "销售出库查询"),

ORDERS_HISTORY_QUERY("orders.history.query", "历史订单查询"),
REFUND_HISTORY_QUERY("refund.history.query", "历史退货退款查询"),
JUSHUITAN_SALEOUT_HISTORY_QUERY("jushuitan.saleout.history.query", "历史销售出库单查询"),

JUSHUITAN_ORDER_LIST_QUERY("jushuitan.order.list.query", "聚水潭奇门云订单"),
JUSHUITAN_REFUND_LIST_QUERY("jushuitan.refund.list.query", "聚水潭奇门云售后"),
JUSHUITAN_SALEOUT_LIST_QUERY("jushuitan.saleout.list.query", "聚水潭奇门云销售出库"),
;

private String api;
private String desc;

JstApiEnum(String api, String desc) {
this.api = api;
this.desc = desc;
}
}
Loading

0 comments on commit 38036d3

Please sign in to comment.