Search in sources :

Example 1 with ConfigPush

use of com.qlangtech.tis.runtime.pojo.ConfigPush in project tis by qlangtech.

the class AppSynAction method doInitAppFromDaily.

// private ITerminatorTriggerBizDalDAOFacade triggerContext;
/**
 * 接收从日常环境中推送上来的配置文件,<br>
 * 当第一次推送的时候线上还不存在索引实例的时候,会自动创建索引实例
 *
 * @param context
 * @throws Exception
 */
@Func(PermissionConstant.APP_ADD)
public void doInitAppFromDaily(Context context) throws Exception {
    String content = null;
    try (InputStream reader = this.getRequest().getInputStream()) {
        content = IOUtils.toString(reader, getEncode());
        if (StringUtils.isEmpty(content)) {
            throw new IllegalArgumentException("upload content can not be null");
        }
    }
    final ConfigPush configPush = (ConfigPush) HttpConfigFileReader.xstream.fromXML(content);
    final String collection = configPush.getCollection();
    ISchemaPluginContext schemaPlugin = SchemaAction.createSchemaPlugin(collection);
    // 校验当前的snapshot 版本是否就是传输上来的snapshot版本
    ServerGroup serverGroup = null;
    if (configPush.getRemoteSnapshotId() != null) {
        serverGroup = this.getServerGroupDAO().load(collection, (short) 0, /* groupIndex */
        RunEnvironment.ONLINE.getId());
        if (serverGroup.getPublishSnapshotId() != (configPush.getRemoteSnapshotId() + 0)) {
            this.addErrorMessage(context, "exist snapshotid:" + serverGroup.getPublishSnapshotId() + " is not equal push snapshotid:" + configPush.getRemoteSnapshotId());
            return;
        }
    }
    // List<UploadResource> resources = configPush.getUploadResources();
    Snapshot snapshot = null;
    SnapshotDomain snapshotDomain = null;
    Application app = null;
    ApplicationCriteria criteria = new ApplicationCriteria();
    criteria.createCriteria().andProjectNameEqualTo(collection);
    List<Application> apps = this.getApplicationDAO().selectByExample(criteria);
    for (Application p : apps) {
        app = p;
        break;
    }
    if (app == null) {
        // 在服务端创建新应用
        app = new Application();
    // Integer newAppid = this.createNewApp(context, configPush);
    // app.setAppId(newAppid);
    }
    String snycDesc = "NEW CREATE";
    serverGroup = this.getServerGroupDAO().load(collection, (short) 0, /* groupIndex */
    RunEnvironment.ONLINE.getId());
    boolean newSnapshot = false;
    if (serverGroup == null || serverGroup.getPublishSnapshotId() == null) {
        snapshot = new Snapshot();
        snapshot.setSnId(-1);
        snapshot.setPreSnId(-1);
        snapshot.setAppId(app.getAppId());
        newSnapshot = true;
    } else {
        snycDesc = "PUSH FROM DAILY";
        snapshotDomain = this.getSnapshotViewDAO().getView(configPush.getRemoteSnapshotId());
        snapshot = snapshotDomain.getSnapshot();
    }
    if (snapshot == null) {
        throw new IllegalStateException("snapshot can not be null,collection:" + collection);
    }
    snapshot.setCreateUserId(0l);
    snapshot.setCreateUserName(configPush.getReception());
    // ///////////////////////////////////
    // 组装新的snapshot
    PropteryGetter pGetter = null;
    for (UploadResource res : configPush.getUploadResources()) {
        pGetter = ConfigFileReader.createPropertyGetter(res.getResourceType());
        // 校验配置是否相等
        if (!newSnapshot) {
            final String md5 = ConfigFileReader.md5file(res.getContent());
            if (StringUtils.equals(md5, pGetter.getMd5CodeValue(snapshotDomain))) {
                this.addErrorMessage(context, "resource " + pGetter.getFileName() + " is newest,shall not be updated");
                return;
            }
        }
        Integer newResId = ResSynManager.createNewResource(context, schemaPlugin, res.getContent(), ConfigFileReader.md5file(res.getContent()), pGetter, this, this);
        snapshot = pGetter.createNewSnapshot(newResId, snapshot);
    }
    serverGroup = new ServerGroup();
    serverGroup.setPublishSnapshotId(SaveFileContentAction.createNewSnapshot(snapshot, snycDesc, this, 0l, configPush.getReception()));
    serverGroup.setUpdateTime(new Date());
    ServerGroupCriteria serverGroupCriteria = new ServerGroupCriteria();
    serverGroupCriteria.createCriteria().andAppIdEqualTo(app.getAppId()).andRuntEnvironmentEqualTo(RunEnvironment.ONLINE.getId()).andGroupIndexEqualTo((short) 0);
    this.getServerGroupDAO().updateByExampleSelective(serverGroup, serverGroupCriteria);
    // /////////////////////////////////////
    this.addActionMessage(context, "synsuccess");
}
Also used : SnapshotDomain(com.qlangtech.tis.manage.common.SnapshotDomain) InputStream(java.io.InputStream) Date(java.util.Date) PropteryGetter(com.qlangtech.tis.manage.common.PropteryGetter) ISchemaPluginContext(com.qlangtech.tis.solrdao.ISchemaPluginContext) ConfigPush(com.qlangtech.tis.runtime.pojo.ConfigPush) Func(com.qlangtech.tis.manage.spring.aop.Func)

Aggregations

PropteryGetter (com.qlangtech.tis.manage.common.PropteryGetter)1 SnapshotDomain (com.qlangtech.tis.manage.common.SnapshotDomain)1 Func (com.qlangtech.tis.manage.spring.aop.Func)1 ConfigPush (com.qlangtech.tis.runtime.pojo.ConfigPush)1 ISchemaPluginContext (com.qlangtech.tis.solrdao.ISchemaPluginContext)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1