Sha256: 716ce07ce0e96da3b4febf5723ee5d710bde9bb13dbd8b261e1022e6cce58977
Contents?: true
Size: 1.89 KB
Versions: 12
Compression:
Stored size: 1.89 KB
Contents
require 'json' module Flydata module Command module ExclusiveRunnable def self.included base base.extend ClassMethods base.class_variable_set(:@@exclusive_run_home, ENV['HOME']) end module ClassMethods def run_exclusive(method, options = {}) saved_method = :"__#{method}" alias_method saved_method, method define_method(method) do |*args| result = nil exclusive_run_info = nil begin info = self.class.load_exclusive_run_info if info raise "Command `#{info['command']}` is already running. Wait until the command completes or stop the command. (pid:#{info['pid']})" end exclusive_run_info = { command: self.class.command(method, options), pid: Process.pid } self.class.save_exclusive_run_info(exclusive_run_info) result = send(saved_method, *args) ensure self.class.delete_exclusive_run_info if exclusive_run_info end result end end def exclusive_run_home @@exclusive_run_home end def exclusive_run_home=(value) @@exclusive_run_home = value end def exclusive_run_info_path File.join(exclusive_run_home, "exclusive_run.info") end def load_exclusive_run_info path = exclusive_run_info_path return nil unless File.exists?(path) JSON.parse(File.open(path){|f| f.read}) end def save_exclusive_run_info(info) path = exclusive_run_info_path File.open(path, "w"){|f| f.write(info.to_json)} end def delete_exclusive_run_info path = exclusive_run_info_path File.delete(path) if File.exists?(path) end def command(subcommand, options = {}) return options[:command] if options[:command] modules = self.name.split('::') cmd = modules.last.downcase subcommand == :run ? cmd : "#{cmd}:#{subcommand}" end end end end end
Version data entries
12 entries across 12 versions & 1 rubygems