Forum: Ruby Help on a loop EACH

6219fe13b441e60321ed53e1ef462ae7?d=identicon&s=25 Damien Frutiger (frutix)
on 2017-03-08 10:25
Hi everybody,

I work on a PLM and I must modify a RubyScript. After 4 days, I need
help for you on this develop language. I hope you can help me but
I think, is not too difficult for a developer.

My goal: Delete a lock when my script left the loop.

My error: The file create well (teamcenter.lock) in the correct folder
but where I must write the command line:
File.delete(teamcenter_lock_file, 'w')



The code:

        #teamcenter_lock_file = File.join(@options.output_dir,
"teamcenter.lock")
    teamcenter_lock_file = @baan_options['tc_lock_file']
        #
        @log.info("Creating Teamcenter lock file at
#{teamcenter_lock_file}")
        #
        tclock = File.new(teamcenter_lock_file, 'w')
    File.new(teamcenter_lock_file, 'w')
        tclock.close

        files.each do |f|

          src = File.join(@env.srcdir(), f)
          next if not File.exist?(src)

          pf = "1" if f =~ /tiedm010.txt/i
          pf = "2" if f =~ /tiedm010txt.txt/i
          pf = "3" if f =~ /tiedm100.txt/i
          pf = "4" if f =~ /tiedm100txt.txt/i
          pf = "5" if f =~ /tiedm110.txt/i
          # KST -> Smaller filenames!  dst =
File.join(@options.output_dir, sprintf("%s_%s_%s_%s", @job_tag,
@job_date, pf, f))
          dst = File.join(@options.output_dir, sprintf("%s_%s_%s_%s",
@job_tag, @job_date[0,12], pf, f))

      if not File.exist?(dst)
            @log.info "Copy '#{src}' to '#{dst}'"
            begin

        File.open(dst, 'w') {|out|
          out.puts header[f]
          out.write(File.new(src, 'r').read())
        }

              #FileUtils.cp src, dst, :verbose => true
            rescue => error
              @log.error "Failed to copy '#{src}' to '#{dst}'
(#{error})"
            end
          else
            @log.info "Appending '#{src}' to '#{dst}'"
            begin
              File.open(dst, 'a') {|f|
        f.puts ''
        f.write(File.new(src, 'r').read())
        #File.delete(teamcenter_lock_file, 'w')
        }

            rescue => error
              @log.error "Failed to append '#{src}' to '#{dst}'
(#{error})"
            end

          end

        end
        #
        @log.info("Removing Teamcenter lock file at
#{teamcenter_lock_file}")
    #
        begin
           File.delete(teamcenter_lock_file, 'w')
       #FileUtils.rm_f teamcenter_lock_file
        rescue
        end
      end
    end
  end
end




Thank you for your help, if you have questions, contact me. :)
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-03-14 06:58
Basically, you need to "erase the lock file" after you left the critical
region, i.e. the commands which need to be protected by the lock.

Having said this, I would like to point out however, that your program
doesn't safely protect (locks) anything. Simply creating a lock file
which then is (probably) tested by another application, does not
guarantee any locking.

If all application competing for the critical region, run on the same
host, you would need File#flock to manage lock files. See the Ruby Docs
for an example.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.