Search in sources :

Example 1 with RedisLock

use of com.ibeiliao.deployment.common.util.redis.RedisLock in project Corgi by kevinYin.

the class LogSyncService method startupSyncShellLog.

/**
 * 启动同步shell日志线程
 */
public void startupSyncShellLog() {
    popLogLock = new RedisLock(redis, "deployment:popShellLogLock", 2);
    for (int i = 0; i < LogSyncConfig.getInstance().getSyncShellLogThreadCount(); i++) {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                int exCount = 0;
                while (threadIsRunning.get()) {
                    try {
                        syncShellLog();
                        exCount = 0;
                    } catch (InterruptedException e) {
                        threadIsRunning.set(false);
                        break;
                    } catch (Exception e) {
                        logger.error("同步shell日志失败 | msg:{}", e.getMessage(), e);
                        // 防止 redis 挂掉的时候,磁盘瞬间满
                        exCount = (exCount + 1) % 20;
                        try {
                            Thread.sleep((exCount * 1000L) % 19000L + 1000L);
                        } catch (InterruptedException e1) {
                            break;
                        }
                    }
                }
                logger.info("同步shell日志线程结束");
            }
        });
        thread.setName("同步shell日志线程-" + i);
        syncThreads.add(thread);
        thread.start();
    }
}
Also used : RedisLock(com.ibeiliao.deployment.common.util.redis.RedisLock)

Example 2 with RedisLock

use of com.ibeiliao.deployment.common.util.redis.RedisLock in project Corgi by kevinYin.

the class ProcessTimeoutTask method process.

/**
 * 每15分钟处理一次
 */
@Scheduled(cron = "0 */15 * * * ?")
public void process() {
    // lock 时间要超过5分钟,防止上一个任务没处理完,新的任务又来了
    // 1800 秒最多可以占 1800/5 次定时任务
    RedisLock redisLock = new RedisLock(redis, "deployment_timeout_task_check", 1800);
    if (redisLock.lock()) {
        logger.info("准备处理超时任务 ...");
        try {
            Date now = new Date();
            Date startTime = DateUtils.addSeconds(now, -Constants.DEPLOY_TASK_TIMEOUT * 3);
            Date endTime = DateUtils.addSeconds(now, -Constants.DEPLOY_TASK_TIMEOUT);
            final int MAX = 1000;
            logger.info("startTime: {}, endTime: {}", startTime, endTime);
            List<DeployHistory> list = deployHistoryService.queryUnfinished(startTime, endTime, MAX);
            if (list.size() > 0) {
                for (DeployHistory history : list) {
                    deployHistoryService.systemCancel(history.getHistoryId());
                }
                logger.info("处理超时任务数量: " + list.size());
            }
        } catch (Exception e) {
            logger.error("处理超时任务出错", e);
        } finally {
            redisLock.unlock();
        }
    } else {
        logger.info("抢锁失败,跳过 ......");
    }
}
Also used : RedisLock(com.ibeiliao.deployment.common.util.redis.RedisLock) DeployHistory(com.ibeiliao.deployment.admin.vo.deploy.DeployHistory) Date(java.util.Date) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 3 with RedisLock

use of com.ibeiliao.deployment.common.util.redis.RedisLock in project Corgi by kevinYin.

the class StatTask method dailyStat.

/**
 * 每日统计
 */
@Scheduled(cron = "0 0 1 * * ?")
public void dailyStat() {
    RedisLock redisLock = new RedisLock(redis, "task_daily_stat", 120);
    if (redisLock.lock()) {
        try {
            Date yesterday = DateUtils.addDays(new Date(), -1);
            statService.statDate(yesterday);
        } finally {
            redisLock.unlock();
        }
    } else {
        logger.info("抢锁失败,跳过 ......");
    }
}
Also used : RedisLock(com.ibeiliao.deployment.common.util.redis.RedisLock) Date(java.util.Date) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

RedisLock (com.ibeiliao.deployment.common.util.redis.RedisLock)3 Date (java.util.Date)2 Scheduled (org.springframework.scheduling.annotation.Scheduled)2 DeployHistory (com.ibeiliao.deployment.admin.vo.deploy.DeployHistory)1