rcjob.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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. #include "rcjob.h"
  9. #include <AzToolsFramework/UI/Logging/LogLine.h>
  10. #include <AzToolsFramework/Metadata/UuidUtils.h>
  11. #include <native/utilities/BuilderManager.h>
  12. #include <native/utilities/ThreadHelper.h>
  13. #include <QtConcurrent/QtConcurrentRun>
  14. #include <QElapsedTimer>
  15. #include "native/utilities/JobDiagnosticTracker.h"
  16. #include <qstorageinfo.h>
  17. #include <native/utilities/ProductOutputUtil.h>
  18. namespace
  19. {
  20. bool s_typesRegistered = false;
  21. // You have up to 60 minutes to finish processing an asset.
  22. // This was increased from 10 to account for PVRTC compression
  23. // taking up to an hour for large normal map textures, and should
  24. // be reduced again once we move to the ASTC compression format, or
  25. // find another solution to reduce processing times to be reasonable.
  26. const unsigned int g_jobMaximumWaitTime = 1000 * 60 * 60;
  27. const unsigned int g_sleepDurationForLockingAndFingerprintChecking = 100;
  28. const unsigned int g_timeoutInSecsForRetryingCopy = 30;
  29. const char* const s_tempString = "%TEMP%";
  30. const char* const s_jobLogFileName = "jobLog.xml";
  31. bool MoveCopyFile(QString sourceFile, QString productFile, bool isCopyJob = false)
  32. {
  33. if (!isCopyJob && (AssetUtilities::MoveFileWithTimeout(sourceFile, productFile, g_timeoutInSecsForRetryingCopy)))
  34. {
  35. //We do not want to rename the file if it is a copy job
  36. return true;
  37. }
  38. else if (AssetUtilities::CopyFileWithTimeout(sourceFile, productFile, g_timeoutInSecsForRetryingCopy))
  39. {
  40. // try to copy instead
  41. return true;
  42. }
  43. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Failed to move OR copy file from Source directory: %s to Destination Directory: %s", sourceFile.toUtf8().data(), productFile.toUtf8().data());
  44. return false;
  45. }
  46. }
  47. using namespace AssetProcessor;
  48. bool Params::IsValidParams() const
  49. {
  50. return !m_cacheOutputDir.empty() && !m_intermediateOutputDir.empty() && !m_relativePath.empty();
  51. }
  52. bool RCParams::IsValidParams() const
  53. {
  54. return (
  55. (!m_rcExe.isEmpty()) &&
  56. (!m_rootDir.isEmpty()) &&
  57. (!m_inputFile.isEmpty()) &&
  58. Params::IsValidParams()
  59. );
  60. }
  61. namespace AssetProcessor
  62. {
  63. RCJob::RCJob(QObject* parent)
  64. : QObject(parent)
  65. , m_timeCreated(QDateTime::currentDateTime())
  66. , m_scanFolderID(0)
  67. {
  68. m_jobState = RCJob::pending;
  69. if (!s_typesRegistered)
  70. {
  71. qRegisterMetaType<RCParams>("RCParams");
  72. qRegisterMetaType<BuilderParams>("BuilderParams");
  73. qRegisterMetaType<JobOutputInfo>("JobOutputInfo");
  74. s_typesRegistered = true;
  75. }
  76. }
  77. RCJob::~RCJob()
  78. {
  79. }
  80. void RCJob::Init(JobDetails& details)
  81. {
  82. m_jobDetails = AZStd::move(details);
  83. m_queueElementID = QueueElementID(GetJobEntry().m_sourceAssetReference, GetPlatformInfo().m_identifier.c_str(), GetJobKey());
  84. }
  85. const JobEntry& RCJob::GetJobEntry() const
  86. {
  87. return m_jobDetails.m_jobEntry;
  88. }
  89. bool RCJob::HasMissingSourceDependency() const
  90. {
  91. return m_jobDetails.m_hasMissingSourceDependency;
  92. }
  93. QDateTime RCJob::GetTimeCreated() const
  94. {
  95. return m_timeCreated;
  96. }
  97. void RCJob::SetTimeCreated(const QDateTime& timeCreated)
  98. {
  99. m_timeCreated = timeCreated;
  100. }
  101. QDateTime RCJob::GetTimeLaunched() const
  102. {
  103. return m_timeLaunched;
  104. }
  105. void RCJob::SetTimeLaunched(const QDateTime& timeLaunched)
  106. {
  107. m_timeLaunched = timeLaunched;
  108. }
  109. QDateTime RCJob::GetTimeCompleted() const
  110. {
  111. return m_timeCompleted;
  112. }
  113. void RCJob::SetTimeCompleted(const QDateTime& timeCompleted)
  114. {
  115. m_timeCompleted = timeCompleted;
  116. }
  117. AZ::u32 RCJob::GetOriginalFingerprint() const
  118. {
  119. return m_jobDetails.m_jobEntry.m_computedFingerprint;
  120. }
  121. void RCJob::SetOriginalFingerprint(unsigned int fingerprint)
  122. {
  123. m_jobDetails.m_jobEntry.m_computedFingerprint = fingerprint;
  124. }
  125. RCJob::JobState RCJob::GetState() const
  126. {
  127. return m_jobState;
  128. }
  129. void RCJob::SetState(const JobState& state)
  130. {
  131. bool wasPending = (m_jobState == pending);
  132. m_jobState = state;
  133. if ((wasPending)&&(m_jobState == cancelled))
  134. {
  135. // if we were pending (had not started yet) and we are now canceled, we still have to emit the finished signal
  136. // so that all the various systems waiting for us can do their housekeeping.
  137. Q_EMIT Finished();
  138. }
  139. }
  140. void RCJob::SetJobEscalation(int jobEscalation)
  141. {
  142. m_JobEscalation = jobEscalation;
  143. }
  144. void RCJob::SetCheckExclusiveLock(bool value)
  145. {
  146. m_jobDetails.m_jobEntry.m_checkExclusiveLock = value;
  147. }
  148. QString RCJob::GetStateDescription(const RCJob::JobState& state)
  149. {
  150. switch (state)
  151. {
  152. case RCJob::pending:
  153. return tr("Pending");
  154. case RCJob::processing:
  155. return tr("Processing");
  156. case RCJob::completed:
  157. return tr("Completed");
  158. case RCJob::crashed:
  159. return tr("Crashed");
  160. case RCJob::terminated:
  161. return tr("Terminated");
  162. case RCJob::failed:
  163. return tr("Failed");
  164. case RCJob::cancelled:
  165. return tr("Cancelled");
  166. }
  167. return QString();
  168. }
  169. const AZ::Uuid& RCJob::GetInputFileUuid() const
  170. {
  171. return m_jobDetails.m_jobEntry.m_sourceFileUUID;
  172. }
  173. AZ::IO::Path RCJob::GetCacheOutputPath() const
  174. {
  175. return m_jobDetails.m_cachePath;
  176. }
  177. AZ::IO::Path RCJob::GetIntermediateOutputPath() const
  178. {
  179. return m_jobDetails.m_intermediatePath;
  180. }
  181. AZ::IO::Path RCJob::GetRelativePath() const
  182. {
  183. return m_jobDetails.m_relativePath;
  184. }
  185. const AssetBuilderSDK::PlatformInfo& RCJob::GetPlatformInfo() const
  186. {
  187. return m_jobDetails.m_jobEntry.m_platformInfo;
  188. }
  189. AssetBuilderSDK::ProcessJobResponse& RCJob::GetProcessJobResponse()
  190. {
  191. return m_processJobResponse;
  192. }
  193. void RCJob::PopulateProcessJobRequest(AssetBuilderSDK::ProcessJobRequest& processJobRequest)
  194. {
  195. processJobRequest.m_jobDescription.m_critical = IsCritical();
  196. processJobRequest.m_jobDescription.m_additionalFingerprintInfo = m_jobDetails.m_extraInformationForFingerprinting;
  197. processJobRequest.m_jobDescription.m_jobKey = GetJobKey().toUtf8().data();
  198. processJobRequest.m_jobDescription.m_jobParameters = AZStd::move(m_jobDetails.m_jobParam);
  199. processJobRequest.m_jobDescription.SetPlatformIdentifier(GetPlatformInfo().m_identifier.c_str());
  200. processJobRequest.m_jobDescription.m_priority = GetPriority();
  201. processJobRequest.m_platformInfo = GetPlatformInfo();
  202. processJobRequest.m_builderGuid = GetBuilderGuid();
  203. processJobRequest.m_sourceFile = GetJobEntry().m_sourceAssetReference.RelativePath().c_str();
  204. processJobRequest.m_sourceFileUUID = GetInputFileUuid();
  205. processJobRequest.m_watchFolder = GetJobEntry().m_sourceAssetReference.ScanFolderPath().c_str();
  206. processJobRequest.m_fullPath = GetJobEntry().GetAbsoluteSourcePath().toUtf8().data();
  207. processJobRequest.m_jobId = GetJobEntry().m_jobRunKey;
  208. }
  209. QString RCJob::GetJobKey() const
  210. {
  211. return m_jobDetails.m_jobEntry.m_jobKey;
  212. }
  213. AZ::Uuid RCJob::GetBuilderGuid() const
  214. {
  215. return m_jobDetails.m_jobEntry.m_builderGuid;
  216. }
  217. bool RCJob::IsCritical() const
  218. {
  219. return m_jobDetails.m_critical;
  220. }
  221. bool RCJob::IsAutoFail() const
  222. {
  223. return m_jobDetails.m_autoFail;
  224. }
  225. int RCJob::GetPriority() const
  226. {
  227. return m_jobDetails.m_priority;
  228. }
  229. const AZStd::vector<AssetProcessor::JobDependencyInternal>& RCJob::GetJobDependencies()
  230. {
  231. return m_jobDetails.m_jobDependencyList;
  232. }
  233. void RCJob::Start()
  234. {
  235. // the following trace can be uncommented if there is a need to deeply inspect job running.
  236. //AZ_TracePrintf(AssetProcessor::DebugChannel, "JobTrace Start(%i %s,%s,%s)\n", this, GetInputFileAbsolutePath().toUtf8().data(), GetPlatform().toUtf8().data(), GetJobKey().toUtf8().data());
  237. AssetUtilities::QuitListener listener;
  238. listener.BusConnect();
  239. RCParams rc(this);
  240. BuilderParams builderParams(this);
  241. //Create the process job request
  242. AssetBuilderSDK::ProcessJobRequest processJobRequest;
  243. PopulateProcessJobRequest(processJobRequest);
  244. builderParams.m_processJobRequest = processJobRequest;
  245. builderParams.m_cacheOutputDir = GetCacheOutputPath();
  246. builderParams.m_intermediateOutputDir = GetIntermediateOutputPath();
  247. builderParams.m_relativePath = GetRelativePath();
  248. builderParams.m_assetBuilderDesc = m_jobDetails.m_assetBuilderDesc;
  249. builderParams.m_sourceUuid = m_jobDetails.m_sourceUuid;
  250. // when the job finishes, record the results and emit Finished()
  251. connect(this, &RCJob::JobFinished, this, [this](AssetBuilderSDK::ProcessJobResponse result)
  252. {
  253. m_processJobResponse = AZStd::move(result);
  254. switch (m_processJobResponse.m_resultCode)
  255. {
  256. case AssetBuilderSDK::ProcessJobResult_Crashed:
  257. {
  258. SetState(crashed);
  259. }
  260. break;
  261. case AssetBuilderSDK::ProcessJobResult_Success:
  262. {
  263. SetState(completed);
  264. }
  265. break;
  266. case AssetBuilderSDK::ProcessJobResult_Cancelled:
  267. {
  268. SetState(cancelled);
  269. }
  270. break;
  271. default:
  272. {
  273. SetState(failed);
  274. }
  275. break;
  276. }
  277. Q_EMIT Finished();
  278. });
  279. if (!listener.WasQuitRequested())
  280. {
  281. QtConcurrent::run(&RCJob::ExecuteBuilderCommand, builderParams);
  282. }
  283. else
  284. {
  285. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Job canceled due to quit being requested.");
  286. SetState(terminated);
  287. Q_EMIT Finished();
  288. }
  289. listener.BusDisconnect();
  290. }
  291. void RCJob::ExecuteBuilderCommand(BuilderParams builderParams)
  292. {
  293. // Note: this occurs inside a worker thread.
  294. // Signal start and end of the job
  295. ScopedJobSignaler signaler;
  296. // listen for the user quitting (CTRL-C or otherwise)
  297. AssetUtilities::QuitListener listener;
  298. listener.BusConnect();
  299. QElapsedTimer ticker;
  300. ticker.start();
  301. AssetBuilderSDK::ProcessJobResponse result;
  302. AssetBuilderSDK::JobCancelListener cancelListener(builderParams.m_processJobRequest.m_jobId);
  303. if (builderParams.m_rcJob->m_jobDetails.m_autoFail)
  304. {
  305. // if this is an auto-fail job, we should avoid doing any additional work besides the work required to fail the job and
  306. // write the details into its log. This is because Auto-fail jobs have 'incomplete' job descriptors, and only exist to
  307. // force a job to fail with a reasonable log file stating the reason for failure. An example of where it is useful to
  308. // use auto-fail jobs is when, after compilation was successful, something goes wrong integrating the result into the
  309. // cache. (For example, files collide, or the product file name would be too long). The job will have at that point
  310. // already completed, the thread long gone, so we can 'append' to the log in this manner post-build by creating a new
  311. // job that will automatically fail and ingest the old (success) log along with additional fail reasons and then fail.
  312. AutoFailJob(builderParams);
  313. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  314. Q_EMIT builderParams.m_rcJob->JobFinished(result);
  315. return;
  316. }
  317. // If requested, make sure we can open the file with exclusive permissions
  318. QString inputFile = builderParams.m_rcJob->GetJobEntry().GetAbsoluteSourcePath();
  319. if (builderParams.m_rcJob->GetJobEntry().m_checkExclusiveLock && QFile::exists(inputFile))
  320. {
  321. // We will only continue once we get exclusive lock on the source file
  322. while (!AssetUtilities::CheckCanLock(inputFile))
  323. {
  324. // Wait for a while before checking again, we need to let some time pass for the other process to finish whatever work it is doing
  325. QThread::msleep(g_sleepDurationForLockingAndFingerprintChecking);
  326. // If AP shutdown is requested, the job is canceled or we exceeded the max wait time, abort the loop and mark the job as canceled
  327. if (listener.WasQuitRequested() || cancelListener.IsCancelled() || (ticker.elapsed() > g_jobMaximumWaitTime))
  328. {
  329. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Cancelled;
  330. Q_EMIT builderParams.m_rcJob->JobFinished(result);
  331. return;
  332. }
  333. }
  334. }
  335. Q_EMIT builderParams.m_rcJob->BeginWork();
  336. // We will actually start working on the job after this point and even if RcController gets the same job again, we will put it in the queue for processing
  337. builderParams.m_rcJob->DoWork(result, builderParams, listener);
  338. Q_EMIT builderParams.m_rcJob->JobFinished(result);
  339. }
  340. void RCJob::AutoFailJob(BuilderParams& builderParams)
  341. {
  342. // force the fail data to be captured to the log file.
  343. // because this is being executed in a thread worker, this won't stomp the main thread's job id.
  344. AssetProcessor::SetThreadLocalJobId(builderParams.m_rcJob->GetJobEntry().m_jobRunKey);
  345. AssetUtilities::JobLogTraceListener jobLogTraceListener(builderParams.m_rcJob->m_jobDetails.m_jobEntry);
  346. #if defined(AZ_ENABLE_TRACING)
  347. QString sourceFullPath(builderParams.m_processJobRequest.m_fullPath.c_str());
  348. auto failReason = builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.find(AZ_CRC(AssetProcessor::AutoFailReasonKey));
  349. if (failReason != builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.end())
  350. {
  351. // you are allowed to have many lines in your fail reason.
  352. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "Failed processing %s", sourceFullPath.toUtf8().data());
  353. AZStd::vector<AZStd::string> delimited;
  354. AzFramework::StringFunc::Tokenize(failReason->second.c_str(), delimited, "\n");
  355. for (const AZStd::string& token : delimited)
  356. {
  357. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "%s", token.c_str());
  358. }
  359. }
  360. else
  361. {
  362. // since we didn't have a custom auto-fail reason, add a token to the log file that will help with
  363. // forensic debugging to differentiate auto-fails from regular fails (although it should also be
  364. // obvious from the output in other ways)
  365. AZ_TracePrintf("Debug", "(auto-failed)\n");
  366. }
  367. auto failLogFile = builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.find(AZ_CRC(AssetProcessor::AutoFailLogFile));
  368. if (failLogFile != builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.end())
  369. {
  370. AzToolsFramework::Logging::LogLine::ParseLog(failLogFile->second.c_str(), failLogFile->second.size(),
  371. [](AzToolsFramework::Logging::LogLine& target)
  372. {
  373. switch (target.GetLogType())
  374. {
  375. case AzToolsFramework::Logging::LogLine::TYPE_DEBUG:
  376. AZ_TracePrintf(target.GetLogWindow().c_str(), "%s", target.GetLogMessage().c_str());
  377. break;
  378. case AzToolsFramework::Logging::LogLine::TYPE_MESSAGE:
  379. AZ_TracePrintf(target.GetLogWindow().c_str(), "%s", target.GetLogMessage().c_str());
  380. break;
  381. case AzToolsFramework::Logging::LogLine::TYPE_WARNING:
  382. AZ_Warning(target.GetLogWindow().c_str(), false, "%s", target.GetLogMessage().c_str());
  383. break;
  384. case AzToolsFramework::Logging::LogLine::TYPE_ERROR:
  385. AZ_Error(target.GetLogWindow().c_str(), false, "%s", target.GetLogMessage().c_str());
  386. break;
  387. case AzToolsFramework::Logging::LogLine::TYPE_CONTEXT:
  388. AZ_TracePrintf(target.GetLogWindow().c_str(), " %s", target.GetLogMessage().c_str());
  389. break;
  390. }
  391. });
  392. }
  393. #endif
  394. // note that this line below is printed out to be consistent with the output from a job that normally failed, so
  395. // applications reading log file will find it.
  396. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "Builder indicated that the job has failed.\n");
  397. if (builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.find(AZ_CRC(AssetProcessor::AutoFailOmitFromDatabaseKey)) != builderParams.m_processJobRequest.m_jobDescription.m_jobParameters.end())
  398. {
  399. // we don't add Auto-fail jobs to the database if they have asked to be emitted.
  400. builderParams.m_rcJob->m_jobDetails.m_jobEntry.m_addToDatabase = false;
  401. }
  402. AssetProcessor::SetThreadLocalJobId(0);
  403. }
  404. void RCJob::DoWork(AssetBuilderSDK::ProcessJobResponse& result, BuilderParams& builderParams, AssetUtilities::QuitListener& listener)
  405. {
  406. // Setting job id for logging purposes
  407. AssetProcessor::SetThreadLocalJobId(builderParams.m_rcJob->GetJobEntry().m_jobRunKey);
  408. AssetUtilities::JobLogTraceListener jobLogTraceListener(builderParams.m_rcJob->m_jobDetails.m_jobEntry);
  409. {
  410. AssetBuilderSDK::JobCancelListener JobCancelListener(builderParams.m_rcJob->m_jobDetails.m_jobEntry.m_jobRunKey);
  411. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed; // failed by default
  412. #if defined(AZ_ENABLE_TRACING)
  413. for (const auto& warningMessage : builderParams.m_rcJob->m_jobDetails.m_warnings)
  414. {
  415. // you are allowed to have many lines in your warning message.
  416. AZStd::vector<AZStd::string> delimited;
  417. AzFramework::StringFunc::Tokenize(warningMessage.c_str(), delimited, "\n");
  418. for (const AZStd::string& token : delimited)
  419. {
  420. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "%s", token.c_str());
  421. }
  422. jobLogTraceListener.AddWarning();
  423. }
  424. #endif
  425. // create a temporary directory for Builder to work in.
  426. // lets make it as a subdir of a known temp dir
  427. QString workFolder;
  428. if (!AssetUtilities::CreateTempWorkspace(workFolder))
  429. {
  430. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "Could not create temporary directory for Builder!\n");
  431. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  432. Q_EMIT builderParams.m_rcJob->JobFinished(result);
  433. return;
  434. }
  435. builderParams.m_processJobRequest.m_tempDirPath = AZStd::string(workFolder.toUtf8().data());
  436. QString sourceFullPath(builderParams.m_processJobRequest.m_fullPath.c_str());
  437. if (sourceFullPath.length() >= AP_MAX_PATH_LEN)
  438. {
  439. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "Source Asset: %s filepath length %d exceeds the maximum path length (%d) allowed.\n", sourceFullPath.toUtf8().data(), sourceFullPath.length(), AP_MAX_PATH_LEN);
  440. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  441. }
  442. else
  443. {
  444. if (!JobCancelListener.IsCancelled())
  445. {
  446. bool runProcessJob = true;
  447. if (m_jobDetails.m_checkServer)
  448. {
  449. AssetServerMode assetServerMode = AssetServerMode::Inactive;
  450. AssetServerBus::BroadcastResult(assetServerMode, &AssetServerBus::Events::GetRemoteCachingMode);
  451. QFileInfo fileInfo(builderParams.m_processJobRequest.m_sourceFile.c_str());
  452. builderParams.m_serverKey = QString("%1_%2_%3_%4")
  453. .arg(fileInfo.completeBaseName(),
  454. builderParams.m_processJobRequest.m_jobDescription.m_jobKey.c_str(),
  455. builderParams.m_processJobRequest.m_platformInfo.m_identifier.c_str())
  456. .arg(builderParams.m_rcJob->GetOriginalFingerprint());
  457. bool operationResult = false;
  458. if (assetServerMode == AssetServerMode::Server)
  459. {
  460. // sending process job command to the builder
  461. builderParams.m_assetBuilderDesc.m_processJobFunction(builderParams.m_processJobRequest, result);
  462. runProcessJob = false;
  463. if (result.m_resultCode == AssetBuilderSDK::ProcessJobResult_Success)
  464. {
  465. auto beforeStoreResult = BeforeStoringJobResult(builderParams, result);
  466. if (beforeStoreResult.IsSuccess())
  467. {
  468. AssetProcessor::AssetServerBus::BroadcastResult(operationResult, &AssetProcessor::AssetServerBusTraits::StoreJobResult, builderParams, beforeStoreResult.GetValue());
  469. }
  470. else
  471. {
  472. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "Failed preparing store result for %s", builderParams.m_processJobRequest.m_sourceFile.c_str());
  473. }
  474. if (!operationResult)
  475. {
  476. AZ_TracePrintf(AssetProcessor::DebugChannel, "Unable to save job (%s, %s, %s) with fingerprint (%u) to the server.\n",
  477. builderParams.m_rcJob->GetJobEntry().m_sourceAssetReference.AbsolutePath().c_str(), builderParams.m_rcJob->GetJobKey().toUtf8().data(),
  478. builderParams.m_rcJob->GetPlatformInfo().m_identifier.c_str(), builderParams.m_rcJob->GetOriginalFingerprint());
  479. }
  480. else
  481. {
  482. for (auto& product : result.m_outputProducts)
  483. {
  484. product.m_outputFlags |= AssetBuilderSDK::ProductOutputFlags::CachedAsset;
  485. }
  486. }
  487. }
  488. }
  489. else if (assetServerMode == AssetServerMode::Client)
  490. {
  491. // running as client, check with the server whether it has already
  492. // processed this asset, if not or if the operation fails then process locally
  493. AssetProcessor::AssetServerBus::BroadcastResult(operationResult, &AssetProcessor::AssetServerBusTraits::RetrieveJobResult, builderParams);
  494. if (operationResult)
  495. {
  496. operationResult = AfterRetrievingJobResult(builderParams, jobLogTraceListener, result);
  497. }
  498. else
  499. {
  500. AZ_TracePrintf(AssetProcessor::DebugChannel, "Unable to get job (%s, %s, %s) with fingerprint (%u) from the server. Processing locally.\n",
  501. builderParams.m_rcJob->GetJobEntry().m_sourceAssetReference.AbsolutePath().c_str(), builderParams.m_rcJob->GetJobKey().toUtf8().data(),
  502. builderParams.m_rcJob->GetPlatformInfo().m_identifier.c_str(), builderParams.m_rcJob->GetOriginalFingerprint());
  503. }
  504. if (operationResult)
  505. {
  506. for (auto& product : result.m_outputProducts)
  507. {
  508. product.m_outputFlags |= AssetBuilderSDK::ProductOutputFlags::CachedAsset;
  509. }
  510. }
  511. runProcessJob = !operationResult;
  512. }
  513. }
  514. if(runProcessJob)
  515. {
  516. result.m_outputProducts.clear();
  517. // sending process job command to the builder
  518. builderParams.m_assetBuilderDesc.m_processJobFunction(builderParams.m_processJobRequest, result);
  519. }
  520. }
  521. }
  522. if (JobCancelListener.IsCancelled())
  523. {
  524. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Cancelled;
  525. }
  526. }
  527. bool shouldRemoveTempFolder = true;
  528. if (result.m_resultCode == AssetBuilderSDK::ProcessJobResult_Success)
  529. {
  530. // do a final check of this job to make sure its not making colliding subIds.
  531. AZStd::unordered_map<AZ::u32, AZStd::string> subIdsFound;
  532. for (const AssetBuilderSDK::JobProduct& product : result.m_outputProducts)
  533. {
  534. if (!subIdsFound.insert({ product.m_productSubID, product.m_productFileName }).second)
  535. {
  536. // if this happens the element was already in the set.
  537. AZ_Error(AssetBuilderSDK::ErrorWindow, false,
  538. "The builder created more than one asset with the same subID (%u) when emitting product %.*s, colliding with %.*s\n Builders should set a unique m_productSubID value for each product, as this is used as part of the address of the asset.",
  539. product.m_productSubID,
  540. AZ_STRING_ARG(product.m_productFileName),
  541. AZ_STRING_ARG(subIdsFound[product.m_productSubID]));
  542. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  543. break;
  544. }
  545. }
  546. }
  547. if(result.m_resultCode == AssetBuilderSDK::ProcessJobResult_Success)
  548. {
  549. bool handledDependencies = true; // True in case there are no outputs
  550. for (const AssetBuilderSDK::JobProduct& jobProduct : result.m_outputProducts)
  551. {
  552. handledDependencies = false; // False by default since there are outputs
  553. if(jobProduct.m_dependenciesHandled)
  554. {
  555. handledDependencies = true;
  556. break;
  557. }
  558. }
  559. if(!handledDependencies)
  560. {
  561. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "The builder (%s) has not indicated it handled outputting product dependencies for file %s. This is a programmer error.", builderParams.m_assetBuilderDesc.m_name.c_str(), builderParams.m_processJobRequest.m_sourceFile.c_str());
  562. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "For builders that output AZ serialized types, it is recommended to use AssetBuilderSDK::OutputObject which will handle outputting product depenedencies and creating the JobProduct. This is fine to use even if your builder never has product dependencies.");
  563. AZ_Warning(AssetBuilderSDK::WarningWindow, false, "For builders that need custom depenedency parsing that cannot be handled by AssetBuilderSDK::OutputObject or ones that output non-AZ serialized types, add the dependencies to m_dependencies and m_pathDependencies on the JobProduct and then set m_dependenciesHandled to true.");
  564. jobLogTraceListener.AddWarning();
  565. }
  566. WarningLevel warningLevel = WarningLevel::Default;
  567. JobDiagnosticRequestBus::BroadcastResult(warningLevel, &JobDiagnosticRequestBus::Events::GetWarningLevel);
  568. const bool hasErrors = jobLogTraceListener.GetErrorCount() > 0;
  569. const bool hasWarnings = jobLogTraceListener.GetWarningCount() > 0;
  570. if(warningLevel == WarningLevel::FatalErrors && hasErrors)
  571. {
  572. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "Failing job, fatal errors setting is enabled");
  573. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  574. }
  575. else if(warningLevel == WarningLevel::FatalErrorsAndWarnings && (hasErrors || hasWarnings))
  576. {
  577. AZ_Error(AssetBuilderSDK::ErrorWindow, false, "Failing job, fatal errors and warnings setting is enabled");
  578. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  579. }
  580. }
  581. switch (result.m_resultCode)
  582. {
  583. case AssetBuilderSDK::ProcessJobResult_Success:
  584. // make sure there's no subid collision inside a job.
  585. {
  586. if (!CopyCompiledAssets(builderParams, result))
  587. {
  588. result.m_resultCode = AssetBuilderSDK::ProcessJobResult_Failed;
  589. shouldRemoveTempFolder = false;
  590. }
  591. shouldRemoveTempFolder = shouldRemoveTempFolder && !result.m_keepTempFolder && !s_createRequestFileForSuccessfulJob;
  592. }
  593. break;
  594. case AssetBuilderSDK::ProcessJobResult_Crashed:
  595. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Builder indicated that its process crashed!");
  596. break;
  597. case AssetBuilderSDK::ProcessJobResult_Cancelled:
  598. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Builder indicates that the job was cancelled.");
  599. break;
  600. case AssetBuilderSDK::ProcessJobResult_Failed:
  601. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Builder indicated that the job has failed.");
  602. shouldRemoveTempFolder = false;
  603. break;
  604. }
  605. if ((shouldRemoveTempFolder) || (listener.WasQuitRequested()))
  606. {
  607. QDir workingDir(QString(builderParams.m_processJobRequest.m_tempDirPath.c_str()));
  608. workingDir.removeRecursively();
  609. }
  610. // Setting the job id back to zero for error detection
  611. AssetProcessor::SetThreadLocalJobId(0);
  612. listener.BusDisconnect();
  613. JobDiagnosticRequestBus::Broadcast(&JobDiagnosticRequestBus::Events::RecordDiagnosticInfo, builderParams.m_rcJob->GetJobEntry().m_jobRunKey, JobDiagnosticInfo(aznumeric_cast<AZ::u32>(jobLogTraceListener.GetWarningCount()), aznumeric_cast<AZ::u32>(jobLogTraceListener.GetErrorCount())));
  614. }
  615. bool RCJob::CopyCompiledAssets(BuilderParams& params, AssetBuilderSDK::ProcessJobResponse& response)
  616. {
  617. if (response.m_outputProducts.empty())
  618. {
  619. // early out here for performance - no need to do anything at all here so don't waste time with IsDir or Exists or anything.
  620. return true;
  621. }
  622. AZ::IO::Path cacheDirectory = params.m_cacheOutputDir;
  623. AZ::IO::Path intermediateDirectory = params.m_intermediateOutputDir;
  624. AZ::IO::Path relativeFilePath = params.m_relativePath;
  625. QString tempFolder = params.m_processJobRequest.m_tempDirPath.c_str();
  626. QDir tempDir(tempFolder);
  627. if (params.m_cacheOutputDir.empty() || params.m_intermediateOutputDir.empty())
  628. {
  629. AZ_Assert(false, "CopyCompiledAssets: params.m_finalOutputDir or m_intermediateOutputDir is empty for an asset processor job. This should not happen and is because of a recent code change. Check history of any new builders or rcjob.cpp\n");
  630. return false;
  631. }
  632. if (!tempDir.exists())
  633. {
  634. AZ_Assert(false, "CopyCompiledAssets: params.m_processJobRequest.m_tempDirPath is empty for an asset processor job. This should not happen and is because of a recent code change! Check history of RCJob.cpp and any new builder code changes.\n");
  635. return false;
  636. }
  637. // copy the built products into the appropriate location in the real cache and update the job status accordingly.
  638. // note that we go to the trouble of first doing all the checking for disk space and existence of the source files
  639. // before we notify the AP or start moving any of the files so that failures cause the least amount of damage possible.
  640. // this vector is a set of pairs where the first of each pair is the source file (absolute) we intend to copy
  641. // and the second is the product destination we intend to copy it to.
  642. QList< QPair<QString, QString> > outputsToCopy;
  643. outputsToCopy.reserve(static_cast<int>(response.m_outputProducts.size()));
  644. QList<QPair<QString, AZ::Uuid>> intermediateOutputPaths;
  645. qint64 fileSizeRequired = 0;
  646. bool needCacheDirectory = false;
  647. bool needIntermediateDirectory = false;
  648. for (AssetBuilderSDK::JobProduct& product : response.m_outputProducts)
  649. {
  650. // each Output Product communicated by the builder will either be
  651. // * a relative path, which means we assume its relative to the temp folder, and we attempt to move the file
  652. // * an absolute path in the temp folder, and we attempt to move also
  653. // * an absolute path outside the temp folder, in which we assume you'd like to just copy a file somewhere.
  654. QString outputProduct = QString::fromUtf8(product.m_productFileName.c_str()); // could be a relative path.
  655. QFileInfo fileInfo(outputProduct);
  656. if (fileInfo.isRelative())
  657. {
  658. // we assume that its relative to the TEMP folder.
  659. fileInfo = QFileInfo(tempDir.absoluteFilePath(outputProduct));
  660. }
  661. QString absolutePathOfSource = fileInfo.absoluteFilePath();
  662. QString outputFilename = fileInfo.fileName();
  663. bool outputToCache = (product.m_outputFlags & AssetBuilderSDK::ProductOutputFlags::ProductAsset) == AssetBuilderSDK::ProductOutputFlags::ProductAsset;
  664. bool outputToIntermediate = (product.m_outputFlags & AssetBuilderSDK::ProductOutputFlags::IntermediateAsset) ==
  665. AssetBuilderSDK::ProductOutputFlags::IntermediateAsset;
  666. if (outputToCache && outputToIntermediate)
  667. {
  668. // We currently do not support both since intermediate outputs require the Common platform, which is not supported for cache outputs yet
  669. AZ_Error(AssetProcessor::ConsoleChannel, false, "Outputting an asset as both a product and intermediate is not supported. To output both, please split the job into two separate ones.");
  670. return false;
  671. }
  672. if (!outputToCache && !outputToIntermediate)
  673. {
  674. AZ_Error(AssetProcessor::ConsoleChannel, false, "An output asset must be flagged as either a product or an intermediate asset. "
  675. "Please update the output job to include either AssetBuilderSDK::ProductOutputFlags::ProductAsset "
  676. "or AssetBuilderSDK::ProductOutputFlags::IntermediateAsset");
  677. return false;
  678. }
  679. // Intermediates are required to output for the common platform only
  680. if (outputToIntermediate && params.m_processJobRequest.m_platformInfo.m_identifier != AssetBuilderSDK::CommonPlatformName)
  681. {
  682. AZ_Error(AssetProcessor::ConsoleChannel, false, "Intermediate outputs are only supported for the %s platform. "
  683. "Either change the Job platform to %s or change the output flag to AssetBuilderSDK::ProductOutputFlags::ProductAsset",
  684. AssetBuilderSDK::CommonPlatformName,
  685. AssetBuilderSDK::CommonPlatformName);
  686. return false;
  687. }
  688. // Common platform is not currently supported for product assets
  689. if (outputToCache && params.m_processJobRequest.m_platformInfo.m_identifier == AssetBuilderSDK::CommonPlatformName)
  690. {
  691. AZ_Error(
  692. AssetProcessor::ConsoleChannel, false,
  693. "Product asset outputs are not currently supported for the %s platform. "
  694. "Either change the Job platform to a normal platform or change the output flag to AssetBuilderSDK::ProductOutputFlags::IntermediateAsset",
  695. AssetBuilderSDK::CommonPlatformName);
  696. return false;
  697. }
  698. const bool isSourceMetadataEnabled = !params.m_sourceUuid.IsNull();
  699. if (isSourceMetadataEnabled)
  700. {
  701. // For metadata enabled files, the output file needs to be prefixed to handle multiple files with the same relative path.
  702. // This phase will just use a temporary prefix which is longer and less likely to result in accidental conflicts.
  703. // During AssetProcessed_Impl in APM, the prefixing will be resolved to figure out which file is highest priority and gets renamed
  704. // back to the non-prefixed, backwards compatible format and every other file with the same rel path will be re-prefixed to a finalized form.
  705. ProductOutputUtil::GetInterimProductPath(outputFilename, params.m_rcJob->GetJobEntry().m_sourceAssetReference.ScanFolderId());
  706. }
  707. if(outputToCache)
  708. {
  709. needCacheDirectory = true;
  710. if(!product.m_outputPathOverride.empty())
  711. {
  712. AZ_Error(AssetProcessor::ConsoleChannel, false, "%s specified m_outputPathOverride on a ProductAsset. This is not supported."
  713. " Please update the builder accordingly.", params.m_processJobRequest.m_sourceFile.c_str());
  714. return false;
  715. }
  716. if (!VerifyOutputProduct(
  717. QDir(cacheDirectory.c_str()), outputFilename, absolutePathOfSource, fileSizeRequired,
  718. outputsToCopy))
  719. {
  720. return false;
  721. }
  722. }
  723. if(outputToIntermediate)
  724. {
  725. needIntermediateDirectory = true;
  726. if(!product.m_outputPathOverride.empty())
  727. {
  728. relativeFilePath = product.m_outputPathOverride;
  729. }
  730. if (VerifyOutputProduct(
  731. QDir(intermediateDirectory.c_str()), outputFilename, absolutePathOfSource, fileSizeRequired, outputsToCopy))
  732. {
  733. // A null uuid indicates the source is not using metadata files.
  734. // The assumption for the UUID generated below is that the source UUID will not change. A type which has no metadata
  735. // file currently may be updated later to have a metadata file, which would break that assumption. In that case, stick
  736. // with the default path-based UUID.
  737. if (isSourceMetadataEnabled)
  738. {
  739. // Generate a UUID for the intermediate as:
  740. // SourceUuid:BuilderUuid:SubId
  741. auto uuid = AZ::Uuid::CreateName(AZStd::string::format(
  742. "%s:%s:%d",
  743. params.m_sourceUuid.ToFixedString().c_str(),
  744. params.m_assetBuilderDesc.m_busId.ToFixedString().c_str(),
  745. product.m_productSubID));
  746. // Add the product absolute path to the list of intermediates
  747. intermediateOutputPaths.append(QPair(outputsToCopy.back().second, uuid));
  748. }
  749. }
  750. else
  751. {
  752. return false;
  753. }
  754. }
  755. // update the productFileName to be the scanfolder relative path (without the platform)
  756. product.m_productFileName = (relativeFilePath / outputFilename.toUtf8().constData()).c_str();
  757. }
  758. // now we can check if there's enough space for ALL the files before we copy any.
  759. bool hasSpace = false;
  760. auto* diskSpaceInfoInterface = AZ::Interface<AssetProcessor::IDiskSpaceInfo>::Get();
  761. if (diskSpaceInfoInterface)
  762. {
  763. hasSpace = diskSpaceInfoInterface->CheckSufficientDiskSpace(fileSizeRequired, false);
  764. }
  765. if (!hasSpace)
  766. {
  767. AZ_Error(
  768. AssetProcessor::ConsoleChannel, false,
  769. "Cannot save file(s) to cache, not enough disk space to save all the products of %s. Total needed: %lli bytes",
  770. params.m_processJobRequest.m_sourceFile.c_str(), fileSizeRequired);
  771. return false;
  772. }
  773. // if we get here, we are good to go in terms of disk space and sources existing, so we make the best attempt we can.
  774. // if outputDirectory does not exist then create it
  775. unsigned int waitTimeInSecs = 3;
  776. if (needCacheDirectory && !AssetUtilities::CreateDirectoryWithTimeout(QDir(cacheDirectory.AsPosix().c_str()), waitTimeInSecs))
  777. {
  778. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Failed to create output directory: %s\n", cacheDirectory.c_str());
  779. return false;
  780. }
  781. if (needIntermediateDirectory && !AssetUtilities::CreateDirectoryWithTimeout(QDir(intermediateDirectory.AsPosix().c_str()), waitTimeInSecs))
  782. {
  783. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Failed to create intermediate directory: %s\n", intermediateDirectory.c_str());
  784. return false;
  785. }
  786. auto* uuidInterface = AZ::Interface<AzToolsFramework::IUuidUtil>::Get();
  787. if (!uuidInterface)
  788. {
  789. AZ_Assert(false, "Programmer Error - IUuidUtil interface is not available");
  790. return false;
  791. }
  792. // Go through all the intermediate products and output the assigned UUID
  793. for (auto [intermediateProduct, uuid] : intermediateOutputPaths)
  794. {
  795. if(!uuidInterface->CreateSourceUuid(intermediateProduct.toUtf8().constData(), uuid))
  796. {
  797. AZ_TracePrintf(AssetBuilderSDK::ErrorWindow, "Failed to create metadata file for intermediate product " AZ_STRING_FORMAT, AZ_STRING_ARG(intermediateProduct));
  798. }
  799. }
  800. bool anyFileFailed = false;
  801. for (const QPair<QString, QString>& filePair : outputsToCopy)
  802. {
  803. const QString& sourceAbsolutePath = filePair.first;
  804. const QString& productAbsolutePath = filePair.second;
  805. bool isCopyJob = !(sourceAbsolutePath.startsWith(tempFolder, Qt::CaseInsensitive));
  806. isCopyJob |= response.m_keepTempFolder; // Copy instead of Move if the builder wants to keep the Temp Folder.
  807. if (!MoveCopyFile(sourceAbsolutePath, productAbsolutePath, isCopyJob)) // this has its own traceprintf for failure
  808. {
  809. // MoveCopyFile will have output to the log. No need to double output here.
  810. anyFileFailed = true;
  811. continue;
  812. }
  813. //we now ensure that the file is writable - this is just a warning if it fails, not a complete failure.
  814. if (!AssetUtilities::MakeFileWritable(productAbsolutePath))
  815. {
  816. AZ_TracePrintf(AssetBuilderSDK::WarningWindow, "Unable to change permission for the file: %s.\n", productAbsolutePath.toUtf8().data());
  817. }
  818. }
  819. return !anyFileFailed;
  820. }
  821. bool RCJob::VerifyOutputProduct(
  822. QDir outputDirectory,
  823. QString outputFilename,
  824. QString absolutePathOfSource,
  825. qint64& totalFileSizeRequired,
  826. QList<QPair<QString, QString>>& outputsToCopy)
  827. {
  828. QString productFile = AssetUtilities::NormalizeFilePath(outputDirectory.filePath(outputFilename.toLower()));
  829. // Don't make productFile all lowercase for case-insensitive as this
  830. // breaks macOS. The case is already setup properly when the job
  831. // was created.
  832. if (productFile.length() >= AP_MAX_PATH_LEN)
  833. {
  834. AZ_Error(
  835. AssetBuilderSDK::ErrorWindow, false,
  836. "Cannot copy file: Product '%s' path length (%d) exceeds the max path length (%d) allowed on disk\n",
  837. productFile.toUtf8().data(), productFile.length(), AP_MAX_PATH_LEN);
  838. return false;
  839. }
  840. QFileInfo inFile(absolutePathOfSource);
  841. if (!inFile.exists())
  842. {
  843. AZ_Error(
  844. AssetBuilderSDK::ErrorWindow, false,
  845. "Cannot copy file - product file with absolute path '%s' attempting to save into cache could not be found",
  846. absolutePathOfSource.toUtf8().constData());
  847. return false;
  848. }
  849. totalFileSizeRequired += inFile.size();
  850. outputsToCopy.push_back(qMakePair(absolutePathOfSource, productFile));
  851. return true;
  852. }
  853. AZ::Outcome<AZStd::vector<AZStd::string>> RCJob::BeforeStoringJobResult(const BuilderParams& builderParams, AssetBuilderSDK::ProcessJobResponse jobResponse)
  854. {
  855. AZStd::string normalizedTempFolderPath = builderParams.m_processJobRequest.m_tempDirPath;
  856. AzFramework::StringFunc::Path::Normalize(normalizedTempFolderPath);
  857. AZStd::vector<AZStd::string> sourceFiles;
  858. for (AssetBuilderSDK::JobProduct& product : jobResponse.m_outputProducts)
  859. {
  860. // Try to handle Absolute paths within the temp folder
  861. AzFramework::StringFunc::Path::Normalize(product.m_productFileName);
  862. if (!AzFramework::StringFunc::Replace(product.m_productFileName, normalizedTempFolderPath.c_str(), s_tempString))
  863. {
  864. // From CopyCompiledAssets:
  865. // each Output Product communicated by the builder will either be
  866. // * a relative path, which means we assume its relative to the temp folder, and we attempt to move the file
  867. // * an absolute path in the temp folder, and we attempt to move also
  868. // * an absolute path outside the temp folder, in which we assume you'd like to just copy a file somewhere.
  869. // We need to handle case 3 here (Case 2 was above, case 1 is treated as relative within temp)
  870. // If the path was not absolute within the temp folder and not relative it should be an absolute path beneath our source (Including the source)
  871. // meaning a copy job which needs to be added to our archive.
  872. if (!AzFramework::StringFunc::Path::IsRelative(product.m_productFileName.c_str()))
  873. {
  874. AZStd::string sourceFile{ builderParams.m_rcJob->GetJobEntry().GetAbsoluteSourcePath().toUtf8().data() };
  875. AzFramework::StringFunc::Path::Normalize(sourceFile);
  876. AzFramework::StringFunc::Path::StripFullName(sourceFile);
  877. size_t sourcePathPos = product.m_productFileName.find(sourceFile.c_str());
  878. if(sourcePathPos != AZStd::string::npos)
  879. {
  880. sourceFiles.push_back(product.m_productFileName.substr(sourceFile.size()).c_str());
  881. AzFramework::StringFunc::Path::Join(s_tempString, product.m_productFileName.substr(sourceFile.size()).c_str(), product.m_productFileName);
  882. }
  883. else
  884. {
  885. AZ_Warning(AssetBuilderSDK::WarningWindow, false,
  886. "Failed to find source path %s or temp path %s in non relative path in %s",
  887. sourceFile.c_str(), normalizedTempFolderPath.c_str(), product.m_productFileName.c_str());
  888. }
  889. }
  890. }
  891. }
  892. AZStd::string responseFilePath;
  893. AzFramework::StringFunc::Path::ConstructFull(builderParams.m_processJobRequest.m_tempDirPath.c_str(), AssetBuilderSDK::s_processJobResponseFileName, responseFilePath, true);
  894. //Save ProcessJobResponse to disk
  895. if (!AZ::Utils::SaveObjectToFile(responseFilePath, AZ::DataStream::StreamType::ST_XML, &jobResponse))
  896. {
  897. return AZ::Failure();
  898. }
  899. AzToolsFramework::AssetSystem::JobInfo jobInfo;
  900. AzToolsFramework::AssetSystem::AssetJobLogResponse jobLogResponse;
  901. jobInfo.m_sourceFile = builderParams.m_rcJob->GetJobEntry().m_sourceAssetReference.RelativePath().c_str();
  902. jobInfo.m_platform = builderParams.m_rcJob->GetPlatformInfo().m_identifier.c_str();
  903. jobInfo.m_jobKey = builderParams.m_rcJob->GetJobKey().toUtf8().data();
  904. jobInfo.m_builderGuid = builderParams.m_rcJob->GetBuilderGuid();
  905. jobInfo.m_jobRunKey = builderParams.m_rcJob->GetJobEntry().m_jobRunKey;
  906. jobInfo.m_watchFolder = builderParams.m_processJobRequest.m_watchFolder;
  907. AssetUtilities::ReadJobLog(jobInfo, jobLogResponse);
  908. //Save joblog to disk
  909. AZStd::string jobLogFilePath;
  910. AzFramework::StringFunc::Path::ConstructFull(builderParams.m_processJobRequest.m_tempDirPath.c_str(), s_jobLogFileName, jobLogFilePath, true);
  911. if (!AZ::Utils::SaveObjectToFile(jobLogFilePath, AZ::DataStream::StreamType::ST_XML, &jobLogResponse))
  912. {
  913. return AZ::Failure();
  914. }
  915. return AZ::Success(sourceFiles);
  916. }
  917. bool RCJob::AfterRetrievingJobResult(const BuilderParams& builderParams, AssetUtilities::JobLogTraceListener& jobLogTraceListener, AssetBuilderSDK::ProcessJobResponse& jobResponse)
  918. {
  919. AZStd::string responseFilePath;
  920. AzFramework::StringFunc::Path::ConstructFull(builderParams.m_processJobRequest.m_tempDirPath.c_str(), AssetBuilderSDK::s_processJobResponseFileName, responseFilePath, true);
  921. if (!AZ::Utils::LoadObjectFromFileInPlace(responseFilePath.c_str(), jobResponse))
  922. {
  923. return false;
  924. }
  925. //Ensure that ProcessJobResponse have the correct absolute paths
  926. for (AssetBuilderSDK::JobProduct& product : jobResponse.m_outputProducts)
  927. {
  928. AzFramework::StringFunc::Replace(product.m_productFileName, s_tempString, builderParams.m_processJobRequest.m_tempDirPath.c_str(), s_tempString);
  929. }
  930. AZStd::string jobLogFilePath;
  931. AzFramework::StringFunc::Path::ConstructFull(builderParams.m_processJobRequest.m_tempDirPath.c_str(), s_jobLogFileName, jobLogFilePath, true);
  932. AzToolsFramework::AssetSystem::AssetJobLogResponse jobLogResponse;
  933. if (!AZ::Utils::LoadObjectFromFileInPlace(jobLogFilePath.c_str(), jobLogResponse))
  934. {
  935. return false;
  936. }
  937. if (!jobLogResponse.m_isSuccess)
  938. {
  939. AZ_TracePrintf(AssetProcessor::DebugChannel, "Job log request was unsuccessful for job (%s, %s, %s) from the server.\n",
  940. builderParams.m_rcJob->GetJobEntry().m_sourceAssetReference.AbsolutePath().c_str(), builderParams.m_rcJob->GetJobKey().toUtf8().data(),
  941. builderParams.m_rcJob->GetPlatformInfo().m_identifier.c_str());
  942. if(jobLogResponse.m_jobLog.find("No log file found") != AZStd::string::npos)
  943. {
  944. AZ_TracePrintf(AssetProcessor::DebugChannel, "Unable to find job log from the server. This could happen if you are trying to use the server cache with a copy job, "
  945. "please check the assetprocessorplatformconfig.ini file and ensure that server cache is disabled for the job.\n");
  946. }
  947. return false;
  948. }
  949. // writing server logs
  950. AZ_TracePrintf(AssetProcessor::DebugChannel, "------------SERVER BEGIN----------\n");
  951. AzToolsFramework::Logging::LogLine::ParseLog(jobLogResponse.m_jobLog.c_str(), jobLogResponse.m_jobLog.size(),
  952. [&jobLogTraceListener](AzToolsFramework::Logging::LogLine& line)
  953. {
  954. jobLogTraceListener.AppendLog(line);
  955. });
  956. AZ_TracePrintf(AssetProcessor::DebugChannel, "------------SERVER END----------\n");
  957. return true;
  958. }
  959. AZStd::string BuilderParams::GetTempJobDirectory() const
  960. {
  961. return m_processJobRequest.m_tempDirPath;
  962. }
  963. QString BuilderParams::GetServerKey() const
  964. {
  965. return m_serverKey;
  966. }
  967. } // namespace AssetProcessor
  968. //////////////////////////////////////////////////////////////////////////