PageRenderTime 24ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/python/php/sdk/google/appengine/api/modules/ModulesService.php

https://gitlab.com/gregtyka/frankenserver
PHP | 426 lines | 218 code | 37 blank | 171 comment | 26 complexity | d6aaad8385d15e6889251f8feb80eb46 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2007 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * An API for fetching information about and controlling App Engine Modules.
  19. *
  20. */
  21. namespace google\appengine\api\modules;
  22. use google\appengine\runtime\ApiProxy;
  23. use google\appengine\runtime\ApplicationError;
  24. use google\appengine\GetDefaultVersionRequest;
  25. use google\appengine\GetDefaultVersionResponse;
  26. use google\appengine\GetHostnameRequest;
  27. use google\appengine\GetHostnameResponse;
  28. use google\appengine\GetModulesRequest;
  29. use google\appengine\GetModulesResponse;
  30. use google\appengine\GetNumInstancesRequest;
  31. use google\appengine\GetNumInstancesResponse;
  32. use google\appengine\GetVersionsRequest;
  33. use google\appengine\GetVersionsResponse;
  34. use google\appengine\ModulesServiceError\ErrorCode;
  35. use google\appengine\SetNumInstancesRequest;
  36. use google\appengine\SetNumInstancesResponse;
  37. use google\appengine\StartModuleRequest;
  38. use google\appengine\StartModuleResponse;
  39. use google\appengine\StopModuleRequest;
  40. use google\appengine\StopModuleResponse;
  41. final class ModulesService {
  42. private static function errorCodeToException($error) {
  43. switch($error) {
  44. case ErrorCode::INVALID_MODULE:
  45. return new ModulesException('Invalid module.');
  46. case ErrorCode::INVALID_VERSION:
  47. return new ModulesException('Invalid version.');
  48. case ErrorCode::INVALID_INSTANCES:
  49. return new ModulesException('Invalid instances.');
  50. case ErrorCode::TRANSIENT_ERROR:
  51. return new TransientModulesException('Temporary error, please re-try');
  52. case ErrorCode::UNEXPECTED_STATE:
  53. return new InvalidModuleStateException('Module in an unexpected state');
  54. default:
  55. return new ModulesException('Error Code: ' . $error);
  56. }
  57. }
  58. /**
  59. * Gets the name of the currently running module.
  60. *
  61. * @return string The name of the current module. For example, if this is
  62. * version "v1" of module "module5" for app "my-app", this function
  63. * will return "module5".
  64. */
  65. public static function getCurrentModuleName() {
  66. return $_SERVER['CURRENT_MODULE_ID'];
  67. }
  68. /**
  69. * Gets the version of the currently running module.
  70. *
  71. * @return string The name of the current module. For example, if this is
  72. * version "v1" of module "module5" for app "my-app", this function
  73. * will return "v1".
  74. */
  75. public static function getCurrentVersionName() {
  76. return explode('.', $_SERVER['CURRENT_VERSION_ID'])[0];
  77. }
  78. /**
  79. * Gets the id of the currently running instance.
  80. *
  81. * @return string The name of the current module. For example, if this is
  82. * instance 2 of version "v1" of module "module5" for app "my-app", this
  83. * function will return "2". For automatically-scaled modules, this function
  84. * will return a unique hex string for the instance (e.g.
  85. * "00c61b117c7f7fd0ce9e1325a04b8f0df30deaaf").
  86. */
  87. public static function getCurrentInstanceId() {
  88. return $_SERVER['INSTANCE_ID'];
  89. }
  90. /**
  91. * Gets an array of all the modules for the application.
  92. *
  93. * @return string[] An array of string containing the names of the modules
  94. * associated with the application. The 'default' module will be included if
  95. * it exists, as will the name of the module that is associated with the
  96. * instance that calls this function.
  97. */
  98. public static function getModules() {
  99. $req = new GetModulesRequest();
  100. $resp = new GetModulesResponse();
  101. ApiProxy::makeSyncCall('modules', 'GetModules', $req, $resp);
  102. return $resp->getModuleList();
  103. }
  104. /**
  105. * Get an array of all versions associated with a module.
  106. *
  107. * @param string $module The name of the module to retrieve the versions for.
  108. * If null then the versions for the current module will be retrieved.
  109. *
  110. * @return string[] An array of strings containing the names of versions
  111. * associated with the module. The current version will also be included in
  112. * this list.
  113. *
  114. * @throws \InvalidArgumentException If $module is not a string.
  115. * @throws ModulesException If the given $module isn't valid.
  116. * @throws TransientModulesException if there is an issue fetching the
  117. * information.
  118. */
  119. public static function getVersions($module = null) {
  120. $req = new GetVersionsRequest();
  121. $resp = new GetVersionsResponse();
  122. if ($module !== null) {
  123. if (!is_string($module)) {
  124. throw new \InvalidArgumentException(
  125. '$module must be a string. Actual type: ' . gettype($module));
  126. }
  127. $req->setModule($module);
  128. }
  129. try {
  130. ApiProxy::makeSyncCall('modules', 'GetVersions', $req, $resp);
  131. } catch (ApplicationError $e) {
  132. throw errorCodeToException($e->getApplicationError());
  133. }
  134. return $resp->getVersionList();
  135. }
  136. /**
  137. * Get the default version of a module.
  138. *
  139. * @param string $module The name of the module to retrieve the default
  140. * versions for. If null then the default versions for the current module
  141. * will be retrieved.
  142. *
  143. * @return string The default version of the module.
  144. *
  145. * @throws \InvalidArgumentException If $module is not a string.
  146. * @throws ModulesException If the given $module is invalid or if no default
  147. * version could be found.
  148. */
  149. public static function getDefaultVersion($module = null) {
  150. $req = new GetDefaultVersionRequest();
  151. $resp = new GetDefaultVersionResponse();
  152. if ($module !== null) {
  153. if (!is_string($module)) {
  154. throw new \InvalidArgumentException(
  155. '$module must be a string. Actual type: ' . gettype($module));
  156. }
  157. $req->setModule($module);
  158. }
  159. try {
  160. ApiProxy::makeSyncCall('modules', 'GetDefaultVersion', $req, $resp);
  161. } catch (ApplicationError $e) {
  162. throw errorCodeToException($e->getApplicationError());
  163. }
  164. return $resp->getVersion();
  165. }
  166. /**
  167. * Get the number of instances set for a version of a module.
  168. *
  169. * This function does not work on automatically-scaled modules.
  170. *
  171. * @param string $module The name of the module to retrieve the count for. If
  172. * null then the count for the current module will be retrieved.
  173. *
  174. * @param string $version The version of the module to retrieve the count for.
  175. * If null then the count for the version of the current instance will be
  176. * retrieved.
  177. *
  178. * @return integer The number of instances set for the current module
  179. * version.
  180. *
  181. * @throws \InvalidArgumentException If $module or $version is not a string.
  182. * @throws ModulesException if the given combination of $module and $version
  183. * is invalid.
  184. */
  185. public static function getNumInstances($module = null, $version = null) {
  186. $req = new GetNumInstancesRequest();
  187. $resp = new GetNumInstancesResponse();
  188. if ($module !== null) {
  189. if (!is_string($module)) {
  190. throw new \InvalidArgumentException(
  191. '$module must be a string. Actual type: ' . gettype($module));
  192. }
  193. $req->setModule($module);
  194. }
  195. if ($version !== null) {
  196. if (!is_string($version)) {
  197. throw new \InvalidArgumentException(
  198. '$version must be a string. Actual type: ' . gettype($version));
  199. }
  200. $req->setVersion($version);
  201. }
  202. try {
  203. ApiProxy::makeSyncCall('modules', 'GetNumInstances', $req, $resp);
  204. } catch (ApplicationError $e) {
  205. throw self::errorCodeToException($e->getApplicationError());
  206. }
  207. return (int) $resp->getInstances();
  208. }
  209. /**
  210. * Set the number of instances for a version of a module.
  211. *
  212. * This function does not work on automatically-scaled modules.
  213. *
  214. * @param string $module The name of the module to set the instance count for.
  215. * If null then the instance count for the current module will be set.
  216. *
  217. * @param string $version The version of the module to set the instance count
  218. * for. If null then the count for the version of the current instance will
  219. * be set.
  220. *
  221. * @throws \InvalidArgumentException If $instances is not an integer or if
  222. * $module or $version is not a string.
  223. * @throws ModulesException if the given combination of $module and $version
  224. * is invalid.
  225. * @throws TransientModulesException if there is an issue setting the
  226. * instance count.
  227. */
  228. public static function setNumInstances($instances,
  229. $module = null,
  230. $version = null) {
  231. $req = new SetNumInstancesRequest();
  232. $resp = new SetNumInstancesResponse();
  233. if (!is_int($instances)) {
  234. throw new \InvalidArgumentException(
  235. '$instances must be an integer. Actual type: ' . gettype($instances));
  236. }
  237. $req->setInstances($instances);
  238. if ($module !== null) {
  239. if (!is_string($module)) {
  240. throw new \InvalidArgumentException(
  241. '$module must be a string. Actual type: ' . gettype($module));
  242. }
  243. $req->setModule($module);
  244. }
  245. if ($version !== null) {
  246. if (!is_string($version)) {
  247. throw new \InvalidArgumentException(
  248. '$version must be a string. Actual type: ' . gettype($version));
  249. }
  250. $req->setVersion($version);
  251. }
  252. try {
  253. ApiProxy::makeSyncCall('modules', 'SetNumInstances', $req, $resp);
  254. } catch (ApplicationError $e) {
  255. throw self::errorCodeToException($e->getApplicationError());
  256. }
  257. }
  258. /**
  259. * Starts all instances of the given version of a module.
  260. * *
  261. * @param string $module The name of the module to start.
  262. *
  263. * @param string $version The version of the module to start.
  264. *
  265. * @throws \InvalidArgumentException If $module or $version is not a string.
  266. * @throws ModulesException if the given combination of $module and $version
  267. * is invalid.
  268. * @throws InvalidModuleStateException if the given $version is already
  269. * started or cannot be started.
  270. * @throws TransientModulesException if there is an issue starting the module
  271. * version.
  272. */
  273. public static function startVersion($module, $version) {
  274. $req = new StartModuleRequest();
  275. $resp = new StartModuleResponse();
  276. if (!is_string($module)) {
  277. throw new \InvalidArgumentException(
  278. '$module must be a string. Actual type: ' . gettype($module));
  279. }
  280. $req->setModule($module);
  281. if (!is_string($version)) {
  282. throw new \InvalidArgumentException(
  283. '$version must be a string. Actual type: ' . gettype($version));
  284. }
  285. $req->setVersion($version);
  286. try {
  287. ApiProxy::makeSyncCall('modules', 'StartModule', $req, $resp);
  288. } catch (ApplicationError $e) {
  289. throw self::errorCodeToException($e->getApplicationError());
  290. }
  291. }
  292. /**
  293. * Stops all instances of the given version of a module.
  294. * *
  295. * @param string $module The name of the module to stop. If null then the
  296. * current module will be stopped.
  297. *
  298. * @param string $version The version of the module to stop. If null then the
  299. * current version will be stopped.
  300. *
  301. * @throws \InvalidArgumentException If $module or $version is not a string.
  302. * @throws ModulesException if the given combination of $module and $version
  303. * instance is invalid.
  304. * @throws InvalidModuleStateException if the given $version is already
  305. * stopped or cannot be stopped.
  306. * @throws TransientModulesException if there is an issue stopping the module
  307. * version.
  308. */
  309. public static function stopVersion($module = null, $version = null) {
  310. $req = new StopModuleRequest();
  311. $resp = new StopModuleResponse();
  312. if ($module !== null) {
  313. if (!is_string($module)) {
  314. throw new \InvalidArgumentException(
  315. '$module must be a string. Actual type: ' . gettype($module));
  316. }
  317. $req->setModule($module);
  318. }
  319. if ($version !== null) {
  320. if (!is_string($version)) {
  321. throw new \InvalidArgumentException(
  322. '$version must be a string. Actual type: ' . gettype($version));
  323. }
  324. $req->setVersion($version);
  325. }
  326. try {
  327. ApiProxy::makeSyncCall('modules', 'StopModule', $req, $resp);
  328. } catch (ApplicationError $e) {
  329. throw self::errorCodeToException($e->getApplicationError());
  330. }
  331. }
  332. /**
  333. * Returns the hostname to use when contacting a module.
  334. * *
  335. * @param string $module The name of the module whose hostname should be
  336. * returned. If null then the hostname of the current module will be returned.
  337. *
  338. * @param string $version The version of the module whose hostname should be
  339. * returned. If null then the hostname for the version of the current
  340. * instance will be returned.
  341. *
  342. * @param string $instance The instance whose hostname should be returned. If
  343. * null then the load balanced hostname for the module will be returned. If
  344. * the module is not a fixed module then the instance parameter is ignored.
  345. *
  346. * @return string The valid canonical hostname that can be used to communicate
  347. * with the given module/version/instance e.g.
  348. * "0.version1.module5.myapp.appspot.com".
  349. * @throws \InvalidArgumentException If $module or $version is not a string
  350. * or if $instance is not a string or integer.
  351. * @throws ModulesException if the given combination of $module and $instance
  352. * is invalid.
  353. */
  354. public static function getHostname($module = null,
  355. $version = null,
  356. $instance = null) {
  357. $req = new GetHostnameRequest();
  358. $resp = new GetHostnameResponse();
  359. if ($module !== null) {
  360. if (!is_string($module)) {
  361. throw new \InvalidArgumentException(
  362. '$module must be a string. Actual type: ' . gettype($module));
  363. }
  364. $req->setModule($module);
  365. }
  366. if ($version !== null) {
  367. if (!is_string($version)) {
  368. throw new \InvalidArgumentException(
  369. '$version must be a string. Actual type: ' . gettype($version));
  370. }
  371. $req->setVersion($version);
  372. }
  373. if ($instance !== null) {
  374. if (!is_int($instance) && !is_string($instance)) {
  375. throw new \InvalidArgumentException(
  376. '$instance must be an integer or string. Actual type: ' .
  377. gettype($instance));
  378. }
  379. $req->setInstance((string) $instance);
  380. }
  381. try {
  382. ApiProxy::makeSyncCall('modules', 'GetHostname', $req, $resp);
  383. } catch (ApplicationError $e) {
  384. throw self::errorCodeToException($e->getApplicationError());
  385. }
  386. return $resp->getHostname();
  387. }
  388. }