代码之家  ›  专栏  ›  技术社区  ›  MikeWyatt

部署Windows服务时的Ruby文件锁定

  •  0
  • MikeWyatt  · 技术社区  · 14 年前

    我正在部署一个带有Ruby脚本的Windows服务。使用FileUtils.cp将文件复制到服务器后,我运行 sc \\MYSERVER start MyService 通过鲁比 cmd syntax

    [SC] StartService FAILED 32:
    
    The process cannot access the file because it is being used by another process.
    

    如果我在Ruby脚本结束后立即手动运行该命令,它可以正常工作:

    SERVICE_NAME: MyService
            TYPE               : 10  WIN32_OWN_PROCESS
            STATE              : 2  START_PENDING
                                    (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
            WIN32_EXIT_CODE    : 0  (0x0)
            SERVICE_EXIT_CODE  : 0  (0x0)
            CHECKPOINT         : 0x0
            WAIT_HINT          : 0x0
            PID                : 21736
            FLAGS              :
    

    FileUtils.cp是否可能锁定复制的EXE?如果没有,我的脚本中还有什么可能持有这个锁?

    在没有所有自动重试代码和配置的情况下,我的脚本大致如下:

    srcRoot = Pathname.new 'c:\\MyService'
    destRoot = Pathname.new '\\\\MYSERVER\\services\\MyService'
    
    destRoot.each_entry() {|item| 
        if not %w(. ..).include?( item.to_s )
            FileUtils.rm_r destRoot.to_s + "\\" + item.to_s, :force => true
        end
    } 
    
    destRoot.mkdir unless destRoot.exist?
    
    for dir in %w(Release)
        copy(src_root + dir + ".", destRoot) { destRoot + dir }
    end
    
    `sc \\\\MYSERVER start MyService`   
    

    下面是copy函数,它递归地复制目录和文件:

    # recursively copies the given source file or directory to the given destination directory.
    def copy( src, destDir )
        src = Pathname.new src
        destDir = Pathname.new destDir  
        destDir.mkdir unless destDir.exist?
        exclusions = %w(. .. .svn _svn Thumbs.db)
    
        for item in Dir.glob( src + "*" )
            itemPath = Pathname.new item
    
            if not %w(. .. .svn _svn Thumbs.db).include?( itemPath.basename.to_s )
                if itemPath.directory?
                    copy( itemPath, destDir + itemPath.basename ) {destDir + itemPath.basename}
                elsif exclusions.select {|k,v| extension? k}.select {|k,v| item.include? k}.empty?
                    begin
                        FileUtils.cp( itemPath, destDir, {:verbose => true, :preserve => true} )
                    rescue
                        puts "Warning! " + $!
                    end
                end
            end
        end
    end
    
    1 回复  |  直到 14 年前
        1
  •  0
  •   MikeWyatt    14 年前

    我发现了问题。MyService.exe.config文件是用一个单独的方法复制出来的,该方法在服务器上创建新文件之前对源文件内容进行了一些操作。新文件未被关闭,因此当无法锁定配置文件时,尝试启动服务失败。