Sha256: 7f1d4b4dc648bcb7a9b4e55171986107953d3ff85f69bc0f04240291245dc7db

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

module SimplerWorkflow

	module ParentProcess

		# This class is aimed to be used with daemons gem or something similar
		# it launches as many workers as required, which is just running the on boot time block n times
		# a sample daemon using daemons gem would like this:

		#	#!/usr/bin/env/ ruby
		#	require 'rubygems'
		#	require 'bundler/setup'
		#	require 'daemons'
		#	dirmode = :normal
		#	# Check if we're in a deployment machine
		#	# to store logs in /var/run or in the app folder
		#	if ENV['ENV']
		#		log_dir = "/var/log/swf"
		#		pid_dir = "/var/run/swf"
		#	else
		#		log_dir = File.expand_path '../log/', __FILE__
		#		pid_dir = File.expand_path '../log/pid', __FILE__
		#	end
		#	script_path = File.expand_path '../boot.rb', __FILE__
		#	Daemons.run script_path, {
		#		:app_name   => "my_daemon",
		#		:dir_mode   => dirmode,
		#		:log_dir	=> log_dir,
		#		:dir        => pid_dir,
		#		:multiple   => false,
		#		:monitor    => true,
		#		:log_output => true,
		#		# backtrace causes errors detecting as uncatched
		#		# some correctly handled exceptions. Keep disabled.
		#		:backtrace  => false
		#  	}


		require 'fileutils'

		def self.extended(base)

			$logger = Logger.new STDOUT
			
			base.extend ClassMethods

			Signal.trap('TERM') { base.graceful_exit 'QUIT'}

			Signal.trap('INT') { base.graceful_exit 'INT'}
			
		end

		module ClassMethods

			def graceful_exit(s)
				SimplerWorkflow.child_processes.each do |child| 
					Process.kill(s, child)
				end
			end
			
			def workers(val)
				@workers = val
			end

			def log_level(val)
				@log_level = val
			end

			def on_boot(&block)
					$logger.level = @log_level if @log_level
					# separate this execution in the log
					$logger.info "Booting with #{@workers} workers----------------------------"
					@workers.times { yield }
					# we wait for all children processes to exit; when QUIT is sent
					# we terminate them and this will automatically exit.
					Process.waitall
				
			end

		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simpler_workflow-0.3.3 lib/simpler_workflow/parent_process.rb
simpler_workflow-0.3.2 lib/simpler_workflow/parent_process.rb