programming erlang ch18 - andstudy/forge GitHub Wiki

18์žฅ OTP๋กค ์‹œ์Šคํ…œ ๊ตฌ์ถ•ํ•˜๊ธฐ

๋ชฉ์ฐจ 1.๋ฒ”์šฉ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ 2.์˜ค๋ฅ˜๋กœ๊ทธ 3.์•Œ๋žŒ ๊ด€๋ฆฌ 4.์Šˆํผ๋น„์ ผํŠธ๋ฆฌ 5.์• ํ”Œ๋ฆฌ์ผ€์ด์…˜

1.์ด๋ฒคํŠธ : ์ผ์–ด๋‚˜๋Š” ์–ด๋–ค ๊ฒƒ...

RegProcName ! {evnet. E}	
	RegProcName : ๋“ฑ๋ก๋œ ํ”„๋กœ์„ธ์Šค ์ด๋ฆ„
	E : ์ด๋ฒคํŠธ

- ์ด๋ฒค๋“œํ•ธ๋“ค๋Ÿฌ API
event_handler:make(Name)    	: ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ƒ์„ฑ
event_handler:make(Name, X)		: Name ์ด๋ผ๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋กœ ์ด๋ฒคํŠธ X ์ „์†ก
event_handler:make(Name, Fun)	: Name ์ด๋ผ๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์— ํ•ธ๋“ค๋Ÿฌ Fun์„ ์ถ”๊ฐ€

ex.1) event_handler.erl 
	
ex.2)	motor_controller.erl
	event_handler:add_handler(errors, fun controller/1).   
			==> ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” errors ๋ผ๋Š” ์ด๋ฒค๋“œ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค.
				( event_handler:make(errors). )
				
์˜ค๋ฅ˜๋กœ๊ทธ / ์•Œ๋žŒ์ฒ˜๋ฆฌ์˜ ํ•˜๋ถ€ ๊ตฌ์กฐ๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํŒจํ„ด์„ ๋”ฐ๋ฅธ๋‹ค.

2.์˜ค๋ฅ˜๋กœ๊ทธ - ์˜ค๋ฅ˜ ๋กœ๊ทธ๋ฅผ ๋ณด๋Š” 3๊ฐ€์ง€ ๊ด€์  1.ํ”„๋กœ๊ทธ๋ž˜๋จธ ๊ด€์  : ๋กœ๊ทธ ๋‚จ๊ธฐ๊ธฐ ์œ„ํ•ด ํ”Œ๋จธ๊ฐ€ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜ 2.๊ตฌ์„ฑ์„ค์ • ๊ด€์  : ์˜ค๋ฅ˜๋กœ๊ทธ๊ฐ€ ๋กœ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์œ„์น˜ 3.๋ณด๊ณ ์„œ ๊ด€์  : ๊ธฐ๋ก๋œ ์˜ค๋ฅ˜ ์–ด๋–ป๊ฒŒ ๋ถ„์„ํ•˜๋‚˜

- ์˜ค๋ฅ˜๋กœ๊ทธ API
error_logger:error_msg(String) -> ok
	==> ์ฝ˜์†”๋กœ ์˜ค๋ฅ˜๋ฉ”์„ธ์ง€ ๋ณด๋ƒ„
error_logger:error_msg(Format, Data) -> ok    
	==> ์ธ์ž๋Š” io:format(Format, Data) ์™€ ๋™์ผ
error_logger:error_report(Report) -> ok
		
error_msg ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค.. -> ์ถ”๊ฐ€์ ์ธ ๋‚ด์šฉ์€ ๋ฉ”๋‰ด์–ผ ์ฐธ์กฐ...
		ex) error_logger:warning_map().
				error_logger:error_report(Type, Report) -> ok
	
- ์˜ค๋ฅ˜๋กœ๊ทธ ์„ค์ • ๋ฐฉ๋ฒ•
	์ฝ˜์†”์—์„œ ํ™•์ธ, ํŒŒ์ผ๋กœ ์ €์žฅ, ์ˆœํ™˜๋กœ๊ทธ๋กœ ์ €์žฅ

- ๋กœ๊ทธ ํŒŒ์ผ ์„ค์ • ์ธ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์–ผ๋žญ ์‹œ์ž‘ํ•˜๊ธฐ
1.erl -boot start_clean  ==> ์ธ์ž์—†์ด ์‹คํ–‰๊ณผ ๋™์ผ
2.erl -boot start_sasl   
	==> ์‹œ์Šคํ…œ์•„ํ‚คํ…์ณ ์ง€์› ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์˜ค๋ฅ˜ ๋กœ๊น…๊ณผ ๋ถ€ํ•˜ ๋ฐฉ์ง€๋ฅผ ๋‹ด๋‹น
3.erl -boot start_sasl -config elog1
	==> elog1.config ํŒŒ์ผ์— ๊ธฐ์ˆ ๋œ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ์„œ ๋กœ๊ทธ ์ถœ๋ ฅ ๋ฐ ํŒŒ์ผ ์ƒ์„ฑ
	* config file : ์˜ค๋ฅ˜ ๋กœ๊ทธ ์„ค์ • ( ๋””๋ ‰ํ† ๋ฆฌ/๋กœ๊ทธํŒŒ์ผ ๊ฐฏ์ˆ˜/์ˆœํ™˜๋กœ๊ทธyn )
	
sample) elog1..4.config ํŒŒ์ผ 
	%% no tty 
	[{sasl, [
	 {sasl_error_logger, false}  ==> ์˜ค์ง ์˜ค๋ฅ˜๋งŒ. ์ง„ํ–‰๋ณด๊ณ  ํŒจ์Šค ===> ์˜ค์ง ์‰˜๋กœ๋งŒ ์ถœ๋ ฅ๋จ..
	 ...
	 {sasl_error_logger, {file, "/home/joe/error_logs/THELOG"}} ==> ์ƒ๋Œ€๊ฒฝ๋กœ์ด์šฉ. homepath ์— ์„ค์ •.
	 				==> ํ•ด๋‹น ํŒŒ์ผ์— ๋กœ๊ทธ ๊ธฐ๋ก..
	 ...
	 {error_logger_mf_dir,"./mslogs4"},   ==> ํด๋”์ƒ์„ฑ ์•ˆ๋จ..ใ…ก.ใ…ก;
	 ...
	 {errlog_type, error}, ==> ์˜ค์ง ์˜ค๋ฅ˜๋งŒ ๋ณด๊ณ .. ( ์ •๋ณด์„ฑ ๋ณด๊ณ  / ์ง„ํ–‰ ๋ณด๊ณ  ํŒจ์Šค..)
	]}].	
	
		
- ์˜ค๋ฅ˜ ๋ถ„์„
MODULE
	rb 
MODULE SUMMARY
	The Report Browser Tool 
DESCRIPTION
	The Report Browser (RB) tool makes it possible to browse and format error reports 
	written by the error logger handler log_mf_h. 

rb:grep(RegExp)
	Types : RegExp = string()
	
	All reports containing the regular expression RegExp are printed. 		
	RegExp is a string containing the regular expression. 
	Refer to the module regexp in the STDLIB reference manual 
	for a definition of valid regular expressions. 
	They are essentially the same as the UNIX command egrep. 

rb:start{max, MaxNoOfReports}. 
	: Controls how many reports the rb_server should read on start-up.
  1. ์•Œ๋žŒ ๊ด€๋ฆฌ
    • ์ œ๋„ค๋ฆญ ์„œ๋ฒ„๋Š” Mod:init(InitArgs) ํ˜ธ์ถœ๋กœ ์‹œ์ž‘ํ•œ๋‹ค.

    • handle_event(Event, State) Event : {EventType, EventArg} ํ˜•ํƒœ์˜ ํŠœํ”Œ

4.์Šˆํผ๋น„์ ผํŠธ๋ฆฌ - ํ”„๋กœ์„ธ์Šค๋“ค์˜ ๊ตฌ์กฐ - ์ƒ์œ„ ํ”„๋กœ์„ธ์Šค๋Š” ํŠธ๋ฆฌ์— ์žˆ๋Š” ํ•˜์œ„ ํ”„๋กœ์„ธ์Šค(์›Œ์ปค)๋ฅผ ๊ฐ์‹œํ•˜๋ฉด์„œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํŒจํ• ๊ฒฝ์šฐ ์žฌ์‹œ์ž‘ ์‹œํ‚จ๋‹ค.

* ํŠธ๋ฆฌ ์œ ํ˜•
	- ์ผ๋Œ€์ผ ์Šˆํผ๋น„์ ผ ํŠธ๋ฆฌ
		์–ด๋–ค ์›Œ์ปค๊ฐ€ ์‹คํŒจํ•˜๋ฉด ๊ทธ ํ”„๋กœ์„ธ์Šค๋Š” ์Šˆํผ๋ฐ”์ด์ €์— ์˜ํ•ด ์žฌ์‹œ์ž‘
	- ์ผ๋Œ€๋‹ค ์Šˆํผ๋น„์ ผ ํŠธ๋ฆฌ
		์–ด๋–ค ์›Œ์ปค๊ฐ€ ์ฃฝ์œผ๋ฉด ๋ชจ๋“  ์›Œ์ปค๋ฅผ ์ฃฝ์ด๊ณ  ๋ชจ๋‘ ์žฌ์‹œ์ž‘

* ์Šˆํผ๋น„์ ผ ํ•จ์ˆ˜ ํ˜•ํƒœ
init(...) ->
	{ok, {RestartStrategy, MaxRestart, Time},
		[Worker1, Worker2, Worker3, ...]
	}
	
	- RestartStrategy : ์• ํ…€ ์ผ๋Œ€์ผ / ์ผ๋Œ€๋‹ค ๊ฐ€์šด๋ฐ ํ•˜๋‚˜
	- MaxRestart, Time : ์žฌ ์‹œ์ž‘ ๋นˆ๋„ (Time ๋‚ด์— MaxRestart ์ด์ƒ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์›Œ์ปค๋ฅผ ๋ชจ๋‘ ์ฃฝ์ด๊ณ  ์ž์‹ ๋„ ์ข…๋ฃŒ)
	- [{Worker1}..n] : ์›Œ์ปค ํ”„๋กœ์„ธ์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ์‹œ์ž‘ํ• ์ง€ ๊ธฐ์ˆ 

	Worker ์ƒ์„ธ
	{Tag, {Mod, Func, ArgList},
					Restart,
					Shutdown,
					Type,
					[Mod1] }
	- Tag			: ์›Œ์ปคํ”„๋กœ์„ธ์Šค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์• ํ…€ํƒœ๊ทธ
	- {Mod, Func, ArgList} : ์Šˆํผ๋ฐ”์ด์ €๊ฐ€ ์›Œ์ปค๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ์‚ฌ์šฉํ•  ํ•จ์ˆ˜ ์ •
	- Restart : permanent/transient/tempory
	- Shutdown : ์ค‘๋‹จ ์ตœ๋Œ€ํ—ˆ์šฉ ์‹œ๊ฐ„
	- Type : worker/supervisor ํ”„๋กœ์„ธ์Šค ์œ ํ˜•
	- Mod1 : ์ฝœ๋ฐฑ ๋ชจ๋“ˆ์˜ ์ด๋ฆ„

5.์• ํ”Œ๋ฆฌ์ผ€์ด์…˜

MODULE
	application 
MODULE SUMMARY
	Generic OTP application functions 
DESCRIPTION						
	In OTP, application denotes a component implementing some specific functionality, 
	that can be started and stopped as a unit, and which can be re-used in other systems 
	as well. This module interfaces the application controller, a process started at every 
	Erlang runtime system, and contains functions for controlling applications (for example 
	starting and stopping applications), and functions to access information about
	 applications (for example configuration parameters). 

	OTP ์‹œ์Šคํ…œ์—์„œ Application์€ ํ•˜๋‚˜์˜ ๋‹จ์œ„๋กœ์„œ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ์ค‘์ง€ ๋ ์ˆ˜ ์žˆ๋Š” ํŠนํ™”๋œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ 
	์ปดํฌ๋„ŒํŠธ ์ด๋‹ค. ์ด ๋ชจ๋“ˆ์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํŠธ๋กค ๊ฐ„์˜ ์ž‘์—…, ๋ชจ๋“  ์–ผ๋žญ ์‹œ์Šคํ…œ ํ”„๋กœ์„ธ์„œ, ์‹œ์ž‘/์ •์ง€
	๋“ฑ์˜ ์ž‘์—… ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜, ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ ์ ‘์†ํ•˜๋Š” ๊ธฐ๋Šฅ ๋“ฑ์— ๊ด€์—ฌํ•œ๋‹ค.		
	
	An application is defined by an application specification. ๋ช…์„ธ์„œ์— ์ •์˜๋˜์–ด์žˆ๋‹ค.
	The specification is normally located in an application resource file called Application.app, 
	where Application is the name of the application. Refer to app(4) for more information about
	 the application specification. 

	This module can also be viewed as a behaviour for an application implemented according to 
	the OTP design principles as a supervision tree. The definition of how to start and stop the 
	tree should be located in an application callback module exporting a pre-defined set of functions. 


- loaded_applications() -> [{Application, Description, Vsn}]

	Types:
		Application = atom()
		Description = string()
		Vsn = string()

	Returns a list with information about the applications which have been loaded using load/1,2, 
	also included applications. Application is the application name. Description and Vsn are the values 
	of its description and vsn application specification keys, respectively. 



=* File Types *=
	The following file types are defined in Erlang/OTP: 

=* File Types *=
	
	File Type  									File Name/Extension  		Documented in  
	module  										.erl  									Erlang Reference Manual  
	include file  							.hrl  									Erlang Reference Manual  
	release resource file  			.rel  									rel(4)  
	application resource file  	.app  									app(4)  
	boot script  								.script  								script(4)  
	binary boot script  				.boot  									-  
	configuration file  				.config  								config(4)  
	application upgrade file  	.appup  								appup(4)  
	release upgrade file  			relup  									relup(4)