@nrwl/nx-cloud 15.0.0 → 15.0.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,263 @@
1
- const a6_0xdc60=['Agent\x20','Agent\x20was\x20terminated\x20via\x20SIGINT','done','NO_MESSAGES_TIMEOUT','executionId','length','map','completed:\x20','ignore','../../../utilities/nx-imports','Error:','RUN_GROUP_COMPLETED','throw','--configuration=','catch','error','Duplicate\x20Agent\x20ID\x20Detected','executionId:\x20','retryDuring:\x20','tasks','projectName',').\x20Tasks\x20hashes\x20haven\x27t\x20been\x20recorded.','status:\x20','criticalErrorMessage','SIGTERM','If\x20you\x20believe\x20this\x20is\x20the\x20case,\x20run\x20\x22npx\x20nx-cloud\x20clean-up-agents\x22.','find','NX_AGENT_NAME','error:\x20','parse','join','retryDuring','../../error/print-run-group-error','defineProperty','forEach','toString','reset','readdirSync','env','SIGINT','Command\x20execution\x20failed\x20(distributed\x20task\x20execution:\x20','readFileSync','existsSync','/tasks-hashes-','No\x20new\x20messages\x20received\x20after\x20','DistributedAgentApi','cacheDirectory','getRunGroup','../../../utilities/create-unchanged-value-timeout','completedStatusCode','assign','tasksRunnerOptions','maxParallel:\x20','../../../utilities/waiter','true','unlinkSync','VERBOSE_LOGGING','status','createUnchangedValueTimeout','../../../utilities/environment','Waiting...','completed','CIRCLE_STAGE','We\x20have\x20detected\x20another\x20agent\x20with\x20this\x20ID\x20running\x20in\x20this\x20workspace.\x20This\x20should\x20not\x20happen.','Distributed\x20Execution\x20Terminated','DISTRIBUTED_TASK_EXECUTION_INTERNAL_ERROR_STATUS_CODE','random','CIRCLE_JOB','configuration','Executing:\x20\x27','completeRunGroupWithError','Starting\x20an\x20agent\x20for\x20running\x20Nx\x20tasks','printRunGroupError','__awaiter','next','getTime','message','Waiter','maxParallel','push','value','We\x20have\x20detected\x20other\x20agents\x20running\x20in\x20this\x20workspace.\x20This\x20can\x20cause\x20unexpected\x20behavior.','wait','params','apply','note','target','../../../utilities/metric-logger','projects','strip-json-comments','completedTasks','includes','warn','CIRCLECI','inherit','npx\x20nx\x20run-many\x20--target=','./distributed-agent.api','startAgent','execSync','.lock','Fetching\x20tasks...','exit','number\x20of\x20tasks:\x20','./node_modules/.cache/nx'];(function(_0x26281c,_0xdc600f){const _0x1905dc=function(_0x12fced){while(--_0x12fced){_0x26281c['push'](_0x26281c['shift']());}};_0x1905dc(++_0xdc600f);}(a6_0xdc60,0x104));const a6_0x1905=function(_0x26281c,_0xdc600f){_0x26281c=_0x26281c-0x0;let _0x1905dc=a6_0xdc60[_0x26281c];return _0x1905dc;};'use strict';var __awaiter=this&&this[a6_0x1905('0x15')]||function(_0x28a0a7,_0x2a7239,_0x7489c8,_0x237d33){function _0x2688a9(_0x1792b5){return _0x1792b5 instanceof _0x7489c8?_0x1792b5:new _0x7489c8(function(_0x5de0ab){_0x5de0ab(_0x1792b5);});}return new(_0x7489c8||(_0x7489c8=Promise))(function(_0x2a6bf7,_0x34783c){function _0x1f11a2(_0x13bf75){try{_0x3325a5(_0x237d33[a6_0x1905('0x16')](_0x13bf75));}catch(_0x171faa){_0x34783c(_0x171faa);}}function _0x1081dd(_0x5598d8){try{_0x3325a5(_0x237d33[a6_0x1905('0x40')](_0x5598d8));}catch(_0x428c51){_0x34783c(_0x428c51);}}function _0x3325a5(_0x2023f5){_0x2023f5[a6_0x1905('0x36')]?_0x2a6bf7(_0x2023f5['value']):_0x2688a9(_0x2023f5[a6_0x1905('0x1c')])['then'](_0x1f11a2,_0x1081dd);}_0x3325a5((_0x237d33=_0x237d33[a6_0x1905('0x20')](_0x28a0a7,_0x2a7239||[]))['next']());});};Object[a6_0x1905('0x55')](exports,'__esModule',{'value':!![]});exports[a6_0x1905('0x2d')]=void 0x0;const child_process_1=require('child_process');const fs_1=require('fs');const stripJsonComments=require(a6_0x1905('0x25'));const create_unchanged_value_timeout_1=require(a6_0x1905('0x64'));const environment_1=require(a6_0x1905('0x7'));const metric_logger_1=require(a6_0x1905('0x23'));const waiter_1=require(a6_0x1905('0x1'));const print_run_group_error_1=require(a6_0x1905('0x54'));const distributed_agent_api_1=require(a6_0x1905('0x2c'));const {output,workspaceRoot}=require(a6_0x1905('0x3d'));function executeTasks(_0x179584,_0xdc59de){return __awaiter(this,void 0x0,void 0x0,function*(){let _0x5bbdee=0x0;let _0x34354b=null;const _0x4dd504=(0x0,create_unchanged_value_timeout_1[a6_0x1905('0x6')])({'title':a6_0x1905('0x60')+environment_1[a6_0x1905('0x37')]/0x3e8+'\x20seconds','timeout':environment_1[a6_0x1905('0x37')]});const _0x359f59=new waiter_1[(a6_0x1905('0x19'))]();let _0x50a525=[];const _0x4f8de1=new Date();let _0x7416a1=![];while(!![]){if(environment_1['VERBOSE_LOGGING']){output['note']({'title':a6_0x1905('0x30')});}_0x34354b=yield _0xdc59de['tasks'](_0x34354b?_0x34354b[a6_0x1905('0x38')]:null,_0x5bbdee,_0x50a525);if(environment_1[a6_0x1905('0x4')]){output[a6_0x1905('0x21')]({'title':'API\x20Response','bodyLines':[a6_0x1905('0x3b')+_0x34354b[a6_0x1905('0x9')],a6_0x1905('0x4a')+_0x34354b[a6_0x1905('0x5')],a6_0x1905('0x46')+_0x34354b[a6_0x1905('0x53')],a6_0x1905('0x45')+_0x34354b[a6_0x1905('0x38')],a6_0x1905('0x32')+_0x34354b['tasks'][a6_0x1905('0x39')],a6_0x1905('0x50')+_0x34354b[a6_0x1905('0x4b')],a6_0x1905('0x0')+_0x34354b[a6_0x1905('0x1a')]]});}if(_0x34354b[a6_0x1905('0x4b')]){output['error']({'title':a6_0x1905('0xc'),'bodyLines':[a6_0x1905('0x3e'),_0x34354b[a6_0x1905('0x4b')]]});process[a6_0x1905('0x31')](0x0);}if((_0x34354b===null||_0x34354b===void 0x0?void 0x0:_0x34354b['retryDuring'])&&(_0x34354b===null||_0x34354b===void 0x0?void 0x0:_0x34354b[a6_0x1905('0x53')])!==0x0&&!_0x7416a1&&new Date()[a6_0x1905('0x17')]()-_0x4f8de1[a6_0x1905('0x17')]()>_0x34354b[a6_0x1905('0x53')]){yield _0x359f59[a6_0x1905('0x1e')]();continue;}if((_0x34354b===null||_0x34354b===void 0x0?void 0x0:_0x34354b[a6_0x1905('0x5')])!==undefined){if(_0x34354b[a6_0x1905('0x5')]===a6_0x1905('0x3f')||_0x34354b['status']==='NO_FURTHER_TASKS_TO_RUN'){return;}}else if(_0x34354b[a6_0x1905('0x9')]){return;}_0x4dd504(_0x34354b[a6_0x1905('0x47')][a6_0x1905('0x3a')](_0xfe130=>_0xfe130['taskId'])[a6_0x1905('0x52')](''));if(!_0x34354b[a6_0x1905('0x38')]){if(environment_1[a6_0x1905('0x4')]){output['note']({'title':a6_0x1905('0x8')});}yield _0x359f59[a6_0x1905('0x1e')]();_0x5bbdee=0x0;_0x50a525=[];continue;}_0x359f59[a6_0x1905('0x58')]();_0x7416a1=!![];const _0x117913=invokeTasksUsingRunMany(_0x179584,_0x34354b['executionId'],_0x34354b[a6_0x1905('0x47')],_0x34354b[a6_0x1905('0x1a')]);_0x5bbdee=_0x117913[a6_0x1905('0x65')];_0x50a525=_0x117913[a6_0x1905('0x26')];}});}function readCompletedTasks(_0xe67307,_0x222e99){const _0x4a727a=a6_0x1905('0x5c')+_0x222e99+a6_0x1905('0x49');let _0x3be938;try{const _0xac7b87=_0xe67307[a6_0x1905('0x62')]||'./node_modules/.cache/nx';const _0x3e892d=_0xac7b87+a6_0x1905('0x5f')+_0x222e99;_0x3be938=JSON['parse']((0x0,fs_1['readFileSync'])(_0x3e892d)['toString']());(0x0,fs_1[a6_0x1905('0x3')])(_0x3e892d);}catch(_0x30b025){throw new Error(_0x4a727a);}if(_0x3be938['length']==0x0){throw new Error(_0x4a727a);}return _0x3be938;}function invokeTasksUsingRunMany(_0x3916f3,_0x2a590d,_0x4c821e,_0x3d76df){let _0x2c5191=0x0;const _0x4160e7=[];for(const _0x2ecfe0 of groupByTarget(_0x4c821e)){const _0x8dd954=_0x2ecfe0[a6_0x1905('0x10')]?a6_0x1905('0x41')+_0x2ecfe0[a6_0x1905('0x10')]:'';const _0x243a5f=_0x3d76df>0x1?'\x20--parallel\x20--max-parallel='+_0x3d76df:'';const _0x5aed7e=a6_0x1905('0x2b')+_0x2ecfe0[a6_0x1905('0x22')]+'\x20'+_0x8dd954+'\x20--projects='+_0x2ecfe0['projects'][a6_0x1905('0x52')](',')+'\x20'+_0x2ecfe0[a6_0x1905('0x1f')]+_0x243a5f;if(environment_1[a6_0x1905('0x4')]){output[a6_0x1905('0x21')]({'title':a6_0x1905('0x11')+_0x5aed7e+'\x27'});}try{(0x0,child_process_1[a6_0x1905('0x2e')])(_0x5aed7e,{'stdio':[a6_0x1905('0x3c'),a6_0x1905('0x2a'),a6_0x1905('0x2a')],'env':Object[a6_0x1905('0x66')](Object['assign']({},process['env']),{'NX_CACHE_FAILURES':'true','NX_CLOUD_DISTRIBUTED_EXECUTION_ID':_0x2a590d,'NX_STREAM_OUTPUT':a6_0x1905('0x2'),'NX_PREFIX_OUTPUT':'true'})});_0x4160e7['push'](...readCompletedTasks(_0x3916f3,_0x2a590d));}catch(_0xd8e124){if(_0xd8e124['status']===environment_1[a6_0x1905('0xd')]){throw _0xd8e124;}else{_0x2c5191=0x1;_0x4160e7[a6_0x1905('0x1b')](...readCompletedTasks(_0x3916f3,_0x2a590d));}}}return{'completedStatusCode':_0x2c5191,'completedTasks':_0x4160e7};}function groupByTarget(_0x250322){const _0x1b749c=[];_0x250322[a6_0x1905('0x56')](_0x4fe226=>{const _0x6993f=_0x1b749c[a6_0x1905('0x4e')](_0x3d23f5=>_0x3d23f5[a6_0x1905('0x22')]===_0x4fe226[a6_0x1905('0x22')]&&_0x3d23f5['configuration']===_0x4fe226[a6_0x1905('0x10')]);if(_0x6993f){_0x6993f[a6_0x1905('0x24')][a6_0x1905('0x1b')](_0x4fe226['projectName']);}else{_0x1b749c[a6_0x1905('0x1b')]({'target':_0x4fe226['target'],'projects':[_0x4fe226[a6_0x1905('0x48')]],'params':_0x4fe226[a6_0x1905('0x1f')],'configuration':_0x4fe226['configuration']});}});return _0x1b749c;}function getAgentName(){if(process[a6_0x1905('0x5a')][a6_0x1905('0x4f')]!==undefined){return process[a6_0x1905('0x5a')]['NX_AGENT_NAME'];}else if(process[a6_0x1905('0x5a')][a6_0x1905('0x29')]!==undefined&&process[a6_0x1905('0x5a')]['CIRCLE_STAGE']){return process[a6_0x1905('0x5a')][a6_0x1905('0xa')];}else if(process[a6_0x1905('0x5a')][a6_0x1905('0x29')]!==undefined&&process[a6_0x1905('0x5a')][a6_0x1905('0xf')]){return process[a6_0x1905('0x5a')][a6_0x1905('0xf')];}else{return a6_0x1905('0x34')+Math['floor'](Math[a6_0x1905('0xe')]()*0x186a0);}}function createAgentLockfileAndSetUpListeners(_0x5a0b5d,_0x5b9b0c,_0x1659d4){const _0x5b6fa8=_0x5b9b0c[a6_0x1905('0x62')]||a6_0x1905('0x33');const _0xf581f6=_0x5b6fa8+'/lockfiles';const _0x209712=_0xf581f6+'/'+_0x1659d4+a6_0x1905('0x2f');if(!(0x0,fs_1[a6_0x1905('0x5e')])(_0xf581f6)){(0x0,fs_1['mkdirSync'])(_0xf581f6,{'recursive':!![]});}const _0x1e2edf=(0x0,fs_1[a6_0x1905('0x59')])(_0xf581f6);if(_0x1e2edf[a6_0x1905('0x39')]){if(_0x1e2edf[a6_0x1905('0x27')](_0x1659d4+a6_0x1905('0x2f'))){output[a6_0x1905('0x43')]({'title':a6_0x1905('0x44'),'bodyLines':[a6_0x1905('0xb'),'','End\x20all\x20currently\x20running\x20agents,\x20run\x20\x22npx\x20nx-cloud\x20clean-up-agents\x22,\x20and\x20try\x20again.']});process['exit'](0x1);}output[a6_0x1905('0x28')]({'title':'Other\x20Nx\x20Cloud\x20Agents\x20Detected','bodyLines':[a6_0x1905('0x1d'),'','This\x20can\x20also\x20be\x20a\x20false\x20positive\x20caused\x20by\x20agents\x20that\x20did\x20not\x20shut\x20down\x20correctly.',a6_0x1905('0x4d')]});}(0x0,fs_1['writeFileSync'])(_0x209712,'');process['on']('exit',_0x2e1ae4=>{cleanupAgentLockfile(_0x209712,_0x2e1ae4);});process['on'](a6_0x1905('0x4c'),()=>__awaiter(this,void 0x0,void 0x0,function*(){yield _0x5a0b5d['completeRunGroupWithError']('Agent\x20was\x20terminated\x20via\x20SIGTERM');cleanupAgentLockfile(_0x209712,0x1);}));process['on'](a6_0x1905('0x5b'),()=>__awaiter(this,void 0x0,void 0x0,function*(){yield _0x5a0b5d[a6_0x1905('0x12')](a6_0x1905('0x35'));cleanupAgentLockfile(_0x209712,0x1);}));}function cleanupAgentLockfile(_0xbc6ce9,_0x18f8a4){if((0x0,fs_1[a6_0x1905('0x5e')])(_0xbc6ce9)){(0x0,fs_1[a6_0x1905('0x3')])(_0xbc6ce9);process[a6_0x1905('0x31')](_0x18f8a4);}}function startAgent(){return __awaiter(this,void 0x0,void 0x0,function*(){const _0x139fc6=(0x0,environment_1[a6_0x1905('0x63')])();if(!_0x139fc6){(0x0,print_run_group_error_1[a6_0x1905('0x14')])();return process[a6_0x1905('0x31')](0x1);}output[a6_0x1905('0x21')]({'title':a6_0x1905('0x13')});const _0x479a1a=JSON[a6_0x1905('0x51')](stripJsonComments((0x0,fs_1[a6_0x1905('0x5d')])(workspaceRoot+'/nx.json')[a6_0x1905('0x57')]()))[a6_0x1905('0x67')]['default']['options'];const _0x162319=getAgentName();const _0xfbafb9=new distributed_agent_api_1[(a6_0x1905('0x61'))](_0x479a1a,_0x139fc6,_0x162319);createAgentLockfileAndSetUpListeners(_0xfbafb9,_0x479a1a,_0x162319);return executeTasks(_0x479a1a,_0xfbafb9)['then'](_0x4ff9df=>__awaiter(this,void 0x0,void 0x0,function*(){yield(0x0,metric_logger_1['submitRunMetrics'])(_0x479a1a);return _0x4ff9df;}))[a6_0x1905('0x42')](_0xdd7d03=>__awaiter(this,void 0x0,void 0x0,function*(){yield _0xfbafb9[a6_0x1905('0x12')]('Critical\x20Error\x20in\x20Agent:\x20\x22'+_0xdd7d03[a6_0x1905('0x18')]+'\x22');throw _0xdd7d03;}));});}exports['startAgent']=startAgent;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.startAgent = void 0;
13
+ const child_process_1 = require("child_process");
14
+ const fs_1 = require("fs");
15
+ const stripJsonComments = require("strip-json-comments");
16
+ const create_unchanged_value_timeout_1 = require("../../../utilities/create-unchanged-value-timeout");
17
+ const environment_1 = require("../../../utilities/environment");
18
+ const metric_logger_1 = require("../../../utilities/metric-logger");
19
+ const waiter_1 = require("../../../utilities/waiter");
20
+ const print_run_group_error_1 = require("../../error/print-run-group-error");
21
+ const distributed_agent_api_1 = require("./distributed-agent.api");
22
+ const { output, workspaceRoot } = require('../../../utilities/nx-imports');
23
+ function executeTasks(options, api) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ let completedStatusCode = 0;
26
+ let apiResponse = null;
27
+ const failIfSameTasksAfterTimeout = (0, create_unchanged_value_timeout_1.createUnchangedValueTimeout)({
28
+ title: `No new messages received after ${environment_1.NO_MESSAGES_TIMEOUT / 1000} seconds`,
29
+ timeout: environment_1.NO_MESSAGES_TIMEOUT,
30
+ });
31
+ const waiter = new waiter_1.Waiter();
32
+ let completedTasks = [];
33
+ const startTime = new Date();
34
+ let executedAnyTasks = false;
35
+ while (true) {
36
+ if (environment_1.VERBOSE_LOGGING) {
37
+ output.note({
38
+ title: 'Fetching tasks...',
39
+ });
40
+ }
41
+ apiResponse = yield api.tasks(apiResponse ? apiResponse.executionId : null, completedStatusCode, completedTasks);
42
+ if (environment_1.VERBOSE_LOGGING) {
43
+ output.note({
44
+ title: 'API Response',
45
+ bodyLines: [
46
+ `completed: ${apiResponse.completed}`,
47
+ `status: ${apiResponse.status}`,
48
+ `retryDuring: ${apiResponse.retryDuring}`,
49
+ `executionId: ${apiResponse.executionId}`,
50
+ `number of tasks: ${apiResponse.tasks.length}`,
51
+ `error: ${apiResponse.criticalErrorMessage}`,
52
+ `maxParallel: ${apiResponse.maxParallel}`,
53
+ ],
54
+ });
55
+ }
56
+ if (apiResponse.criticalErrorMessage) {
57
+ output.error({
58
+ title: 'Distributed Execution Terminated',
59
+ bodyLines: ['Error:', apiResponse.criticalErrorMessage],
60
+ });
61
+ process.exit(0);
62
+ }
63
+ // run group is completed but it might be a rerun
64
+ // we will try several times before going further and
65
+ // completed the response
66
+ // we only do it if we haven't executed any tasks
67
+ if ((apiResponse === null || apiResponse === void 0 ? void 0 : apiResponse.retryDuring) &&
68
+ (apiResponse === null || apiResponse === void 0 ? void 0 : apiResponse.retryDuring) !== 0 &&
69
+ !executedAnyTasks &&
70
+ new Date().getTime() - startTime.getTime() > apiResponse.retryDuring) {
71
+ yield waiter.wait();
72
+ continue;
73
+ }
74
+ if ((apiResponse === null || apiResponse === void 0 ? void 0 : apiResponse.status) !== undefined) {
75
+ if (apiResponse.status === 'RUN_GROUP_COMPLETED' ||
76
+ apiResponse.status === 'NO_FURTHER_TASKS_TO_RUN') {
77
+ return;
78
+ }
79
+ }
80
+ else if (apiResponse.completed) {
81
+ return;
82
+ }
83
+ // if status is present that use the status instead of completed, otherwise use completed
84
+ failIfSameTasksAfterTimeout(apiResponse.tasks.map((t) => t.taskId).join(''));
85
+ if (!apiResponse.executionId) {
86
+ if (environment_1.VERBOSE_LOGGING) {
87
+ output.note({
88
+ title: 'Waiting...',
89
+ });
90
+ }
91
+ yield waiter.wait();
92
+ completedStatusCode = 0;
93
+ completedTasks = [];
94
+ continue;
95
+ }
96
+ waiter.reset();
97
+ executedAnyTasks = true;
98
+ const r = invokeTasksUsingRunMany(options, apiResponse.executionId, apiResponse.tasks, apiResponse.maxParallel);
99
+ completedStatusCode = r.completedStatusCode;
100
+ completedTasks = r.completedTasks;
101
+ }
102
+ });
103
+ }
104
+ function readCompletedTasks(options, distributedExecutionId) {
105
+ const errorMessage = `Command execution failed (distributed task execution: ${distributedExecutionId}). Tasks hashes haven\'t been recorded.`;
106
+ let completedTasks;
107
+ try {
108
+ const cacheDirectory = options.cacheDirectory || './node_modules/.cache/nx';
109
+ const taskHashesFile = `${cacheDirectory}/tasks-hashes-${distributedExecutionId}`;
110
+ completedTasks = JSON.parse((0, fs_1.readFileSync)(taskHashesFile).toString());
111
+ // remove it such that if the next command crashes we don't read an obsolete file
112
+ (0, fs_1.unlinkSync)(taskHashesFile);
113
+ }
114
+ catch (e) {
115
+ throw new Error(errorMessage);
116
+ }
117
+ if (completedTasks.length == 0) {
118
+ throw new Error(errorMessage);
119
+ }
120
+ return completedTasks;
121
+ }
122
+ function invokeTasksUsingRunMany(options, executionId, tasks, maxParallel) {
123
+ let completedStatusCode = 0;
124
+ const completedTasks = [];
125
+ for (const g of groupByTarget(tasks)) {
126
+ const config = g.configuration ? `--configuration=${g.configuration}` : ``;
127
+ const parallel = maxParallel > 1 ? ` --parallel --max-parallel=${maxParallel}` : ``;
128
+ // TODO use pnpx or yarn when needed
129
+ const command = `npx nx run-many --target=${g.target} ${config} --projects=${g.projects.join(',')} ${g.params}${parallel}`;
130
+ if (environment_1.VERBOSE_LOGGING) {
131
+ output.note({
132
+ title: `Executing: '${command}'`,
133
+ });
134
+ }
135
+ try {
136
+ (0, child_process_1.execSync)(command, {
137
+ stdio: ['ignore', 'inherit', 'inherit'],
138
+ env: Object.assign(Object.assign({}, process.env), { NX_CACHE_FAILURES: 'true', NX_CLOUD_DISTRIBUTED_EXECUTION_ID: executionId, NX_STREAM_OUTPUT: 'true', NX_PREFIX_OUTPUT: 'true' }),
139
+ });
140
+ completedTasks.push(...readCompletedTasks(options, executionId));
141
+ }
142
+ catch (e) {
143
+ if (e.status === environment_1.DISTRIBUTED_TASK_EXECUTION_INTERNAL_ERROR_STATUS_CODE) {
144
+ throw e;
145
+ }
146
+ else {
147
+ completedStatusCode = 1;
148
+ completedTasks.push(...readCompletedTasks(options, executionId));
149
+ }
150
+ }
151
+ }
152
+ return { completedStatusCode, completedTasks };
153
+ }
154
+ function groupByTarget(tasks) {
155
+ const res = [];
156
+ tasks.forEach((t) => {
157
+ const r = res.find((rr) => rr.target === t.target && rr.configuration === t.configuration);
158
+ if (r) {
159
+ r.projects.push(t.projectName);
160
+ }
161
+ else {
162
+ res.push({
163
+ target: t.target,
164
+ projects: [t.projectName],
165
+ params: t.params,
166
+ configuration: t.configuration,
167
+ });
168
+ }
169
+ });
170
+ return res;
171
+ }
172
+ function getAgentName() {
173
+ if (process.env.NX_AGENT_NAME !== undefined) {
174
+ return process.env.NX_AGENT_NAME;
175
+ }
176
+ else if (process.env.CIRCLECI !== undefined && process.env.CIRCLE_STAGE) {
177
+ return process.env.CIRCLE_STAGE;
178
+ }
179
+ else if (process.env.CIRCLECI !== undefined && process.env.CIRCLE_JOB) {
180
+ return process.env.CIRCLE_JOB;
181
+ }
182
+ else {
183
+ return `Agent ${Math.floor(Math.random() * 100000)}`;
184
+ }
185
+ }
186
+ function createAgentLockfileAndSetUpListeners(api, options, agentName) {
187
+ const cacheDirectory = options.cacheDirectory || './node_modules/.cache/nx';
188
+ const lockFileDirectory = `${cacheDirectory}/lockfiles`;
189
+ const lockFilePath = `${lockFileDirectory}/${agentName}.lock`;
190
+ if (!(0, fs_1.existsSync)(lockFileDirectory)) {
191
+ (0, fs_1.mkdirSync)(lockFileDirectory, { recursive: true });
192
+ }
193
+ // Check for other agents' lockfiles and warn if exist
194
+ const lockFiles = (0, fs_1.readdirSync)(lockFileDirectory);
195
+ if (lockFiles.length) {
196
+ // Check to make sure the current agent name is not in use (only 1/100000 ^ 2 chance of this)
197
+ if (lockFiles.includes(`${agentName}.lock`)) {
198
+ output.error({
199
+ title: 'Duplicate Agent ID Detected',
200
+ bodyLines: [
201
+ 'We have detected another agent with this ID running in this workspace. This should not happen.',
202
+ '',
203
+ 'End all currently running agents, run "npx nx-cloud clean-up-agents", and try again.',
204
+ ],
205
+ });
206
+ process.exit(1);
207
+ }
208
+ output.warn({
209
+ title: 'Other Nx Cloud Agents Detected',
210
+ bodyLines: [
211
+ 'We have detected other agents running in this workspace. This can cause unexpected behavior.',
212
+ '',
213
+ 'This can also be a false positive caused by agents that did not shut down correctly.',
214
+ 'If you believe this is the case, run "npx nx-cloud clean-up-agents".',
215
+ ],
216
+ });
217
+ }
218
+ (0, fs_1.writeFileSync)(lockFilePath, '');
219
+ process.on('exit', (code) => {
220
+ cleanupAgentLockfile(lockFilePath, code);
221
+ });
222
+ process.on('SIGTERM', () => __awaiter(this, void 0, void 0, function* () {
223
+ yield api.completeRunGroupWithError('Agent was terminated via SIGTERM');
224
+ cleanupAgentLockfile(lockFilePath, 1);
225
+ }));
226
+ process.on('SIGINT', () => __awaiter(this, void 0, void 0, function* () {
227
+ yield api.completeRunGroupWithError('Agent was terminated via SIGINT');
228
+ cleanupAgentLockfile(lockFilePath, 1);
229
+ }));
230
+ }
231
+ function cleanupAgentLockfile(lockFilePath, code) {
232
+ if ((0, fs_1.existsSync)(lockFilePath)) {
233
+ (0, fs_1.unlinkSync)(lockFilePath);
234
+ process.exit(code);
235
+ }
236
+ }
237
+ function startAgent() {
238
+ return __awaiter(this, void 0, void 0, function* () {
239
+ const runGroup = (0, environment_1.getRunGroup)();
240
+ if (!runGroup) {
241
+ (0, print_run_group_error_1.printRunGroupError)();
242
+ return process.exit(1);
243
+ }
244
+ output.note({
245
+ title: 'Starting an agent for running Nx tasks',
246
+ });
247
+ const options = JSON.parse(stripJsonComments((0, fs_1.readFileSync)(`${workspaceRoot}/nx.json`).toString())).tasksRunnerOptions.default.options;
248
+ const agentName = getAgentName();
249
+ const api = new distributed_agent_api_1.DistributedAgentApi(options, runGroup, agentName);
250
+ createAgentLockfileAndSetUpListeners(api, options, agentName);
251
+ return executeTasks(options, api)
252
+ .then((res) => __awaiter(this, void 0, void 0, function* () {
253
+ yield (0, metric_logger_1.submitRunMetrics)(options);
254
+ return res;
255
+ }))
256
+ .catch((e) => __awaiter(this, void 0, void 0, function* () {
257
+ yield api.completeRunGroupWithError(`Critical Error in Agent: "${e.message}"`);
258
+ throw e;
259
+ }));
260
+ });
261
+ }
262
+ exports.startAgent = startAgent;
263
+ //# sourceMappingURL=distributed-agent.impl.js.map
@@ -1 +1,143 @@
1
- const a7_0xfc08=['hash','project','false','/nx-cloud/executions/status','true','then','parseCommand','maxParallel','../../../utilities/environment','runGroup:\x20','../../../utilities/serializer-overrides','recordMetric','completeRunGroup','createStartRequest','agentCount','Starting\x20a\x20distributed\x20execution','next','note','Completing\x20run\x20group\x20with\x20an\x20error','completeRunGroupWithError','data','mapRespToPerfEntry','createMetricRecorder','done','dteStatus','RUNNER_FAILURE_PERF_ENTRY','stopAgentsOnFailure','post','axiosException','configuration','defineProperty','NX_CLOUD_DISTRIBUTED_EXECUTION_AGENT_COUNT','__awaiter','value','response','exit','error:\x20','apiAxiosInstance','axiosMultipleTries','/nx-cloud/executions/start','getBranch','message','VERBOSE_LOGGING','apply','error','serializeOverrides','Workspace\x20is\x20disabled.\x20Cannot\x20perform\x20distributed\x20task\x20executions.','enabled','DistributedExecutionApi','createApiAxiosInstance','start','parallel','target','map','../../../utilities/nx-imports'];(function(_0x12e180,_0xfc0808){const _0x9a07f0=function(_0x2a9231){while(--_0x2a9231){_0x12e180['push'](_0x12e180['shift']());}};_0x9a07f0(++_0xfc0808);}(a7_0xfc08,0xb3));const a7_0x9a07=function(_0x12e180,_0xfc0808){_0x12e180=_0x12e180-0x0;let _0x9a07f0=a7_0xfc08[_0x12e180];return _0x9a07f0;};'use strict';var __awaiter=this&&this[a7_0x9a07('0x12')]||function(_0x477154,_0x49b534,_0x115eba,_0x35d958){function _0x582b4a(_0xe08f2f){return _0xe08f2f instanceof _0x115eba?_0xe08f2f:new _0x115eba(function(_0x3205df){_0x3205df(_0xe08f2f);});}return new(_0x115eba||(_0x115eba=Promise))(function(_0x5367e6,_0x28dfc3){function _0x20466e(_0x1598ab){try{_0x5d97db(_0x35d958[a7_0x9a07('0x2')](_0x1598ab));}catch(_0x293220){_0x28dfc3(_0x293220);}}function _0x36a58e(_0x41d325){try{_0x5d97db(_0x35d958['throw'](_0x41d325));}catch(_0x435a88){_0x28dfc3(_0x435a88);}}function _0x5d97db(_0x37b0f9){_0x37b0f9[a7_0x9a07('0x9')]?_0x5367e6(_0x37b0f9[a7_0x9a07('0x13')]):_0x582b4a(_0x37b0f9[a7_0x9a07('0x13')])[a7_0x9a07('0x2e')](_0x20466e,_0x36a58e);}_0x5d97db((_0x35d958=_0x35d958[a7_0x9a07('0x1d')](_0x477154,_0x49b534||[]))[a7_0x9a07('0x2')]());});};Object[a7_0x9a07('0x10')](exports,'__esModule',{'value':!![]});exports['createStartRequest']=exports[a7_0x9a07('0x22')]=void 0x0;const axios_1=require('../../../utilities/axios');const environment_1=require(a7_0x9a07('0x31'));const metric_logger_1=require('../../../utilities/metric-logger');const serializer_overrides_1=require(a7_0x9a07('0x33'));const {output}=require(a7_0x9a07('0x28'));class DistributedExecutionApi{constructor(_0x3494d0){this['apiAxiosInstance']=(0x0,axios_1[a7_0x9a07('0x23')])(_0x3494d0);}[a7_0x9a07('0x24')](_0x1628fb){var _0x2f7623;return __awaiter(this,void 0x0,void 0x0,function*(){const _0x39b4c9=(0x0,metric_logger_1[a7_0x9a07('0x8')])('dteStart');let _0x351090;if(environment_1[a7_0x9a07('0x1c')]){output[a7_0x9a07('0x3')]({'title':a7_0x9a07('0x1'),'bodyLines':[JSON['stringify'](_0x1628fb,null,0x2)]});}try{_0x351090=yield(0x0,axios_1['axiosMultipleTries'])(()=>this[a7_0x9a07('0x17')][a7_0x9a07('0xd')](a7_0x9a07('0x19'),_0x1628fb));_0x39b4c9[a7_0x9a07('0x34')]((0x0,metric_logger_1[a7_0x9a07('0x7')])(_0x351090));}catch(_0x19106a){_0x39b4c9[a7_0x9a07('0x34')](((_0x2f7623=_0x19106a===null||_0x19106a===void 0x0?void 0x0:_0x19106a[a7_0x9a07('0xe')])===null||_0x2f7623===void 0x0?void 0x0:_0x2f7623[a7_0x9a07('0x14')])?(0x0,metric_logger_1['mapRespToPerfEntry'])(_0x19106a['axiosException']['response']):metric_logger_1[a7_0x9a07('0xb')]);throw _0x19106a;}if(!_0x351090['data'][a7_0x9a07('0x21')]){throw new Error(a7_0x9a07('0x20'));}if(_0x351090[a7_0x9a07('0x6')]['error']){throw new Error(_0x351090[a7_0x9a07('0x6')][a7_0x9a07('0x1e')]);}return _0x351090[a7_0x9a07('0x6')]['id'];});}['status'](_0x5ed279){var _0x328e0a;return __awaiter(this,void 0x0,void 0x0,function*(){const _0x4fe0ea=(0x0,metric_logger_1[a7_0x9a07('0x8')])(a7_0x9a07('0xa'));try{const _0x171bd0=yield(0x0,axios_1[a7_0x9a07('0x18')])(()=>this[a7_0x9a07('0x17')][a7_0x9a07('0xd')](a7_0x9a07('0x2c'),{'id':_0x5ed279}));_0x4fe0ea[a7_0x9a07('0x34')]((0x0,metric_logger_1[a7_0x9a07('0x7')])(_0x171bd0));return _0x171bd0[a7_0x9a07('0x6')];}catch(_0x5867f0){_0x4fe0ea[a7_0x9a07('0x34')](((_0x328e0a=_0x5867f0===null||_0x5867f0===void 0x0?void 0x0:_0x5867f0[a7_0x9a07('0xe')])===null||_0x328e0a===void 0x0?void 0x0:_0x328e0a[a7_0x9a07('0x14')])?(0x0,metric_logger_1[a7_0x9a07('0x7')])(_0x5867f0[a7_0x9a07('0xe')][a7_0x9a07('0x14')]):metric_logger_1[a7_0x9a07('0xb')]);output[a7_0x9a07('0x1e')]({'title':_0x5867f0[a7_0x9a07('0x1b')]});process[a7_0x9a07('0x15')](0x1);}});}[a7_0x9a07('0x5')](_0x4d3366,_0x1b61c9){var _0x3234bf;return __awaiter(this,void 0x0,void 0x0,function*(){const _0x36a873=(0x0,metric_logger_1[a7_0x9a07('0x8')])(a7_0x9a07('0x35'));if(environment_1['VERBOSE_LOGGING']){output[a7_0x9a07('0x3')]({'title':a7_0x9a07('0x4'),'bodyLines':[a7_0x9a07('0x32')+_0x4d3366,a7_0x9a07('0x16')+_0x1b61c9]});}try{const _0x455ca1=yield(0x0,axios_1[a7_0x9a07('0x18')])(()=>this[a7_0x9a07('0x17')][a7_0x9a07('0xd')]('/nx-cloud/executions/complete-run-group',{'runGroup':_0x4d3366,'criticalErrorMessage':_0x1b61c9}),0x3);_0x36a873[a7_0x9a07('0x34')]((0x0,metric_logger_1['mapRespToPerfEntry'])(_0x455ca1));}catch(_0x51110d){_0x36a873['recordMetric'](((_0x3234bf=_0x51110d===null||_0x51110d===void 0x0?void 0x0:_0x51110d[a7_0x9a07('0xe')])===null||_0x3234bf===void 0x0?void 0x0:_0x3234bf['response'])?(0x0,metric_logger_1[a7_0x9a07('0x7')])(_0x51110d['axiosException'][a7_0x9a07('0x14')]):metric_logger_1[a7_0x9a07('0xb')]);}});}}exports['DistributedExecutionApi']=DistributedExecutionApi;function createStartRequest(_0x5e1548,_0x22f5b4,_0x3e0717){const _0xb52c7=_0x22f5b4[a7_0x9a07('0x27')](_0x3cff70=>{return _0x3cff70[a7_0x9a07('0x27')](_0x1a9520=>{return{'taskId':_0x1a9520['id'],'hash':_0x1a9520[a7_0x9a07('0x29')],'projectName':_0x1a9520[a7_0x9a07('0x26')][a7_0x9a07('0x2a')],'target':_0x1a9520['target'][a7_0x9a07('0x26')],'configuration':_0x1a9520[a7_0x9a07('0x26')][a7_0x9a07('0xf')]||null,'params':(0x0,serializer_overrides_1[a7_0x9a07('0x1f')])(_0x1a9520)};});});const _0x820e91={'command':(0x0,environment_1[a7_0x9a07('0x2f')])(),'branch':(0x0,environment_1[a7_0x9a07('0x1a')])(),'runGroup':_0x5e1548,'tasks':_0xb52c7,'maxParallel':calculateMaxParallel(_0x3e0717)};if(environment_1[a7_0x9a07('0x11')]){_0x820e91[a7_0x9a07('0x0')]=environment_1[a7_0x9a07('0x11')];}if(!environment_1['NX_CLOUD_DISTRIBUTED_EXECUTION_STOP_AGENTS_ON_FAILURE']){_0x820e91[a7_0x9a07('0xc')]=![];}return _0x820e91;}exports[a7_0x9a07('0x36')]=createStartRequest;function calculateMaxParallel(_0x3f762f){if(_0x3f762f[a7_0x9a07('0x25')]===a7_0x9a07('0x2b')||_0x3f762f['parallel']===![]){return 0x1;}else if(_0x3f762f['parallel']===a7_0x9a07('0x2d')||_0x3f762f[a7_0x9a07('0x25')]===!![]){return Number(_0x3f762f[a7_0x9a07('0x30')]||0x3);}else if(_0x3f762f['parallel']===undefined){return _0x3f762f[a7_0x9a07('0x30')]?Number(_0x3f762f[a7_0x9a07('0x30')]):0x3;}else{return Number(_0x3f762f['parallel'])||0x3;}}
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createStartRequest = exports.DistributedExecutionApi = void 0;
13
+ const axios_1 = require("../../../utilities/axios");
14
+ const environment_1 = require("../../../utilities/environment");
15
+ const metric_logger_1 = require("../../../utilities/metric-logger");
16
+ const serializer_overrides_1 = require("../../../utilities/serializer-overrides");
17
+ const { output } = require('../../../utilities/nx-imports');
18
+ class DistributedExecutionApi {
19
+ constructor(options) {
20
+ this.apiAxiosInstance = (0, axios_1.createApiAxiosInstance)(options);
21
+ }
22
+ start(params) {
23
+ var _a;
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ const recorder = (0, metric_logger_1.createMetricRecorder)('dteStart');
26
+ let resp;
27
+ if (environment_1.VERBOSE_LOGGING) {
28
+ output.note({
29
+ title: 'Starting a distributed execution',
30
+ bodyLines: [JSON.stringify(params, null, 2)],
31
+ });
32
+ }
33
+ try {
34
+ resp = yield (0, axios_1.axiosMultipleTries)(() => this.apiAxiosInstance.post('/nx-cloud/executions/start', params));
35
+ recorder.recordMetric((0, metric_logger_1.mapRespToPerfEntry)(resp));
36
+ }
37
+ catch (e) {
38
+ recorder.recordMetric(((_a = e === null || e === void 0 ? void 0 : e.axiosException) === null || _a === void 0 ? void 0 : _a.response)
39
+ ? (0, metric_logger_1.mapRespToPerfEntry)(e.axiosException.response)
40
+ : metric_logger_1.RUNNER_FAILURE_PERF_ENTRY);
41
+ throw e;
42
+ }
43
+ if (!resp.data.enabled) {
44
+ throw new Error(`Workspace is disabled. Cannot perform distributed task executions.`);
45
+ }
46
+ if (resp.data.error) {
47
+ throw new Error(resp.data.error);
48
+ }
49
+ return resp.data.id;
50
+ });
51
+ }
52
+ status(id) {
53
+ var _a;
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ const recorder = (0, metric_logger_1.createMetricRecorder)('dteStatus');
56
+ try {
57
+ const resp = yield (0, axios_1.axiosMultipleTries)(() => this.apiAxiosInstance.post('/nx-cloud/executions/status', {
58
+ id,
59
+ }));
60
+ recorder.recordMetric((0, metric_logger_1.mapRespToPerfEntry)(resp));
61
+ return resp.data;
62
+ }
63
+ catch (e) {
64
+ recorder.recordMetric(((_a = e === null || e === void 0 ? void 0 : e.axiosException) === null || _a === void 0 ? void 0 : _a.response)
65
+ ? (0, metric_logger_1.mapRespToPerfEntry)(e.axiosException.response)
66
+ : metric_logger_1.RUNNER_FAILURE_PERF_ENTRY);
67
+ output.error({
68
+ title: e.message,
69
+ });
70
+ process.exit(1);
71
+ }
72
+ });
73
+ }
74
+ completeRunGroupWithError(runGroup, error) {
75
+ var _a;
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ const recorder = (0, metric_logger_1.createMetricRecorder)('completeRunGroup');
78
+ if (environment_1.VERBOSE_LOGGING) {
79
+ output.note({
80
+ title: 'Completing run group with an error',
81
+ bodyLines: [`runGroup: ${runGroup}`, `error: ${error}`],
82
+ });
83
+ }
84
+ try {
85
+ const resp = yield (0, axios_1.axiosMultipleTries)(() => this.apiAxiosInstance.post('/nx-cloud/executions/complete-run-group', {
86
+ runGroup: runGroup,
87
+ criticalErrorMessage: error,
88
+ }), 3);
89
+ recorder.recordMetric((0, metric_logger_1.mapRespToPerfEntry)(resp));
90
+ }
91
+ catch (e) {
92
+ recorder.recordMetric(((_a = e === null || e === void 0 ? void 0 : e.axiosException) === null || _a === void 0 ? void 0 : _a.response)
93
+ ? (0, metric_logger_1.mapRespToPerfEntry)(e.axiosException.response)
94
+ : metric_logger_1.RUNNER_FAILURE_PERF_ENTRY);
95
+ }
96
+ });
97
+ }
98
+ }
99
+ exports.DistributedExecutionApi = DistributedExecutionApi;
100
+ function createStartRequest(runGroup, task, options) {
101
+ const tasksToExecute = task.map((arr) => {
102
+ return arr.map((t) => {
103
+ return {
104
+ taskId: t.id,
105
+ hash: t.hash,
106
+ projectName: t.target.project,
107
+ target: t.target.target,
108
+ configuration: t.target.configuration || null,
109
+ params: (0, serializer_overrides_1.serializeOverrides)(t),
110
+ };
111
+ });
112
+ });
113
+ const request = {
114
+ command: (0, environment_1.parseCommand)(),
115
+ branch: (0, environment_1.getBranch)(),
116
+ runGroup,
117
+ tasks: tasksToExecute,
118
+ maxParallel: calculateMaxParallel(options),
119
+ };
120
+ if (environment_1.NX_CLOUD_DISTRIBUTED_EXECUTION_AGENT_COUNT) {
121
+ request.agentCount = environment_1.NX_CLOUD_DISTRIBUTED_EXECUTION_AGENT_COUNT;
122
+ }
123
+ if (!environment_1.NX_CLOUD_DISTRIBUTED_EXECUTION_STOP_AGENTS_ON_FAILURE) {
124
+ request.stopAgentsOnFailure = false;
125
+ }
126
+ return request;
127
+ }
128
+ exports.createStartRequest = createStartRequest;
129
+ function calculateMaxParallel(options) {
130
+ if (options.parallel === 'false' || options.parallel === false) {
131
+ return 1;
132
+ }
133
+ else if (options.parallel === 'true' || options.parallel === true) {
134
+ return Number(options.maxParallel || 3);
135
+ }
136
+ else if (options.parallel === undefined) {
137
+ return options.maxParallel ? Number(options.maxParallel) : 3;
138
+ }
139
+ else {
140
+ return Number(options.parallel) || 3;
141
+ }
142
+ }
143
+ //# sourceMappingURL=distributed-execution.api.js.map