duannao1920 2018-12-18 11:56
浏览 21

如何阻止重复进程?

I have been working on a workaround so that if a container is started and meanwhile a duplicate container is started, so it should not proceed to run rather blocked until first container get exited successfully. To achieve this i used flock but this certainly is not helping me, it works sometimes, i.e, One container aquire flock while another get blocked on it but sometimes both aquire flock and proceed to run. My use case is other container should wait until other container get exited successfully. Here is the code i'm using:

// Flock locks the jiva.lock file. If the file does not exist, it is                                                                                   
// created. If a new process is trying to access the same file it will                                                                                 
// return an error "resource temporarily unavailable".                                                                                                 
func Flock(dir string) error {                                                                                                                         
     logrus.Info("Take flock on jiva.lock file")                                                                                                        
     file, err := os.OpenFile(dir+"/jiva.lock", os.O_CREATE|os.O_RDWR, 0644)                                                                            
     if err != nil {                                                                                                                                    
        return err                                                                                                                                     
     }
     // golang.org/x/sys/unix                                                                                                                                                 
     err = unix.Flock(int(file.Fd()), unix.LOCK_EX|unix.LOCK_NB)                                                                                        
     if err != nil {                                                                                                                                    
        file.Close()                                                                                                                                   
        return fmt.Errorf("Failed to flock, error: %v", err)                                                                                           
     }                                                                                                                                                  
     logrus.Infof("flock successful on file: %v", file.Name())                                                                                          
     return nil                                                                                                                                         
}

I'm using bind mount so that the file jiva.lock is get shared bw two containers. This is not working even if i'm running the binaries directly on my system and in docker container most of the time in both cases.I also tried to debug it with strace but it also shows that both have acquired flock successfully. Any help will be appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗
    • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路