The jenkins gearman plugin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComputerListenerImpl.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. *
  3. * Copyright 2014 Hewlett-Packard Development Company, L.P.
  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. package hudson.plugins.gearman;
  19. import hudson.Extension;
  20. import hudson.model.TaskListener;
  21. import hudson.model.Computer;
  22. import hudson.slaves.ComputerListener;
  23. import hudson.slaves.OfflineCause;
  24. import java.io.IOException;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. /**
  28. * Update gearman workers on node state and configuration changes
  29. */
  30. @Extension
  31. public class ComputerListenerImpl extends ComputerListener {
  32. private static final Logger logger = LoggerFactory
  33. .getLogger(Constants.PLUGIN_LOGGER_NAME);
  34. @Override
  35. public void onConfigurationChange() {
  36. // only fired on nodes configuration changes like a label or
  37. // name change. Not fired on state changes, like offline or online.
  38. logger.info("---- " + ComputerListenerImpl.class.getName() + ":"
  39. + " onConfigurationChange");
  40. // update functions only when gearman-plugin is enabled
  41. if (!GearmanPluginConfig.get().enablePlugin()) {
  42. return;
  43. }
  44. // re-register gearman functions on node configuration changes,
  45. // specifically node label changes
  46. GearmanProxy.getInstance().registerJobs();
  47. // TODO: adjust for an update to executors. Method does not provide the
  48. // computer to know which thread to remove or add
  49. }
  50. @Override
  51. public void onOffline(Computer c) {
  52. // gets called when existing slave dis-connects
  53. // gets called when slave is deleted.
  54. logger.info("---- " + ComputerListenerImpl.class.getName() + ":"
  55. + " onOffline computer" + c);
  56. // update functions only when gearman-plugin is enabled
  57. if (!GearmanPluginConfig.get().enablePlugin()) {
  58. return;
  59. }
  60. // stop worker when jenkins slave is deleted or disconnected
  61. GearmanProxy.getInstance().stop(c);
  62. }
  63. @Override
  64. public void onOnline(Computer c, TaskListener listener) throws IOException,
  65. InterruptedException {
  66. // gets called when master goes into online state
  67. // gets called when existing slave re-connects
  68. // gets called when new slave goes into online state
  69. logger.info("---- " + ComputerListenerImpl.class.getName() + ":"
  70. + " onOnline computer " + c);
  71. // update functions only when gearman-plugin is enabled
  72. if (!GearmanPluginConfig.get().enablePlugin()) {
  73. return;
  74. }
  75. GearmanProxy gp = GearmanProxy.getInstance();
  76. /*
  77. * Spawn management executor worker if one doesn't exist yet.
  78. * This worker does not need any executors. It only needs
  79. * to work with gearman.
  80. */
  81. gp.createManagementWorker();
  82. // on creation of new slave
  83. gp.createExecutorWorkersOnNode(c);
  84. }
  85. @Override
  86. public void onTemporarilyOffline(Computer c, OfflineCause cause) {
  87. // fired when master or slave goes into temporary offline state
  88. logger.info("---- " + ComputerListenerImpl.class.getName() + ":"
  89. + " onTemporarilyOffline computer " + c);
  90. // update functions only when gearman-plugin is enabled
  91. if (!GearmanPluginConfig.get().enablePlugin()) {
  92. return;
  93. }
  94. // stop worker when jenkins slave is set to offline
  95. GearmanProxy.getInstance().stop(c);
  96. }
  97. @Override
  98. public void onTemporarilyOnline(Computer c) {
  99. // fired when master or slave goes into temporary online state
  100. logger.info("---- " + ComputerListenerImpl.class.getName() + ":"
  101. + " onTemporarilyOnline computer " + c);
  102. // update functions only when gearman-plugin is enabled
  103. if (!GearmanPluginConfig.get().enablePlugin()) {
  104. return;
  105. }
  106. GearmanProxy gp = GearmanProxy.getInstance();
  107. /*
  108. * Spawn management executor worker if one doesn't exist yet.
  109. * This worker does not need any executors. It only needs
  110. * to work with gearman.
  111. */
  112. gp.createManagementWorker();
  113. // on brining a slave back online
  114. gp.createExecutorWorkersOnNode(c);
  115. }
  116. }