1 /*******************************************************************************
2 
3     Application extension to try to lock the pid file.
4 
5     If `[PidLock]` section with the `path` member is found in the config file,
6     application will try to lock the file specified by `path` and it will abort
7     the execution if that fails, making sure only one application instance per
8     pid-lock file is running.
9 
10     The pid-lock file contains pid of the application that locked the file, and
11     it's meant for the user inspection - the locking doesn't depend on this
12     data.
13 
14     This extension should be use if it's critical that only one instance of the
15     application is running (say, if sharing the working directory between two
16     instances will corrupt data).
17 
18     Copyright:
19         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
20         All rights reserved.
21 
22     License:
23         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
24         Alternatively, this file may be distributed under the terms of the Tango
25         3-Clause BSD License (see LICENSE_BSD.txt for details).
26 
27 *******************************************************************************/
28 
29 module ocean.util.app.ext.PidLockExt;
30 
31 import ocean.util.app.model.ExtensibleClassMixin;
32 import ocean.util.app.model.IApplicationExtension;
33 import ocean.util.app.ext.model.IConfigExtExtension;
34 
35 import ocean.meta.types.Qualifiers;
36 
37 
38 /// ditto
39 class PidLockExt : IConfigExtExtension, IApplicationExtension
40 {
41     import ocean.application.components.PidLock;
42 
43     /***************************************************************************
44 
45         Pid lock wrapper
46 
47     ***************************************************************************/
48 
49     private PidLock pid;
50 
51     /***************************************************************************
52 
53         Order set to -1500, as the extension should run after ConfigExt (-10000)
54         but before LogExt (-1000) (as LogExt can create side effects).
55 
56         Returns:
57             the extension order
58 
59     ***************************************************************************/
60 
61     public override int order ( )
62     {
63         return -1500;
64     }
65 
66     /***************************************************************************
67 
68         Parse the configuration file options to set up the loggers.
69 
70         Params:
71             app = the application instance
72             config = configuration instance
73 
74     ***************************************************************************/
75 
76     public override void processConfig ( IApplication app, ConfigParser config )
77     {
78         this.pid.parseConfig(config);
79     }
80 
81     /***************************************************************************
82 
83         Tries to lock the pid file.
84 
85         Throws:
86             Exception if the locking is not successful.
87 
88     ***************************************************************************/
89 
90     public override void preRun ( IApplication app, istring[] cl_args )
91     {
92         this.pid.lock();
93     }
94 
95     /***************************************************************************
96 
97         Cleans up behind and releases the lock file.
98 
99         Params:
100             app = the application instance that will run
101             args = command line arguments used to invoke the application
102             status = exit status returned by the application
103             exception = exit exception instance, if one was thrown (null
104                 otherwise)
105 
106         Throws:
107             ErrnoException if any of the system calls fails
108 
109     ***************************************************************************/
110 
111     public override void atExit ( IApplication app, istring[] args, int status,
112             ExitException exception )
113     {
114         this.pid.unlock();
115     }
116 
117     /***************************************************************************
118 
119         Unused IApplicationExtension methods.
120 
121         We just need to provide an "empty" implementation to satisfy the
122         interface.
123 
124     ***************************************************************************/
125 
126     /// ditto
127     public override void postRun ( IApplication app, istring[] args, int status )
128     {
129         // Unused
130     }
131 
132     /// ditto
133     public override ExitException onExitException ( IApplication app,
134             istring[] args, ExitException exception )
135     {
136         // Unused
137         return exception;
138     }
139 
140     /***************************************************************************
141 
142         Unused IConfigExtExtension methods.
143 
144         We just need to provide an "empty" implementation to satisfy the
145         interface.
146 
147         Params:
148             app = the application instance
149             config = configuration instance
150 
151     ***************************************************************************/
152 
153     public override void preParseConfig ( IApplication app, ConfigParser config )
154     {
155         // Unused
156     }
157 
158 
159     /***************************************************************************
160 
161         Function to filter the list of configuration files to parse.
162         Only present to satisfy the interface
163 
164         Params:
165             app = the application instance
166             config = configuration instance
167             files = current list of configuration files to parse
168 
169         Returns:
170             new list of configuration files to parse
171 
172     ***************************************************************************/
173 
174     public override istring[] filterConfigFiles ( IApplication app,
175                                          ConfigParser config,
176                                          istring[] files )
177     {
178         // Unused
179         return files;
180     }
181 }