PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/backwpup/sdk/WindowsAzure/ServiceRuntime/RoleEnvironment.php

https://bitbucket.org/cesarmedrano/cesarmedrano
PHP | 964 lines | 452 code | 118 blank | 394 comment | 52 complexity | eebbb66f483e724f7775ee5c1875699a MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package WindowsAzure\ServiceRuntime
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\ServiceRuntime;
  24. use WindowsAzure\Common\Internal\Resources;
  25. use WindowsAzure\ServiceRuntime\Internal\RuntimeKernel;
  26. use WindowsAzure\ServiceRuntime\Internal\RoleEnvironmentNotAvailableException;
  27. use WindowsAzure\ServiceRuntime\Internal\ChannelNotAvailableException;
  28. use WindowsAzure\ServiceRuntime\Internal\CurrentStatus;
  29. use WindowsAzure\ServiceRuntime\Internal\AcquireCurrentState;
  30. use WindowsAzure\ServiceRuntime\Internal\ReleaseCurrentState;
  31. use WindowsAzure\ServiceRuntime\Internal\RoleInstanceStatus;
  32. use WindowsAzure\ServiceRuntime\Internal\RoleEnvironmentConfigurationSettingChange;
  33. use WindowsAzure\ServiceRuntime\Internal\RoleEnvironmentTopologyChange;
  34. /**
  35. * Represents the Windows Azure environment in which an instance of a role is
  36. * running.
  37. *
  38. * @category Microsoft
  39. * @package WindowsAzure\ServiceRuntime
  40. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  41. * @copyright 2012 Microsoft Corporation
  42. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  43. * @version Release: @package_version@
  44. * @link https://github.com/windowsazure/azure-sdk-for-php
  45. */
  46. class RoleEnvironment
  47. {
  48. /**
  49. * Specifies the environment variable that contains the path to the endpoint.
  50. *
  51. * @var string
  52. */
  53. const VERSION_ENDPOINT_ENVIRONMENT_NAME = 'WaRuntimeEndpoint';
  54. /**
  55. * Specifies the endpoint fixed path.
  56. *
  57. * @var string
  58. */
  59. const VERSION_ENDPOINT_FIXED_PATH = '\\\\.\\pipe\\WindowsAzureRuntime';
  60. /**
  61. * @var string
  62. */
  63. private static $_clientId;
  64. /**
  65. * @var \DateTime
  66. */
  67. private static $_maxDateTime;
  68. /**
  69. * @var IRuntimeClient
  70. */
  71. private static $_runtimeClient;
  72. /**
  73. * @var GoalState
  74. */
  75. private static $_currentGoalState;
  76. /**
  77. * @var RoleEnvironmentData
  78. */
  79. private static $_currentEnvironmentData;
  80. /**
  81. * @var array
  82. */
  83. private static $_changingListeners;
  84. /**
  85. * @var array
  86. */
  87. private static $_changedListeners;
  88. /**
  89. * @var array
  90. */
  91. private static $_stoppingListeners;
  92. /**
  93. * @var CurrentState
  94. */
  95. private static $_lastState;
  96. /**
  97. * @var string
  98. */
  99. private static $_versionEndpoint;
  100. /**
  101. * @var mix
  102. */
  103. private static $_tracking;
  104. /**
  105. * Initializes the role environment.
  106. *
  107. * @static
  108. *
  109. * @return none
  110. */
  111. public static function init()
  112. {
  113. self::$_clientId = uniqid();
  114. // 2038-01-19 04:14:07
  115. self::$_maxDateTime = new \DateTime(
  116. date(Resources::TIMESTAMP_FORMAT, PHP_INT_MAX)
  117. );
  118. self::$_tracking = true;
  119. }
  120. /**
  121. * Returns the client identifier.
  122. *
  123. * @static
  124. *
  125. * @return string
  126. */
  127. public static function getClientId()
  128. {
  129. return self::$_clientId;
  130. }
  131. /**
  132. * Initializes the runtime client.
  133. *
  134. * @param bool $keepOpen Boolean value indicating if the connection
  135. * should remain open.
  136. *
  137. * @static
  138. *
  139. * @return none
  140. */
  141. private static function _initialize($keepOpen = false)
  142. {
  143. try {
  144. if (is_null(self::$_runtimeClient)) {
  145. self::$_versionEndpoint = getenv(
  146. self::VERSION_ENDPOINT_ENVIRONMENT_NAME
  147. );
  148. if (self::$_versionEndpoint == false) {
  149. self::$_versionEndpoint = self::VERSION_ENDPOINT_FIXED_PATH;
  150. }
  151. $kernel = RuntimeKernel::getKernel();
  152. $kernel->getProtocol1RuntimeGoalStateClient()->setKeepOpen(
  153. $keepOpen
  154. );
  155. self::$_runtimeClient = $kernel->getRuntimeVersionManager()
  156. ->getRuntimeClient(self::$_versionEndpoint);
  157. self::$_currentGoalState = self::$_runtimeClient
  158. ->getCurrentGoalState();
  159. self::$_currentEnvironmentData = self::$_runtimeClient
  160. ->getRoleEnvironmentData();
  161. } else {
  162. self::$_currentGoalState = self::$_runtimeClient
  163. ->getCurrentGoalState();
  164. self::$_currentEnvironmentData = self::$_runtimeClient
  165. ->getRoleEnvironmentData();
  166. }
  167. } catch (ChannelNotAvailableException $ex) {
  168. throw new RoleEnvironmentNotAvailableException();
  169. }
  170. }
  171. /**
  172. * Tracks role environment changes raising events as necessary.
  173. *
  174. * This method is blocking and can/should be called in a separate fork.
  175. *
  176. * @static
  177. *
  178. * @return none
  179. */
  180. public static function trackChanges()
  181. {
  182. self::_initialize(true);
  183. while (self::$_tracking) {
  184. $newGoalState = self::$_runtimeClient->getCurrentGoalState();
  185. switch ($newGoalState->getExpectedState()) {
  186. case CurrentStatus::STARTED:
  187. $newIncarnation = $newGoalState->getIncarnation();
  188. $currentIncarnation = self::$_currentGoalState->getIncarnation();
  189. if ($newIncarnation > $currentIncarnation) {
  190. self::_processGoalStateChange($newGoalState);
  191. }
  192. break;
  193. case CurrentStatus::STOPPED:
  194. self::_raiseStoppingEvent();
  195. $stoppedState = new AcquireCurrentState(
  196. self::$_clientId,
  197. $newGoalState->getIncarnation(),
  198. CurrentStatus::STOPPED,
  199. self::$_maxDateTime
  200. );
  201. self::$_runtimeClient->setCurrentState($stoppedState);
  202. break;
  203. }
  204. if (is_int(self::$_tracking)) {
  205. self::$_tracking--;
  206. }
  207. }
  208. }
  209. /**
  210. * Processes a goal state change.
  211. *
  212. * @param GoalState $newGoalState The new goal state.
  213. *
  214. * @static
  215. *
  216. * @return none
  217. */
  218. private static function _processGoalStateChange($newGoalState)
  219. {
  220. $last = self::$_lastState;
  221. $changes = self::_calculateChanges();
  222. if (count($changes) == 0) {
  223. self::_acceptLatestIncarnation($newGoalState, $last);
  224. } else {
  225. self::_raiseChangingEvent($changes);
  226. self::_acceptLatestIncarnation($newGoalState, $last);
  227. self::$_currentEnvironmentData = self::$_runtimeClient
  228. ->getRoleEnvironmentData();
  229. self::_raiseChangedEvent($changes);
  230. }
  231. }
  232. /**
  233. * Accepts the latest incarnation.
  234. *
  235. * @param GoalState $newGoalState The new goal state.
  236. * @param CurrentState $last The last state.
  237. *
  238. * @static
  239. *
  240. * @return none
  241. */
  242. private static function _acceptLatestIncarnation($newGoalState, $last)
  243. {
  244. if (!is_null($last) && $last instanceof AcquireCurrentState) {
  245. $acquireState = $last;
  246. $acceptState = new AcquireCurrentState(
  247. self::$_clientId,
  248. $newGoalState->getIncarnation(),
  249. $acquireState->getStatus(),
  250. $acquireState->getExpiration()
  251. );
  252. self::$_runtimeClient->setCurrentState($acceptState);
  253. }
  254. self::$_currentGoalState = $newGoalState;
  255. }
  256. /**
  257. * Raises a stopping event.
  258. *
  259. * @static
  260. *
  261. * @return none
  262. */
  263. private static function _raiseStoppingEvent()
  264. {
  265. foreach (self::$_stoppingListeners as $callback) {
  266. call_user_func($callback);
  267. }
  268. }
  269. /**
  270. * Raises a changing event.
  271. *
  272. * @param array $changes The changes.
  273. *
  274. * @static
  275. *
  276. * @return none
  277. */
  278. private static function _raiseChangingEvent($changes)
  279. {
  280. foreach (self::$_changingListeners as $callback) {
  281. call_user_func($callback, $changes);
  282. }
  283. }
  284. /**
  285. * Raises a changed event.
  286. *
  287. * @param array $changes The changes.
  288. *
  289. * @static
  290. *
  291. * @return none
  292. */
  293. private static function _raiseChangedEvent($changes)
  294. {
  295. foreach (self::$_changedListeners as $callback) {
  296. call_user_func($callback, $changes);
  297. }
  298. }
  299. /**
  300. * Calculates changes.
  301. *
  302. * @static
  303. *
  304. * @return array
  305. */
  306. private static function _calculateChanges()
  307. {
  308. $current = self::$_currentEnvironmentData;
  309. $newData = self::$_runtimeClient->getRoleEnvironmentData();
  310. $changes = self::_calculateConfigurationChanges($current, $newData);
  311. $currentRoles = $current->getRoles();
  312. $newRoles = $newData->getRoles();
  313. $changedRoleSet = array();
  314. foreach ($currentRoles as $roleName => $role) {
  315. if (array_key_exists($roleName, $newRoles)) {
  316. $currentRole = $currentRoles[$roleName];
  317. $newRole = $newRoles[$roleName];
  318. $currentRoleInstances = $currentRole->getInstances();
  319. $newRoleInstances = $newRole->getInstances();
  320. $changedRoleSet = array_merge(
  321. $changedRoleSet,
  322. self::_calculateNewRoleInstanceChanges(
  323. $role,
  324. $currentRoleInstances,
  325. $newRoleInstances
  326. )
  327. );
  328. } else {
  329. $changedRoleSet[] = $role;
  330. }
  331. }
  332. foreach ($newRoles as $roleName => $role) {
  333. if (array_key_exists($roleName, $currentRoles)) {
  334. $currentRole = $currentRoles[$roleName];
  335. $newRole = $newRoles[$roleName];
  336. $currentRoleInstances = $currentRole->getInstances();
  337. $newRoleInstances = $newRole->getInstances();
  338. $changedRoleSet = array_merge(
  339. $changedRoleSet,
  340. self::_calculateCurrentRoleInstanceChanges(
  341. $role,
  342. $currentRoleInstances,
  343. $newRoleInstances
  344. )
  345. );
  346. } else {
  347. $changedRoleSet[] = $role;
  348. }
  349. }
  350. foreach ($changedRoleSet as $role) {
  351. $changes[] = new RoleEnvironmentTopologyChange($role);
  352. }
  353. return $changes;
  354. }
  355. /**
  356. * Calculates the configuration changes.
  357. *
  358. * @param RoleEnvironmentData $currentRoleEnvironment The current role
  359. * environment data.
  360. * @param RoleEnvionrmentData $newRoleEnvironment The new role
  361. * environment data.
  362. *
  363. * @static
  364. *
  365. * @return array
  366. */
  367. private static function _calculateConfigurationChanges(
  368. $currentRoleEnvironment, $newRoleEnvironment
  369. ) {
  370. $changes = array();
  371. $currentConfig = $currentRoleEnvironment->getConfigurationSettings();
  372. $newConfig = $newRoleEnvironment->getConfigurationSettings();
  373. foreach ($currentConfig as $settingKey => $setting) {
  374. if (array_key_exists($settingKey, $newConfig)) {
  375. if ($newConfig[$settingKey] != $currentConfig[$settingKey]) {
  376. $changes[] = new RoleEnvironmentConfigurationSettingChange(
  377. $settingKey
  378. );
  379. }
  380. } else {
  381. $changes[] = new RoleEnvironmentConfigurationSettingChange(
  382. $settingKey
  383. );
  384. }
  385. }
  386. foreach ($newConfig as $settingKey => $setting) {
  387. if (!array_key_exists($settingKey, $currentConfig)) {
  388. $changes[] = new RoleEnvironmentConfigurationSettingChange(
  389. $settingKey
  390. );
  391. }
  392. }
  393. return $changes;
  394. }
  395. /**
  396. * Calculates which instances / instance endpoints were added from the current
  397. * role to the new role.
  398. *
  399. * @param RoleInstance $role The current role.
  400. * @param array $currentRoleInstances The current role instances.
  401. * @param array $newRoleInstances The new role instances.
  402. *
  403. * @static
  404. *
  405. * @return array
  406. */
  407. private static function _calculateNewRoleInstanceChanges(
  408. $role, $currentRoleInstances, $newRoleInstances
  409. ) {
  410. $changedRoleSet = array();
  411. foreach ($currentRoleInstances as $instanceKey => $currentInstance) {
  412. if (array_key_exists($instanceKey, $newRoleInstances)) {
  413. $newInstance = $newRoleInstances[$instanceKey];
  414. $currentUpdateDomain = $currentInstance->getUpdateDomain();
  415. $newUpdateDomain = $newInstance->getUpdateDomain();
  416. $currentFaultDomain = $currentInstance->getFaultDomain();
  417. $newFaultDomain = $newInstance->getFaultDomain();
  418. if ($currentUpdateDomain == $newUpdateDomain
  419. && $currentFaultDomain == $newFaultDomain
  420. ) {
  421. $currentInstanceEndpoints = $currentInstance
  422. ->getInstanceEndpoints();
  423. $newInstanceEndpoints = $newInstance->getInstanceEndpoints();
  424. $changedRoleSet = array_merge(
  425. $changedRoleSet,
  426. self::_calculateNewRoleInstanceEndpointsChanges(
  427. $role,
  428. $currentInstanceEndpoints,
  429. $newInstanceEndpoints
  430. )
  431. );
  432. } else {
  433. $changedRoleSet[] = $role;
  434. }
  435. } else {
  436. $changedRoleSet[] = $role;
  437. }
  438. }
  439. return $changedRoleSet;
  440. }
  441. /**
  442. * Calculates which endpoints / endpoint were added from the current
  443. * role to the new role.
  444. *
  445. * @param RoleInstance $role The current role.
  446. * @param array $currentInstanceEndpoints The current instance endpoints.
  447. * @param array $newInstanceEndpoints The new instance endpoints.
  448. *
  449. * @static
  450. *
  451. * @return array
  452. */
  453. private static function _calculateNewRoleInstanceEndpointsChanges(
  454. $role, $currentInstanceEndpoints, $newInstanceEndpoints
  455. ) {
  456. $changedRoleSet = array();
  457. foreach ($currentInstanceEndpoints as $endpointKey => $currentEndpoint) {
  458. if (array_key_exists($endpointKey, $newInstanceEndpoints)) {
  459. $newEndpoint = $newInstanceEndpoints[$endpointKey];
  460. $currentProtocol = $currentEndpoint->getProtocol();
  461. $newProtocol = $newEndpoint->getProtocol();
  462. $currentAddress = $currentEndpoint->getAddress();
  463. $newAddress = $newEndpoint->getAddress();
  464. $currentPort = $currentEndpoint->getPort();
  465. $newPort = $newEndpoint->getPort();
  466. if ($currentProtocol != $newProtocol
  467. || $currentAddress != $newAddress
  468. || $currentPort != $newPort
  469. ) {
  470. $changedRoleSet[] = $role;
  471. }
  472. } else {
  473. $changedRoleSet[] = $role;
  474. }
  475. }
  476. return $changedRoleSet;
  477. }
  478. /**
  479. * Calculates which instances / instance endpoints were removed from the current
  480. * role to the new role.
  481. *
  482. * @param RoleInstance $role The current role.
  483. * @param array $currentRoleInstances The current role instances.
  484. * @param array $newRoleInstances The new role instances.
  485. *
  486. * @static
  487. *
  488. * @return array
  489. */
  490. private static function _calculateCurrentRoleInstanceChanges(
  491. $role, $currentRoleInstances, $newRoleInstances
  492. ) {
  493. $changedRoleSet = array();
  494. foreach ($newRoleInstances as $instanceKey => $newInstance) {
  495. if (array_key_exists($instanceKey, $currentRoleInstances)) {
  496. $currentInstance = $currentRoleInstances[$instanceKey];
  497. $currentUpdateDomain = $currentInstance->getUpdateDomain();
  498. $newUpdateDomain = $newInstance->getUpdateDomain();
  499. $currentFaultDomain = $currentInstance->getFaultDomain();
  500. $newFaultDomain = $newInstance->getFaultDomain();
  501. if ($currentUpdateDomain == $newUpdateDomain
  502. && $currentFaultDomain == $newFaultDomain
  503. ) {
  504. $newInstanceEndpoints = $newInstance
  505. ->getInstanceEndpoints();
  506. $currentInstanceEndpoints = $currentInstance
  507. ->getInstanceEndpoints();
  508. $changedRoleSet = array_merge(
  509. $changedRoleSet,
  510. self::_calculateCurrentRoleInstanceEndpointsChanges(
  511. $role,
  512. $currentInstanceEndpoints,
  513. $newInstanceEndpoints
  514. )
  515. );
  516. }
  517. // Intentionally not adding since if the values are different,
  518. // it should have already been added
  519. } else {
  520. $changedRoleSet[] = $role;
  521. }
  522. }
  523. return $changedRoleSet;
  524. }
  525. /**
  526. * Calculates which endpoints / endpoint were removed from the current
  527. * role to the new role.
  528. *
  529. * @param RoleInstance $role The current changed role set.
  530. * @param array $currentInstanceEndpoints The current instance endpoints.
  531. * @param array $newInstanceEndpoints The new instance endpoints.
  532. *
  533. * @static
  534. *
  535. * @return array
  536. */
  537. private static function _calculateCurrentRoleInstanceEndpointsChanges(
  538. $role, $currentInstanceEndpoints, $newInstanceEndpoints
  539. ) {
  540. $changedRoleSet = array();
  541. foreach ($newInstanceEndpoints as $endpointKey => $newEndpoint) {
  542. if (!array_key_exists(
  543. $endpointKey,
  544. $currentInstanceEndpoints
  545. )
  546. ) {
  547. $changedRoleSet[] = $role;
  548. }
  549. }
  550. return $changedRoleSet;
  551. }
  552. /**
  553. * Returns a RoleInstance object that represents the role instance
  554. * in which this code is currently executing.
  555. *
  556. * @static
  557. *
  558. * @return RoleInstance
  559. */
  560. public static function getCurrentRoleInstance()
  561. {
  562. self::_initialize();
  563. return self::$_currentEnvironmentData->getCurrentInstance();
  564. }
  565. /**
  566. * Returns the deployment ID that uniquely identifies the deployment in
  567. * which this role instance is running.
  568. *
  569. * @static
  570. *
  571. * @return string
  572. */
  573. public static function getDeploymentId()
  574. {
  575. self::_initialize();
  576. return self::$_currentEnvironmentData->getDeploymentId();
  577. }
  578. /**
  579. * Indicates whether the role instance is running in the Windows Azure
  580. * environment.
  581. *
  582. * @static
  583. *
  584. * @return boolean
  585. */
  586. public static function isAvailable()
  587. {
  588. try {
  589. self::_initialize();
  590. } catch (RoleEnvironmentNotAvailableException $ex) {
  591. return false;
  592. } catch (ChannelNotAvailableException $ex) {
  593. return false;
  594. }
  595. return self::$_runtimeClient != null;
  596. }
  597. /**
  598. * Indicates whether the role instance is running in the development fabric.
  599. *
  600. * @static
  601. *
  602. * @return boolean
  603. */
  604. public static function isEmulated()
  605. {
  606. self::_initialize();
  607. return self::$_currentEnvironmentData->isEmulated();
  608. }
  609. /**
  610. * Returns the set of Role objects defined for your service.
  611. *
  612. * Roles are defined in the service definition file.
  613. *
  614. * @static
  615. *
  616. * @return array
  617. */
  618. public static function getRoles()
  619. {
  620. self::_initialize();
  621. return self::$_currentEnvironmentData->getRoles();
  622. }
  623. /**
  624. * Retrieves the settings in the service configuration file.
  625. *
  626. * A role's configuration settings are defined in the service definition
  627. * file. Values for configuration settings are set in the service
  628. * configuration file.
  629. *
  630. * @static
  631. *
  632. * @return array
  633. */
  634. public static function getConfigurationSettings()
  635. {
  636. self::_initialize();
  637. return self::$_currentEnvironmentData->getConfigurationSettings();
  638. }
  639. /**
  640. * Retrieves the set of named local storage resources.
  641. *
  642. * @static
  643. *
  644. * @return array
  645. */
  646. public static function getLocalResources()
  647. {
  648. self::_initialize();
  649. return self::$_currentEnvironmentData->getLocalResources();
  650. }
  651. /**
  652. * Requests that the current role instance be stopped and restarted.
  653. * Before the role instance is recycled, the Windows Azure load balancer
  654. * takes the role instance out of rotation.
  655. *
  656. * This ensures that no new requests are routed to the instance while it
  657. * is restarting.
  658. *
  659. * @static
  660. *
  661. * @return none
  662. */
  663. public static function requestRecycle()
  664. {
  665. self::_initialize();
  666. $recycleState = new AcquireCurrentState(
  667. self::$_clientId,
  668. self::$_currentGoalState->getIncarnation(),
  669. CurrentStatus::RECYCLE,
  670. self::$_maxDateTime
  671. );
  672. self::$_runtimeClient->setCurrentState($recycleState);
  673. }
  674. /**
  675. * Sets the status of the role instance.
  676. *
  677. * An instance may indicate that it is in one of two states: Ready or Busy.
  678. * If an instance's state is Ready, it is prepared to receive requests from
  679. * the load balancer. If the instance's state is Busy, it will not receive
  680. * requests from the load balancer.
  681. *
  682. * @param RoleInstanceStatus $status The new role status.
  683. * @param \DateTime $expirationUtc The expiration UTC time.
  684. *
  685. * @static
  686. *
  687. * @return none
  688. */
  689. public static function setStatus($status, $expirationUtc)
  690. {
  691. self::_initialize();
  692. $currentStatus = CurrentStatus::STARTED;
  693. switch ($status) {
  694. case RoleInstanceStatus::BUSY:
  695. $currentStatus = CurrentStatus::BUSY;
  696. break;
  697. case RoleInstanceStatus::READY:
  698. $currentStatus = CurrentStatus::STARTED;
  699. break;
  700. }
  701. $newState = new AcquireCurrentState(
  702. self::$_clientId,
  703. self::$_currentGoalState->getIncarnation(),
  704. $currentStatus,
  705. $expirationUtc
  706. );
  707. self::$_lastState = $newState;
  708. self::$_runtimeClient->setCurrentState($newState);
  709. }
  710. /**
  711. * Clears the status of the role instance.
  712. *
  713. * An instance may indicate that it has completed communicating status by
  714. * calling this method.
  715. *
  716. * @static
  717. *
  718. * @return none
  719. */
  720. public static function clearStatus()
  721. {
  722. self::_initialize();
  723. $newState = new ReleaseCurrentState(self::$_clientId);
  724. self::$_lastState = $newState;
  725. self::$_runtimeClient->setCurrentState($newState);
  726. }
  727. /**
  728. * Adds an event listener for the changed event, which occurs
  729. * after a configuration change has been applied to a role instance.
  730. *
  731. * To listen for events, one should call trackChanges.
  732. *
  733. * @param function $listener The changed listener.
  734. *
  735. * @return none
  736. */
  737. public static function addRoleEnvironmentChangedListener($listener)
  738. {
  739. self::$_changedListeners[] = $listener;
  740. }
  741. /**
  742. * Removes an event listener for the Changed event.
  743. *
  744. * @param function $listener The changed listener.
  745. *
  746. * @static
  747. *
  748. * @return bool
  749. */
  750. public static function removeRoleEnvironmentChangedListener($listener)
  751. {
  752. foreach (self::$_changedListeners as $key => $changedListener) {
  753. if ($changedListener == $listener) {
  754. unset(self::$_changedListeners[$key]);
  755. return true;
  756. }
  757. }
  758. return false;
  759. }
  760. /**
  761. * Adds an event listener for the Changing event, which occurs
  762. * before a change to the service configuration is applied to the running
  763. * instances of the role.
  764. *
  765. * Service configuration changes are applied on-the-fly to running role
  766. * instances. Configuration changes include changes to the service
  767. * configuration changes and changes to the number of instances in the
  768. * service.
  769. *
  770. * This event occurs after the new configuration file has been submitted to
  771. * Windows Azure but before the changes have been applied to each running
  772. * role instance. This event can be cancelled for a given instance to
  773. * prevent the configuration change.
  774. *
  775. * Note that cancelling this event causes the instance to be automatically
  776. * recycled. When the instance is recycled, the configuration change is
  777. * applied when it restarts.
  778. *
  779. * To listen for events, one should call trackChanges.
  780. *
  781. * @param function $listener The changing listener.
  782. *
  783. * @static
  784. *
  785. * @return none
  786. */
  787. public static function addRoleEnvironmentChangingListener($listener)
  788. {
  789. self::$_changingListeners[] = $listener;
  790. }
  791. /**
  792. * Removes an event listener for the Changing event.
  793. *
  794. * @param function $listener The changing listener.
  795. *
  796. * @static
  797. *
  798. * @return bool
  799. */
  800. public static function removeRoleEnvironmentChangingListener($listener)
  801. {
  802. foreach (self::$_changingListeners as $key => $changingListener) {
  803. if ($changingListener == $listener) {
  804. unset(self::$_changingListeners[$key]);
  805. return true;
  806. }
  807. }
  808. return false;
  809. }
  810. /**
  811. * Adds an event listener for the Stopping event, which occurs
  812. * when the role is stopping.
  813. * To listen for events, one should call trackChanges.
  814. *
  815. * @param function $listener The stopping listener.
  816. *
  817. * @static
  818. *
  819. * @return none
  820. */
  821. public static function addRoleEnvironmentStoppingListener($listener)
  822. {
  823. self::$_stoppingListeners[] = $listener;
  824. }
  825. /**
  826. * Removes an event listener for the Stopping event.
  827. *
  828. * @param function $listener The stopping listener.
  829. *
  830. * @static
  831. *
  832. * @return bool
  833. */
  834. public static function removeRoleEnvironmentStoppingListener($listener)
  835. {
  836. foreach (self::$_stoppingListeners as $key => $stoppingListener) {
  837. if ($stoppingListener == $listener) {
  838. unset(self::$_stoppingListeners[$key]);
  839. return true;
  840. }
  841. }
  842. return false;
  843. }
  844. }
  845. // Initialize static fields
  846. RoleEnvironment::init();