rcjob.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #ifndef RCJOB_H
  9. #define RCJOB_H
  10. #if !defined(Q_MOC_RUN)
  11. #include <QObject>
  12. #include <QString>
  13. #include <QDateTime>
  14. #include <QStringList>
  15. #include <AzCore/base.h>
  16. #include "RCCommon.h"
  17. #include "native/utilities/PlatformConfiguration.h"
  18. #include <AzCore/Math/Uuid.h>
  19. #include <AssetBuilderSDK/AssetBuilderSDK.h>
  20. #include "native/assetprocessor.h"
  21. #include <AzToolsFramework/AssetDatabase/AssetDatabaseConnection.h>
  22. #include <QFileInfoList>
  23. #endif
  24. namespace AssetProcessor
  25. {
  26. struct AssetRecognizer;
  27. class RCJob;
  28. //! Interface for signalling when jobs start and stop
  29. //! Primarily intended for unit tests
  30. struct IRCJobSignal
  31. {
  32. AZ_RTTI(IRCJobSignal, "{F81AEDE6-C670-4F3D-8393-4E2FF8ADDD02}");
  33. virtual ~IRCJobSignal() = default;
  34. virtual void Started(){}
  35. virtual void Finished(){}
  36. };
  37. //! Scoped class to automatically signal job start and stop
  38. struct ScopedJobSignaler
  39. {
  40. ScopedJobSignaler()
  41. {
  42. auto signalReciever = AZ::Interface<IRCJobSignal>::Get();
  43. if (signalReciever)
  44. {
  45. signalReciever->Started();
  46. }
  47. }
  48. ~ScopedJobSignaler()
  49. {
  50. auto signalReciever = AZ::Interface<IRCJobSignal>::Get();
  51. if (signalReciever)
  52. {
  53. signalReciever->Finished();
  54. }
  55. }
  56. };
  57. //! Params Base class
  58. struct Params
  59. {
  60. Params(AssetProcessor::RCJob* job = nullptr)
  61. : m_rcJob(job)
  62. {}
  63. virtual ~Params() = default;
  64. AssetProcessor::RCJob* m_rcJob;
  65. AZ::IO::Path m_cacheOutputDir;
  66. AZ::IO::Path m_intermediateOutputDir;
  67. AZ::IO::Path m_relativePath;
  68. // UUID of the original source asset.
  69. // If this job is for an intermediate asset, the UUID is for the direct source which produced the intermediate.
  70. // If the original source asset is not using metadata files, this value will be empty.
  71. AZ::Uuid m_sourceUuid;
  72. Params(const Params&) = default;
  73. virtual bool IsValidParams() const;
  74. };
  75. //! RCParams contains info that is required by the rc
  76. struct RCParams
  77. : public Params
  78. {
  79. QString m_rootDir;
  80. QString m_rcExe;
  81. QString m_inputFile;
  82. QString m_platformIdentifier;
  83. QString m_params;
  84. RCParams(AssetProcessor::RCJob* job = nullptr)
  85. : Params(job)
  86. {}
  87. RCParams(const RCParams&) = default;
  88. bool IsValidParams() const override;
  89. };
  90. //! BuilderParams contains info that is required by the builders
  91. struct BuilderParams
  92. : public Params
  93. {
  94. AssetBuilderSDK::ProcessJobRequest m_processJobRequest;
  95. AssetBuilderSDK::AssetBuilderDesc m_assetBuilderDesc;
  96. QString m_serverKey;
  97. BuilderParams(AssetProcessor::RCJob* job = nullptr)
  98. : Params(job)
  99. {}
  100. BuilderParams(const BuilderParams&) = default;
  101. AZStd::string GetTempJobDirectory() const;
  102. QString GetServerKey() const;
  103. };
  104. //! JobOutputInfo is used to store job related messages.
  105. //! Messages can be an error or just some information.
  106. struct JobOutputInfo
  107. {
  108. QString m_windowName; // window name is used to specify whether it is an error or not
  109. QString m_message;// the actual message
  110. JobOutputInfo() = default;
  111. JobOutputInfo(QString window, QString message)
  112. : m_windowName(window)
  113. , m_message(message)
  114. {
  115. }
  116. };
  117. /**
  118. * The RCJob class contains all the necessary information about a single RC job
  119. */
  120. class RCJob
  121. : public QObject
  122. {
  123. Q_OBJECT
  124. public:
  125. enum JobState
  126. {
  127. pending,
  128. processing,
  129. completed,
  130. crashed,
  131. terminated,
  132. cancelled,
  133. failed,
  134. };
  135. explicit RCJob(QObject* parent = 0);
  136. virtual ~RCJob();
  137. void Init(JobDetails& details);
  138. QString Params() const;
  139. QString CommandLine() const;
  140. QDateTime GetTimeCreated() const;
  141. void SetTimeCreated(const QDateTime& timeCreated);
  142. QDateTime GetTimeLaunched() const;
  143. void SetTimeLaunched(const QDateTime& timeLaunched);
  144. QDateTime GetTimeCompleted() const;
  145. void SetTimeCompleted(const QDateTime& timeCompleted);
  146. void SetOriginalFingerprint(AZ::u32 originalFingerprint);
  147. AZ::u32 GetOriginalFingerprint() const;
  148. QString GetConsoleOutput() const;
  149. void SetConsoleOutput(QString rcOut);
  150. JobState GetState() const;
  151. void SetState(const JobState& state);
  152. const AZ::Uuid& GetInputFileUuid() const;
  153. QString GetDestination() const;
  154. //! the final output path is where the actual outputs are copied when processing succeeds
  155. //! this will be in the asset cache, in the gamename / platform / gamename folder.
  156. AZ::IO::Path GetCacheOutputPath() const;
  157. AZ::IO::Path GetIntermediateOutputPath() const;
  158. AZ::IO::Path GetRelativePath() const;
  159. const AssetProcessor::AssetRecognizer* GetRecognizer() const;
  160. void SetRecognizer(const AssetProcessor::AssetRecognizer* value);
  161. const AssetBuilderSDK::PlatformInfo& GetPlatformInfo() const;
  162. // intentionally non-const to move.
  163. AssetBuilderSDK::ProcessJobResponse& GetProcessJobResponse();
  164. const JobEntry& GetJobEntry() const;
  165. bool HasMissingSourceDependency() const;
  166. void Start();
  167. const QueueElementID& GetElementID() const { return m_queueElementID; }
  168. const int JobEscalation() { return m_JobEscalation; }
  169. void SetJobEscalation(int jobEscalation);
  170. void SetCheckExclusiveLock(bool value);
  171. Q_SIGNALS:
  172. //! This signal will be emitted when we make sure that no other application has a lock on the source file
  173. //! and also that the fingerprint of the source file is stable and not changing.
  174. //! This will basically indicate that we are starting to perform work on the current job
  175. void BeginWork();
  176. void Finished();
  177. void JobFinished(AssetBuilderSDK::ProcessJobResponse result);
  178. public:
  179. static QString GetStateDescription(const JobState& state);
  180. static void ExecuteBuilderCommand(BuilderParams builderParams);
  181. static void AutoFailJob(BuilderParams& builderParams);
  182. static bool CopyCompiledAssets(BuilderParams& params, AssetBuilderSDK::ProcessJobResponse& response);
  183. static bool VerifyOutputProduct(
  184. QDir outputDirectory,
  185. QString outputFilename,
  186. QString absolutePathOfSource,
  187. qint64& totalFileSizeRequired,
  188. QList<QPair<QString, QString>>& outputsToCopy);
  189. //! This method will save the processJobResponse and the job log to the temp directory as xml files.
  190. //! We will be modifying absolute paths in processJobResponse before saving it to the disk.
  191. static AZ::Outcome<AZStd::vector<AZStd::string>> BeforeStoringJobResult(const BuilderParams& builderParams, AssetBuilderSDK::ProcessJobResponse jobResponse);
  192. //! This method will retrieve the processJobResponse and the job log from the temp directory.
  193. //! This method is also responsible for emitting the server job logs to the local job log file.
  194. static bool AfterRetrievingJobResult(const BuilderParams& builderParams, AssetUtilities::JobLogTraceListener& jobLogTraceListener, AssetBuilderSDK::ProcessJobResponse& jobResponse);
  195. QString GetJobKey() const;
  196. AZ::Uuid GetBuilderGuid() const;
  197. bool IsCritical() const;
  198. bool IsAutoFail() const;
  199. int GetPriority() const;
  200. const AZStd::vector<JobDependencyInternal>& GetJobDependencies();
  201. protected:
  202. //! DoWork ensure that the job is ready for being processing and than makes the actual builder call
  203. virtual void DoWork(AssetBuilderSDK::ProcessJobResponse& result, BuilderParams& builderParams, AssetUtilities::QuitListener& listener);
  204. void PopulateProcessJobRequest(AssetBuilderSDK::ProcessJobRequest& processJobRequest);
  205. private:
  206. JobDetails m_jobDetails;
  207. JobState m_jobState;
  208. QueueElementID m_queueElementID; // cached to prevent lots of construction of this all over the place
  209. int m_JobEscalation = AssetProcessor::JobEscalation::Default; // Escalation indicates how important the job is and how soon it needs processing, the greater the number the greater the escalation
  210. QDateTime m_timeCreated;
  211. QDateTime m_timeLaunched;
  212. QDateTime m_timeCompleted;
  213. unsigned int m_exitCode = 0;
  214. AzToolsFramework::AssetDatabase::ProductDatabaseEntryContainer m_products;
  215. AssetBuilderSDK::ProcessJobResponse m_processJobResponse;
  216. AZ::u32 m_scanFolderID;
  217. };
  218. } // namespace AssetProcessor
  219. Q_DECLARE_METATYPE(AssetProcessor::BuilderParams);
  220. Q_DECLARE_METATYPE(AssetProcessor::JobOutputInfo);
  221. Q_DECLARE_METATYPE(AssetProcessor::RCParams);
  222. #endif // RCJOB_H