Android Source:优雅的repo sync自动重试

问题

  • 下载代码被墙

    修改 hosts 文件

  • repo sync 时候卡住

  • repo sync 时候中断

代码

要保证同时只有一个 repo 在运行

#!/bin/bash

# 当前 repo sync 进程的 pid
PID=


kill_all_prog() {
    # 用ps找出所有的repo, 然后kill掉
    echo "kill all prog(s)"
    PIDS=`ps aux |grep python|grep [r]epo |awk '{print $2}'`
    [[ -n $PIDS ]] && kill $PIDS
}

kill_prog() {
    # kill 当前repo sync子进程
    if [[ -n $PID ]]; then
      echo "kill : $PID"
      kill $PID
    fi
}

start_sync() {
    # 启动子进程(使用coproc)
    coproc syncproc { repo sync; }
    PID=$syncproc_PID
}

restart_sync() {
    kill_prog
    kill_all_prog
    start_sync
}

# 如果网络流量在retry_delay时间内小于min_speed, 则认为repo sync已经卡住了

min_speed="50"
retry_delay=300

((counter=0))
((n_retries=0))

kill_all_prog
restart_sync

while [[ 1 ]]; do
    # 用ifstat检测网速
    speed=`ifstat 1 1 | tail -n 1 | awk '{print $1}'`
    result=$(echo "$speed < $min_speed" | bc)
    if [[ $result == "1" ]]; then
        ((counter++))
    else
        ((counter=0))
    fi
    if [[ `ps -p $PID| wc -l` == "1" ]]; then
        # 检测到子进程已经退出(ps已经查不到它了)

        # 用wait取得子进程返回值
        wait $PID

        if [[ $? -eq 0 ]]; then
            echo "sync successful"
            break
        else
            echo "sync failed"
            ((counter=0))
            ((n_retries++))
            restart_sync
            continue
        fi
    fi
    if ((counter > retry_delay)); then
        ((counter=0))
        echo "netspeed low. restart!"
        ((n_retries++))
        restart_sync
    elif [[ counter -gt 20 ]]; then
       echo -e "\n" "netspeed low counter is " $counter "\n";
    fi
done

echo "completed with $n_retries retries"

废弃的代码

下面代码也是要保证只能有一个 repo 命令执行,且执行成功后还会执行

#!/bin/bash

# FIXME: 只允许同时一个repo运行

kill_prog() {
    # 用ps找出所有的repo, 然后kill掉
    PID=`ps aux |grep python|grep [r]epo |awk '{print $2}'`
    [[ -n $PID ]] && kill $PID
}

start_sync() {
    repo sync &
}

restart_sync() {
    kill_prog
    start_sync
}

# 如果网络流量在retry_delay时间内小于min_speed, 则认为repo sync已经卡住了

min_speed="50"
retry_delay=600
((counter=0))
((n_retries=0))

restart_sync


while [[ 1 ]]; do
    # 用ifstat检测网速
    speed=`ifstat 1 1 | tail -n 1 | awk '{print $1}'`

    result=$(echo "$speed < $min_speed" | bc)
    if [[ $result == "1" ]]; then
        ((counter++))
    else
        ((counter=0))
    fi
    if ((counter > retry_delay)); then
        ((counter=0))
        echo "netspeed low. restart!"
        ((n_retries++))
        restart_sync
    elif [[ counter -gt 10 ]]; then
       echo -e "\n" "netspeed low counter is " $counter "\n";
    fi
done

echo "completed with $n_retries retries"

来自

http://blog.csdn.net/xia0pang/article/details/20281071

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值