BuilderManager.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/std/string/string.h>
  10. #include <AzCore/std/parallel/binary_semaphore.h>
  11. #include <AzCore/std/smart_ptr/shared_ptr.h>
  12. #include <native/utilities/assetUtils.h>
  13. #include <QDir> // used in the inl file.
  14. #include <utilities/Builder.h>
  15. #include <utilities/BuilderList.h>
  16. class ConnectionManager;
  17. namespace AssetProcessor
  18. {
  19. struct BuilderRef;
  20. //! Indicates if job request files should be created on success. Can be useful for debugging
  21. static const bool s_createRequestFileForSuccessfulJob = false;
  22. //! This EBUS is used to request a free builder from the builder manager pool
  23. class BuilderManagerBusTraits
  24. : public AZ::EBusTraits
  25. {
  26. public:
  27. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
  28. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  29. using MutexType = AZStd::recursive_mutex;
  30. virtual ~BuilderManagerBusTraits() = default;
  31. //! Returns a builder for doing work
  32. virtual BuilderRef GetBuilder(BuilderPurpose purpose) = 0;
  33. virtual void AddAssetToBuilderProcessedList(const AZ::Uuid& /*builderId*/, const AZStd::string& /*sourceAsset*/)
  34. {
  35. }
  36. };
  37. using BuilderManagerBus = AZ::EBus<BuilderManagerBusTraits>;
  38. class BuilderDebugOutput
  39. {
  40. public:
  41. AZStd::list<AZStd::string> m_assetsProcessed;
  42. };
  43. //! Manages the builder pool
  44. class BuilderManager
  45. : public BuilderManagerBus::Handler
  46. {
  47. public:
  48. explicit BuilderManager(ConnectionManager* connectionManager);
  49. ~BuilderManager();
  50. // Disable copy
  51. AZ_DISABLE_COPY_MOVE(BuilderManager);
  52. void ConnectionLost(AZ::u32 connId);
  53. //BuilderManagerBus
  54. BuilderRef GetBuilder(BuilderPurpose purpose) override;
  55. void AddAssetToBuilderProcessedList(const AZ::Uuid& builderId, const AZStd::string& sourceAsset) override;
  56. protected:
  57. //! Makes a new builder, adds it to the pool, and returns a shared pointer to it
  58. virtual AZStd::shared_ptr<Builder> AddNewBuilder(BuilderPurpose purpose);
  59. //! Handles incoming builder connections
  60. void IncomingBuilderPing(AZ::u32 connId, AZ::u32 type, AZ::u32 serial, QByteArray payload, QString platform);
  61. void PumpIdleBuilders();
  62. void PrintDebugOutput();
  63. AZStd::mutex m_buildersMutex;
  64. //! List of builders. Must be locked before accessing
  65. BuilderList m_builderList;
  66. // Track debug output generated per builder.
  67. // This is done this way so that it can be output in order, to track down race conditions with asset builders.
  68. AZStd::unordered_map<AZ::Uuid, BuilderDebugOutput> m_builderDebugOutput;
  69. //! Indicates if we allow builders to connect that we haven't started up ourselves. Useful for debugging
  70. bool m_allowUnmanagedBuilderConnections = false;
  71. //! Responsible for going through all the idle builders and pumping their communicators so they don't stall
  72. AZStd::thread m_pollingThread;
  73. AssetUtilities::QuitListener m_quitListener;
  74. };
  75. } // namespace AssetProcessor
  76. #include "native/utilities/BuilderManager.inl"