开发者

Ruby file locking when deploying Windows service

I'm deploying a Windows service with a Ruby script. After copying files to the server using FileUtils.cp, I run sc \\MYSERVER start MyService via Ruby's cmd syntax. This command returns the following error for each of 20 consecutive attempts, at five second intervals:

[SC] StartService FAILED 32:

The process cannot access the file because it is being used by another process.

If I run the command manually immediately after my Ruby script ends, it works fine:

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              :

Is FileUtils.cp possibly putting a lock on the copied EXE? If not, what else in my script could possibly be holding this lock?

Here's pretty much what my script looks like, without all the automatic retry code and configuration:

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`   

Here's the copy function, which recursively copies directories and files:

# 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?
    开发者_如何学C            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


I found the issue. The MyService.exe.config file was being copied out in a separate method, which did some manipulation of the source file content before creating a new file on the server. The new file was not being closed, so attempting to start the service failed when it couldn't get a lock on the config file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜