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 moduleocean.util.app.ext.PidLockExt;
30 31 importocean.util.app.model.ExtensibleClassMixin;
32 importocean.util.app.model.IApplicationExtension;
33 importocean.util.app.ext.model.IConfigExtExtension;
34 35 importocean.meta.types.Qualifiers;
36 37 38 /// ditto39 classPidLockExt : IConfigExtExtension, IApplicationExtension40 {
41 importocean.application.components.PidLock;
42 43 /***************************************************************************
44 45 Pid lock wrapper
46 47 ***************************************************************************/48 49 privatePidLockpid;
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 publicoverrideintorder ( )
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 publicoverridevoidprocessConfig ( IApplicationapp, ConfigParserconfig )
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 publicoverridevoidpreRun ( IApplicationapp, 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 publicoverridevoidatExit ( IApplicationapp, istring[] args, intstatus,
112 ExitExceptionexception )
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 /// ditto127 publicoverridevoidpostRun ( IApplicationapp, istring[] args, intstatus )
128 {
129 // Unused130 }
131 132 /// ditto133 publicoverrideExitExceptiononExitException ( IApplicationapp,
134 istring[] args, ExitExceptionexception )
135 {
136 // Unused137 returnexception;
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 publicoverridevoidpreParseConfig ( IApplicationapp, ConfigParserconfig )
154 {
155 // Unused156 }
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 publicoverrideistring[] filterConfigFiles ( IApplicationapp,
175 ConfigParserconfig,
176 istring[] files )
177 {
178 // Unused179 returnfiles;
180 }
181 }