Search in sources :

Example 1 with SysCluster

use of com.publiccms.entities.sys.SysCluster in project PublicCMS-preview by sanluan.

the class ClusterComponent method heartbeat.

/**
 * 每分钟心跳一次
 */
@SuppressWarnings("unchecked")
@Scheduled(fixedDelay = THEARTBEAT_INTERVAL)
public void heartbeat() {
    if (CmsVersion.isInitialized()) {
        SysCluster entity = service.getEntity(CmsVersion.getClusterId());
        Date now = CommonUtils.getDate();
        Date lastHeartbeatDate = null;
        if (null == entity) {
            entity = new SysCluster(CmsVersion.getClusterId(), now, now, false, CmsVersion.getVersion());
            service.save(entity);
        } else {
            lastHeartbeatDate = entity.getHeartbeatDate();
        }
        service.updateHeartbeatDate(CmsVersion.getClusterId(), now);
        if (CmsVersion.isMaster() != entity.isMaster()) {
            CmsVersion.setMaster(entity.isMaster());
        }
        Date acceptTeartbeatDate = new Date(now.getTime() - (2 * THEARTBEAT_INTERVAL));
        if (entity.isMaster()) {
            PageHandler page = service.getPage(null, acceptTeartbeatDate, false, null, null, null, null);
            for (SysCluster cluster : (List<SysCluster>) page.getList()) {
                service.delete(cluster.getUuid());
            }
        } else {
            PageHandler page = service.getPage(null, null, true, "heartbeatDate", "desc", null, null);
            if (page.getTotalCount() == 0) {
                upgrade();
            } else if (page.getTotalCount() == 1) {
                SysCluster master = (SysCluster) page.getList().get(0);
                if (acceptTeartbeatDate.after(master.getHeartbeatDate())) {
                    upgrade();
                    service.delete(master.getUuid());
                }
            } else {
                boolean skip = false;
                for (SysCluster cluster : (List<SysCluster>) page.getList()) {
                    if (skip) {
                        service.updateMaster(cluster.getUuid(), false);
                    } else {
                        skip = true;
                    }
                }
            }
        }
        scheduledTask.init(lastHeartbeatDate);
    }
}
Also used : PageHandler(com.publiccms.common.handler.PageHandler) List(java.util.List) Date(java.util.Date) SysCluster(com.publiccms.entities.sys.SysCluster) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

PageHandler (com.publiccms.common.handler.PageHandler)1 SysCluster (com.publiccms.entities.sys.SysCluster)1 Date (java.util.Date)1 List (java.util.List)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1