ws_workunitsService.cpp 155 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "ws_workunitsService.hpp"
  14. #include "ws_fs.hpp"
  15. #include "jlib.hpp"
  16. #include "jflz.hpp"
  17. #include "daclient.hpp"
  18. #include "dalienv.hpp"
  19. #include "dadfs.hpp"
  20. #include "daaudit.hpp"
  21. #include "exception_util.hpp"
  22. #include "wujobq.hpp"
  23. #include "eventqueue.hpp"
  24. #include "fileview.hpp"
  25. #include "hqlerror.hpp"
  26. #include "sacmd.hpp"
  27. #include "wuwebview.hpp"
  28. #include "portlist.h"
  29. #include "dllserver.hpp"
  30. #include "schedulectrl.hpp"
  31. #include "scheduleread.hpp"
  32. #include "dadfs.hpp"
  33. #include "dfuwu.hpp"
  34. #include "thorplugin.hpp"
  35. #include "roxiecontrol.hpp"
  36. #include "package.h"
  37. #ifdef _USE_ZLIB
  38. #include "zcrypt.hpp"
  39. #endif
  40. #define ESP_WORKUNIT_DIR "workunits/"
  41. #define SDS_LOCK_TIMEOUT (5*60*1000) // 5 mins
  42. const unsigned CHECK_QUERY_STATUS_THREAD_POOL_SIZE = 25;
  43. class ExecuteExistingQueryInfo
  44. {
  45. public:
  46. ExecuteExistingQueryInfo(IConstWorkUnit *cw)
  47. {
  48. const char *name = cw->queryJobName();
  49. const char *div = strchr(name, '.');
  50. if (div)
  51. {
  52. queryset.set(name, div-name);
  53. query.set(div+1);
  54. }
  55. }
  56. public:
  57. StringAttr queryset;
  58. StringAttr query;
  59. };
  60. typedef enum _WuActionType
  61. {
  62. ActionDelete=0,
  63. ActionProtect,
  64. ActionAbort,
  65. ActionRestore,
  66. ActionEventSchedule,
  67. ActionEventDeschedule,
  68. ActionChangeState,
  69. ActionPause,
  70. ActionPauseNow,
  71. ActionResume,
  72. ActionUnknown
  73. } WsWuActionType;
  74. void setActionResult(const char* wuid, int action, const char* result, StringBuffer& strAction, IArrayOf<IConstWUActionResult>* results)
  75. {
  76. if (!results || !wuid || !*wuid || !result || !*result)
  77. return;
  78. switch(action)
  79. {
  80. case ActionDelete:
  81. {
  82. strAction = "Delete";
  83. break;
  84. }
  85. case ActionProtect:
  86. {
  87. strAction = "Protect";
  88. break;
  89. }
  90. case ActionAbort:
  91. {
  92. strAction = "Abort";
  93. break;
  94. }
  95. case ActionRestore:
  96. {
  97. strAction = "Restore";
  98. break;
  99. }
  100. case ActionEventSchedule:
  101. {
  102. strAction = "EventSchedule";
  103. break;
  104. }
  105. case ActionEventDeschedule:
  106. {
  107. strAction = "EventDeschedule";
  108. break;
  109. }
  110. case ActionChangeState:
  111. {
  112. strAction = "ChangeState";
  113. break;
  114. }
  115. case ActionPause:
  116. {
  117. strAction = "Pause";
  118. break;
  119. }
  120. case ActionPauseNow:
  121. {
  122. strAction = "PauseNow";
  123. break;
  124. }
  125. case ActionResume:
  126. {
  127. strAction = "Resume";
  128. break;
  129. }
  130. default:
  131. {
  132. strAction = "Unknown";
  133. break;
  134. }
  135. }
  136. Owned<IEspWUActionResult> res = createWUActionResult("", "");
  137. res->setWuid(wuid);
  138. res->setAction(strAction.str());
  139. res->setResult(result);
  140. results->append(*res.getClear());
  141. }
  142. bool doAction(IEspContext& context, StringArray& wuids, int action, IProperties* params, IArrayOf<IConstWUActionResult>* results)
  143. {
  144. if (!wuids.length())
  145. return true;
  146. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  147. bool bAllSuccess = true;
  148. for(aindex_t i=0; i<wuids.length();i++)
  149. {
  150. StringBuffer strAction;
  151. StringBuffer wuidStr = wuids.item(i);
  152. const char* wuid = wuidStr.trim().str();
  153. if (isEmpty(wuid))
  154. {
  155. WARNLOG("Empty Workunit ID");
  156. continue;
  157. }
  158. try
  159. {
  160. if (!looksLikeAWuid(wuid))
  161. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid Workunit ID: %s", wuid);
  162. if ((action == ActionRestore) || (action == ActionEventDeschedule))
  163. {
  164. switch(action)
  165. {
  166. case ActionRestore:
  167. {
  168. SocketEndpoint ep;
  169. if (params->hasProp("sashaServerIP"))
  170. ep.set(params->queryProp("sashaServerIP"), params->getPropInt("sashaServerPort"));
  171. else
  172. getSashaNode(ep);
  173. Owned<ISashaCommand> cmd = createSashaCommand();
  174. cmd->setAction(SCA_RESTORE);
  175. cmd->addId(wuid);
  176. Owned<INode> node = createINode(ep);
  177. if (!node)
  178. throw MakeStringException(ECLWATCH_INODE_NOT_FOUND,"INode not found.");
  179. StringBuffer s;
  180. if (!cmd->send(node, 1*60*1000))
  181. throw MakeStringException(ECLWATCH_CANNOT_CONNECT_ARCHIVE_SERVER,"Cannot connect to Archive server at %s.", ep.getUrlStr(s).str());
  182. if (cmd->numIds()==0)
  183. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT,"Could not Archive/restore %s",wuid);
  184. StringBuffer reply;
  185. cmd->getId(0,reply);
  186. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wuid);
  187. ensureWsWorkunitAccess(context, wuid, SecAccess_Write);
  188. break;
  189. }
  190. case ActionEventDeschedule:
  191. if (!context.validateFeatureAccess(OWN_WU_ACCESS, SecAccess_Full, false)
  192. || !context.validateFeatureAccess(OTHERS_WU_ACCESS, SecAccess_Full, false))
  193. ensureWsWorkunitAccess(context, wuid, SecAccess_Full);
  194. descheduleWorkunit(wuid);
  195. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wuid);
  196. break;
  197. }
  198. }
  199. else
  200. {
  201. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  202. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid, false);
  203. if(!cw)
  204. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.",wuid);
  205. if ((action == ActionDelete) && (cw->getState() == WUStateWait))
  206. throw MakeStringException(ECLWATCH_CANNOT_DELETE_WORKUNIT,"Cannot delete a workunit which is in a 'Wait' status.");
  207. switch(action)
  208. {
  209. case ActionPause:
  210. {
  211. ensureWsWorkunitAccess(context, *cw, SecAccess_Full);
  212. WorkunitUpdate wu(&cw->lock());
  213. wu->setAction(WUActionPause);
  214. break;
  215. }
  216. case ActionPauseNow:
  217. {
  218. ensureWsWorkunitAccess(context, *cw, SecAccess_Full);
  219. WorkunitUpdate wu(&cw->lock());
  220. wu->setAction(WUActionPauseNow);
  221. break;
  222. }
  223. case ActionResume:
  224. {
  225. ensureWsWorkunitAccess(context, *cw, SecAccess_Full);
  226. WorkunitUpdate wu(&cw->lock());
  227. wu->setAction(WUActionResume);
  228. break;
  229. }
  230. case ActionDelete:
  231. ensureWsWorkunitAccess(context, *cw, SecAccess_Full);
  232. {
  233. int state = cw->getState();
  234. switch (state)
  235. {
  236. case WUStateWait:
  237. case WUStateAborted:
  238. case WUStateCompleted:
  239. case WUStateFailed:
  240. case WUStateArchived:
  241. case WUStateCompiled:
  242. case WUStateUploadingFiles:
  243. break;
  244. default:
  245. {
  246. WorkunitUpdate wu(&cw->lock());
  247. wu->setState(WUStateFailed);
  248. }
  249. }
  250. cw.clear();
  251. factory->deleteWorkUnit(wuid);
  252. AuditSystemAccess(context.queryUserId(), true, "Deleted %s", wuid);
  253. }
  254. break;
  255. case ActionAbort:
  256. ensureWsWorkunitAccess(context, *cw, SecAccess_Full);
  257. {
  258. if (cw->getState() == WUStateWait)
  259. {
  260. WorkunitUpdate wu(&cw->lock());
  261. wu->deschedule();
  262. wu->setState(WUStateAborted);
  263. }
  264. else
  265. secAbortWorkUnit(wuid, *context.querySecManager(), *context.queryUser());
  266. AuditSystemAccess(context.queryUserId(), true, "Aborted %s", wuid);
  267. }
  268. break;
  269. case ActionProtect:
  270. cw->protect(!params || params->getPropBool("Protect",true));
  271. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wuid);
  272. break;
  273. case ActionChangeState:
  274. {
  275. if (params)
  276. {
  277. WUState state = (WUState) params->getPropInt("State");
  278. if (state > WUStateUnknown && state < WUStateSize)
  279. {
  280. WorkunitUpdate wu(&cw->lock());
  281. wu->setState(state);
  282. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wuid);
  283. }
  284. }
  285. }
  286. break;
  287. case ActionEventSchedule:
  288. {
  289. WorkunitUpdate wu(&cw->lock());
  290. wu->schedule();
  291. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wuid);
  292. }
  293. break;
  294. }
  295. }
  296. setActionResult(wuid, action, "Success", strAction, results);
  297. }
  298. catch (IException *e)
  299. {
  300. bAllSuccess = false;
  301. StringBuffer eMsg;
  302. StringBuffer failedMsg("Failed: ");
  303. setActionResult(wuid, action, failedMsg.append(e->errorMessage(eMsg)).str(), strAction, results);
  304. WARNLOG("Failed to %s for workunit: %s, %s", strAction.str(), wuid, eMsg.str());
  305. AuditSystemAccess(context.queryUserId(), false, "Failed to %s %s", strAction.str(), wuid);
  306. e->Release();
  307. continue;
  308. }
  309. catch (...)
  310. {
  311. bAllSuccess = false;
  312. StringBuffer failedMsg;
  313. failedMsg.appendf("Unknown exception");
  314. setActionResult(wuid, action, failedMsg.str(), strAction, results);
  315. WARNLOG("Failed to %s for workunit: %s, %s", strAction.str(), wuid, failedMsg.str());
  316. AuditSystemAccess(context.queryUserId(), false, "Failed to %s %s", strAction.str(), wuid);
  317. continue;
  318. }
  319. }
  320. int timeToWait = 0;
  321. if (params)
  322. timeToWait = params->getPropInt("BlockTillFinishTimer");
  323. if (timeToWait != 0)
  324. {
  325. for(aindex_t i=0; i<wuids.length();i++)
  326. {
  327. const char* wuid=wuids.item(i);
  328. if (isEmpty(wuid))
  329. continue;
  330. waitForWorkUnitToComplete(wuid, timeToWait);
  331. }
  332. }
  333. return bAllSuccess;
  334. }
  335. static void checkUpdateQuerysetLibraries()
  336. {
  337. Owned<IRemoteConnection> globalLock = querySDS().connect("/QuerySets/", myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  338. if (!globalLock)
  339. return;
  340. IPropertyTree *root = globalLock->queryRoot();
  341. if (!root)
  342. return;
  343. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  344. Owned<IPropertyTreeIterator> querySets = root->getElements("QuerySet");
  345. ForEach(*querySets)
  346. {
  347. IPropertyTree &querySet = querySets->query();
  348. if (querySet.hasProp("@updatedLibraries")) //only need to do this once, then publish and copy will keep up to date
  349. continue;
  350. Owned<IPropertyTreeIterator> queries = querySet.getElements("Query");
  351. ForEach(*queries)
  352. {
  353. IPropertyTree &query = queries->query();
  354. if (query.hasProp("@libCount"))
  355. continue;
  356. const char *wuid = query.queryProp("@wuid");
  357. if (!wuid || !*wuid)
  358. continue;
  359. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid, false);
  360. if (!cw)
  361. continue;
  362. checkAddLibrariesToQueryEntry(&query, cw);
  363. }
  364. querySet.setPropBool("@updatedLibraries", true);
  365. }
  366. }
  367. MapStringTo<int> wuActionTable;
  368. void CWsWorkunitsEx::init(IPropertyTree *cfg, const char *process, const char *service)
  369. {
  370. if (!daliClientActive())
  371. {
  372. ERRLOG("No Dali Connection Active.");
  373. throw MakeStringException(-1, "No Dali Connection Active. Please Specify a Dali to connect to in you configuration file");
  374. }
  375. setPasswordsFromSDS();
  376. DBGLOG("Initializing %s service [process = %s]", service, process);
  377. checkUpdateQuerysetLibraries();
  378. refreshValidClusters();
  379. daliServers.set(cfg->queryProp("Software/EspProcess/@daliServers"));
  380. const char *computer = cfg->queryProp("Software/EspProcess/@computer");
  381. if (daliServers.isEmpty() || !computer || streq(computer, "localhost")) //otherwise can't assume environment "." netAddresses are the same as my address
  382. queryHostIP().getIpText(envLocalAddress);
  383. else
  384. {
  385. //a bit weird, but other netAddresses in the environment are not the same localhost as this server
  386. //use the address of the DALI
  387. const char *finger = daliServers.get();
  388. while (*finger && !strchr(":;,", *finger))
  389. envLocalAddress.append(*finger++);
  390. }
  391. wuActionTable.setValue("delete", ActionDelete);
  392. wuActionTable.setValue("abort", ActionAbort);
  393. wuActionTable.setValue("pausenow", ActionPauseNow);
  394. wuActionTable.setValue("pause", ActionPause);
  395. wuActionTable.setValue("resume", ActionResume);
  396. wuActionTable.setValue("protect", ActionProtect);
  397. wuActionTable.setValue("unprotect", ActionProtect);
  398. wuActionTable.setValue("restore", ActionRestore);
  399. wuActionTable.setValue("reschedule", ActionEventSchedule);
  400. wuActionTable.setValue("deschedule", ActionEventDeschedule);
  401. wuActionTable.setValue("settofailed", ActionChangeState);
  402. awusCacheMinutes = AWUS_CACHE_MIN_DEFAULT;
  403. VStringBuffer xpath("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/AWUsCacheMinutes", process, service);
  404. cfg->getPropInt(xpath.str(), awusCacheMinutes);
  405. xpath.setf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/serverForArchivedECLWU/@netAddress", process, service);
  406. if (cfg->hasProp(xpath.str()))
  407. {
  408. sashaServerIp.set(cfg->queryProp(xpath.str()));
  409. xpath.setf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/serverForArchivedECLWU/@port", process, service);
  410. sashaServerPort = cfg->getPropInt(xpath.str(), DEFAULT_SASHA_PORT);
  411. }
  412. maxRequestEntityLength = cfg->getPropInt("Software[1]/EspProcess[1]/EspProtocol[@type='http_protocol'][1]/@maxRequestEntityLength");
  413. directories.set(cfg->queryPropTree("Software/Directories"));
  414. const char *name = cfg->queryProp("Software/EspProcess/@name");
  415. getConfigurationDirectory(directories, "query", "esp", name ? name : "esp", queryDirectory);
  416. recursiveCreateDirectory(queryDirectory.str());
  417. xpath.setf("Software/EspProcess[@name=\"%s\"]/EspBinding[@service=\"%s\"]/Authenticate", process, service);
  418. Owned<IPropertyTree> authCFG = cfg->getPropTree(xpath.str());
  419. if(authCFG)
  420. authMethod.set(authCFG->queryProp("@method"));
  421. dataCache.setown(new DataCache(DATA_SIZE));
  422. archivedWuCache.setown(new ArchivedWuCache(AWUS_CACHE_SIZE));
  423. //Create a folder for temporarily holding gzip files by WUResultBin()
  424. Owned<IFile> tmpdir = createIFile(TEMPZIPDIR);
  425. if(!tmpdir->exists())
  426. tmpdir->createDirectory();
  427. recursiveCreateDirectory(ESP_WORKUNIT_DIR);
  428. m_sched.start();
  429. filesInUse.subscribe();
  430. //Start thread pool
  431. xpath.setf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/ClusterQueryStateThreadPoolSize", process, service);
  432. Owned<CClusterQueryStateThreadFactory> threadFactory = new CClusterQueryStateThreadFactory();
  433. clusterQueryStatePool.setown(createThreadPool("CheckAndSetClusterQueryState Thread Pool", threadFactory, NULL,
  434. cfg->getPropInt(xpath.str(), CHECK_QUERY_STATUS_THREAD_POOL_SIZE)));
  435. }
  436. void CWsWorkunitsEx::refreshValidClusters()
  437. {
  438. validClusters.kill();
  439. Owned<IStringIterator> it = getTargetClusters(NULL, NULL);
  440. ForEach(*it)
  441. {
  442. SCMStringBuffer s;
  443. IStringVal &val = it->str(s);
  444. bool* found = validClusters.getValue(val.str());
  445. if (!found || !*found)
  446. validClusters.setValue(val.str(), true);
  447. }
  448. }
  449. bool CWsWorkunitsEx::isValidCluster(const char *cluster)
  450. {
  451. if (!cluster || !*cluster)
  452. return false;
  453. CriticalBlock block(crit);
  454. bool* found = validClusters.getValue(cluster);
  455. if (found && *found)
  456. return true;
  457. if (validateTargetClusterName(cluster))
  458. {
  459. refreshValidClusters();
  460. return true;
  461. }
  462. return false;
  463. }
  464. bool CWsWorkunitsEx::onWUCreate(IEspContext &context, IEspWUCreateRequest &req, IEspWUCreateResponse &resp)
  465. {
  466. try
  467. {
  468. if (!context.validateFeatureAccess(OWN_WU_ACCESS, SecAccess_Write, false))
  469. throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "Failed to create workunit. Permission denied.");
  470. NewWsWorkunit wu(context);
  471. resp.updateWorkunit().setWuid(wu->queryWuid());
  472. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wu->queryWuid());
  473. }
  474. catch(IException* e)
  475. {
  476. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  477. }
  478. return true;
  479. }
  480. static bool origValueChanged(const char *newValue, const char *origValue, StringBuffer &s, bool nillable=true)
  481. {
  482. if (!nillable && isEmpty(newValue))
  483. return false;
  484. if(newValue && origValue)
  485. {
  486. if (!streq(origValue, newValue))
  487. {
  488. s.append(newValue).trim();
  489. return true;
  490. }
  491. return false;
  492. }
  493. if (newValue)
  494. {
  495. s.append(newValue).trim();
  496. return true;
  497. }
  498. return (origValue!=NULL);
  499. }
  500. bool CWsWorkunitsEx::onWUUpdate(IEspContext &context, IEspWUUpdateRequest &req, IEspWUUpdateResponse &resp)
  501. {
  502. try
  503. {
  504. StringBuffer wuid = req.getWuid();
  505. WsWuHelpers::checkAndTrimWorkunit("WUUpdate", wuid);
  506. ensureWsWorkunitAccess(context, wuid.str(), SecAccess_Write);
  507. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  508. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  509. if(!cw)
  510. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  511. if(req.getProtected() != req.getProtectedOrig())
  512. {
  513. cw->protect(req.getProtected());
  514. cw.clear();
  515. cw.setown(factory->openWorkUnit(wuid.str(), false));
  516. if(!cw)
  517. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  518. }
  519. if ((req.getState() == WUStateRunning)||(req.getState() == WUStateDebugPaused)||(req.getState() == WUStateDebugRunning))
  520. {
  521. WsWuInfo winfo(context, cw);
  522. winfo.getInfo(resp.updateWorkunit(), WUINFO_All);
  523. resp.setRedirectUrl(StringBuffer("/WsWorkunits/WUInfo?Wuid=").append(wuid).str());
  524. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wuid.str());
  525. return true;
  526. }
  527. WorkunitUpdate wu(&cw->lock());
  528. if(!req.getState_isNull() && (req.getStateOrig_isNull() || req.getState() != req.getStateOrig()))
  529. {
  530. if (!req.getStateOrig_isNull() && cw->getState() != (WUState) req.getStateOrig())
  531. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT, "Cannot update workunit %s because its state has been changed internally. Please refresh the page and try again.", wuid.str());
  532. WUState state = (WUState) req.getState();
  533. if(state < WUStateSize)
  534. wu->setState(state);
  535. }
  536. StringBuffer s;
  537. if (origValueChanged(req.getJobname(), req.getJobnameOrig(), s))
  538. wu->setJobName(s.trim().str());
  539. if (origValueChanged(req.getDescription(), req.getDescriptionOrig(), s.clear()))
  540. wu->setDebugValue("description", (req.getDescription() && *req.getDescription()) ? s.trim().str() : NULL, true);
  541. double version = context.getClientVersion();
  542. if (version > 1.04)
  543. {
  544. if (origValueChanged(req.getClusterSelection(), req.getClusterOrig(), s.clear(), false))
  545. {
  546. if (!isValidCluster(s.str()))
  547. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_NAME, "Invalid cluster name: %s", s.str());
  548. if (req.getState() == WUStateBlocked)
  549. switchWorkUnitQueue(wu.get(), s.str());
  550. else if ((req.getState() != WUStateSubmitted) && (req.getState() != WUStateRunning) && (req.getState() != WUStateDebugPaused) && (req.getState() != WUStateDebugRunning))
  551. wu->setClusterName(s.str());
  552. }
  553. }
  554. WsWuHelpers::setXmlParameters(wu, req.getXmlParams(), (req.getAction()==WUActionExecuteExisting));
  555. if (notEmpty(req.getQueryText()))
  556. {
  557. Owned<IWUQuery> query=wu->updateQuery();
  558. query->setQueryText(req.getQueryText());
  559. }
  560. if (version > 1.34 && notEmpty(req.getQueryMainDefinition()))
  561. {
  562. Owned<IWUQuery> query=wu->updateQuery();
  563. query->setQueryMainDefinition(req.getQueryMainDefinition());
  564. }
  565. if (!req.getResultLimit_isNull())
  566. wu->setResultLimit(req.getResultLimit());
  567. if (!req.getAction_isNull())
  568. {
  569. WUAction action = (WUAction) req.getAction();
  570. if(action < WUActionSize)
  571. wu->setAction(action);
  572. }
  573. if (!req.getPriorityClass_isNull())
  574. {
  575. WUPriorityClass priority = (WUPriorityClass) req.getPriorityClass();
  576. if(priority<PriorityClassSize)
  577. wu->setPriority(priority);
  578. }
  579. if (!req.getPriorityLevel_isNull())
  580. wu->setPriorityLevel(req.getPriorityLevel());
  581. if (origValueChanged(req.getScope(), req.getScopeOrig(), s.clear(), false))
  582. wu->setWuScope(s.str());
  583. ForEachItemIn(di, req.getDebugValues())
  584. {
  585. IConstDebugValue& item = req.getDebugValues().item(di);
  586. if (notEmpty(item.getName()))
  587. wu->setDebugValue(item.getName(), item.getValue(), true);
  588. }
  589. ForEachItemIn(ai, req.getApplicationValues())
  590. {
  591. IConstApplicationValue& item=req.getApplicationValues().item(ai);
  592. if(notEmpty(item.getApplication()) && notEmpty(item.getName()))
  593. wu->setApplicationValue(item.getApplication(), item.getName(), item.getValue(), true);
  594. }
  595. wu->commit();
  596. wu.clear();
  597. WsWuInfo winfo(context, cw);
  598. winfo.getInfo(resp.updateWorkunit(), WUINFO_All);
  599. StringBuffer thorSlaveIP;
  600. if (version > 1.24 && notEmpty(req.getThorSlaveIP()))
  601. thorSlaveIP = req.getThorSlaveIP();
  602. if (thorSlaveIP.length() > 0)
  603. {
  604. StringBuffer url;
  605. url.appendf("/WsWorkunits/WUInfo?Wuid=%s&ThorSlaveIP=%s", wuid.str(), thorSlaveIP.str());
  606. resp.setRedirectUrl(url.str());
  607. }
  608. else
  609. resp.setRedirectUrl(StringBuffer("/WsWorkunits/WUInfo?Wuid=").append(wuid).str());
  610. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wuid.str());
  611. }
  612. catch(IException* e)
  613. {
  614. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  615. }
  616. return true;
  617. }
  618. bool CWsWorkunitsEx::onWUCreateAndUpdate(IEspContext &context, IEspWUUpdateRequest &req, IEspWUUpdateResponse &resp)
  619. {
  620. try
  621. {
  622. if (!context.validateFeatureAccess(OWN_WU_ACCESS, SecAccess_Write, false))
  623. throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "Failed to create workunit. Permission denied.");
  624. NewWsWorkunit wu(context);
  625. req.setWuid(wu->queryWuid());
  626. }
  627. catch(IException* e)
  628. {
  629. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  630. }
  631. return onWUUpdate(context, req, resp);
  632. }
  633. static inline StringBuffer &appendUrlParameter(StringBuffer &url, const char *name, const char *value, bool &first)
  634. {
  635. if (notEmpty(value))
  636. {
  637. url.append(first ? '?' : '&').append(name).append('=').append(value);
  638. first=false;
  639. }
  640. return url;
  641. }
  642. bool CWsWorkunitsEx::onWUAction(IEspContext &context, IEspWUActionRequest &req, IEspWUActionResponse &resp)
  643. {
  644. try
  645. {
  646. StringBuffer sAction(req.getActionType());
  647. if (!sAction.length())
  648. throw MakeStringException(ECLWATCH_INVALID_INPUT,"Action not defined.");
  649. int *action=wuActionTable.getValue(sAction.toLowerCase().str());
  650. if (!action)
  651. throw MakeStringException(ECLWATCH_INVALID_INPUT,"Invalid Action '%s'.", sAction.str());
  652. Owned<IProperties> params = createProperties(true);
  653. params->setProp("BlockTillFinishTimer", req.getBlockTillFinishTimer());
  654. if (*action==ActionProtect)
  655. params->setProp("Protect", streq(sAction.str(), "protect"));
  656. if (*action==ActionChangeState && streq(sAction.str(), "settofailed"))
  657. params->setProp("State",4);
  658. if ((*action==ActionRestore) && !sashaServerIp.isEmpty())
  659. {
  660. params->setProp("sashaServerIP", sashaServerIp.get());
  661. params->setProp("sashaServerPort", sashaServerPort);
  662. }
  663. IArrayOf<IConstWUActionResult> results;
  664. if (doAction(context, req.getWuids(), *action, params, &results) && *action!=ActionDelete && checkRedirect(context))
  665. {
  666. StringBuffer redirect;
  667. if(req.getPageFrom() && strieq(req.getPageFrom(), "wuid"))
  668. redirect.append("/WsWorkunits/WUInfo?Wuid=").append(req.getWuids().item(0));
  669. else if (req.getPageFrom() && strieq(req.getPageFrom(), "scheduler"))
  670. {
  671. redirect.set("/WsWorkunits/WUShowScheduled");
  672. bool first=true;
  673. appendUrlParameter(redirect, "Cluster", req.getEventServer(), first);
  674. appendUrlParameter(redirect, "EventName", req.getEventName(), first);
  675. }
  676. else
  677. {
  678. redirect.append("/WsWorkunits/WUQuery");
  679. bool first=true;
  680. appendUrlParameter(redirect, "PageSize", req.getPageSize(), first);
  681. appendUrlParameter(redirect, "PageStartFrom", req.getCurrentPage(), first);
  682. appendUrlParameter(redirect, "Sortby", req.getSortby(), first);
  683. appendUrlParameter(redirect, "Descending", req.getDescending() ? "1" : "0", first);
  684. appendUrlParameter(redirect, "State", req.getState(), first);
  685. appendUrlParameter(redirect, "Cluster", req.getCluster(), first);
  686. appendUrlParameter(redirect, "Owner", req.getOwner(), first);
  687. appendUrlParameter(redirect, "StartDate", req.getStartDate(), first);
  688. appendUrlParameter(redirect, "EndDate", req.getEndDate(), first);
  689. appendUrlParameter(redirect, "ECL", req.getECL(), first);
  690. appendUrlParameter(redirect, "Jobname", req.getJobname(), first);
  691. }
  692. resp.setRedirectUrl(redirect.str());
  693. }
  694. else
  695. resp.setActionResults(results);
  696. }
  697. catch(IException* e)
  698. {
  699. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  700. }
  701. return true;
  702. }
  703. bool CWsWorkunitsEx::onWUDelete(IEspContext &context, IEspWUDeleteRequest &req, IEspWUDeleteResponse &resp)
  704. {
  705. try
  706. {
  707. IArrayOf<IConstWUActionResult> results;
  708. Owned<IProperties> params = createProperties(true);
  709. params->setProp("BlockTillFinishTimer", req.getBlockTillFinishTimer());
  710. if (!doAction(context,req.getWuids(), ActionDelete, params, &results))
  711. resp.setActionResults(results);
  712. }
  713. catch(IException* e)
  714. {
  715. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  716. }
  717. return true;
  718. }
  719. bool CWsWorkunitsEx::onWUAbort(IEspContext &context, IEspWUAbortRequest &req, IEspWUAbortResponse &resp)
  720. {
  721. try
  722. {
  723. IArrayOf<IConstWUActionResult> results;
  724. Owned<IProperties> params = createProperties(true);
  725. params->setProp("BlockTillFinishTimer", req.getBlockTillFinishTimer());
  726. if (!doAction(context,req.getWuids(), ActionAbort, params, &results))
  727. resp.setActionResults(results);
  728. }
  729. catch(IException* e)
  730. {
  731. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  732. }
  733. return true;
  734. }
  735. bool CWsWorkunitsEx::onWUProtect(IEspContext &context, IEspWUProtectRequest &req, IEspWUProtectResponse &resp)\
  736. {
  737. try
  738. {
  739. IArrayOf<IConstWUActionResult> results;
  740. Owned<IProperties> params(createProperties(true));
  741. params->setProp("Protect", req.getProtect());
  742. params->setProp("BlockTillFinishTimer", 0);
  743. if (!doAction(context,req.getWuids(), ActionProtect, params, &results))
  744. resp.setActionResults(results);
  745. }
  746. catch(IException* e)
  747. {
  748. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  749. }
  750. return true;
  751. }
  752. bool CWsWorkunitsEx::onWUResubmit(IEspContext &context, IEspWUResubmitRequest &req, IEspWUResubmitResponse &resp)
  753. {
  754. try
  755. {
  756. Owned<IMultiException> me = MakeMultiException();
  757. StringAttr wuid;
  758. StringArray wuids;
  759. double version = context.getClientVersion();
  760. IArrayOf<IEspResubmittedWU> resubmittedWUs;
  761. for(aindex_t i=0; i<req.getWuids().length();i++)
  762. {
  763. StringBuffer requestWuid = req.getWuids().item(i);
  764. WsWuHelpers::checkAndTrimWorkunit("WUResubmit", requestWuid);
  765. ensureWsWorkunitAccess(context, requestWuid.str(), SecAccess_Write);
  766. wuid.set(requestWuid.str());
  767. try
  768. {
  769. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  770. if(req.getCloneWorkunit() || req.getRecompile())
  771. {
  772. Owned<IConstWorkUnit> src(factory->openWorkUnit(wuid.str(), false));
  773. NewWsWorkunit wu(factory, context);
  774. wuid.set(wu->queryWuid());
  775. queryExtendedWU(wu)->copyWorkUnit(src, false);
  776. SCMStringBuffer token;
  777. wu->setSecurityToken(createToken(wuid.str(), context.queryUserId(), context.queryPassword(), token).str());
  778. }
  779. wuids.append(wuid.str());
  780. Owned<IConstWorkUnit> cw(factory->openWorkUnit(wuid.str(), false));
  781. if(!cw)
  782. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  783. //Don't allow resubmit of someone else's workunit
  784. if (context.querySecManager())
  785. {
  786. IUserDescriptor * owner = cw->queryUserDescriptor();
  787. if (!owner)
  788. throw MakeStringException(ECLWATCH_CANNOT_SUBMIT_WORKUNIT,"Workunit User Descriptor missing on %s", wuid.str());
  789. StringBuffer ownerUserName;
  790. owner->getUserName(ownerUserName);
  791. if (strcmp(context.queryUser()->getName(), ownerUserName.str()))
  792. throw MakeStringException(ECLWATCH_CANNOT_SUBMIT_WORKUNIT,"Cannot resubmit another user's workunit %s.", wuid.str());
  793. }
  794. WsWuHelpers::submitWsWorkunit(context, cw, NULL, NULL, 0, req.getRecompile(), req.getResetWorkflow(), false);
  795. if (version < 1.40)
  796. continue;
  797. Owned<IEspResubmittedWU> resubmittedWU = createResubmittedWU();
  798. resubmittedWU->setWUID(wuid.str());
  799. if (!streq(requestWuid.str(), wuid.str()))
  800. resubmittedWU->setParentWUID(requestWuid.str());
  801. resubmittedWUs.append(*resubmittedWU.getClear());
  802. }
  803. catch (IException *E)
  804. {
  805. me->append(*E);
  806. }
  807. catch (...)
  808. {
  809. me->append(*MakeStringException(0,"Unknown exception submitting %s",wuid.str()));
  810. }
  811. }
  812. if(me->ordinality())
  813. throw me.getLink();
  814. int timeToWait = req.getBlockTillFinishTimer();
  815. if (timeToWait != 0)
  816. {
  817. for(aindex_t i=0; i<wuids.length(); i++)
  818. waitForWorkUnitToComplete(wuids.item(i), timeToWait);
  819. }
  820. if (version >= 1.40)
  821. resp.setWUs(resubmittedWUs);
  822. if(wuids.length()==1)
  823. {
  824. resp.setRedirectUrl(StringBuffer("/WsWorkunits/WUInfo?Wuid=").append(wuids.item(0)));
  825. }
  826. }
  827. catch(IException* e)
  828. {
  829. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  830. }
  831. return true;
  832. }
  833. bool CWsWorkunitsEx::onWUPushEvent(IEspContext &context, IEspWUPushEventRequest &req, IEspWUPushEventResponse &resp)
  834. {
  835. try
  836. {
  837. const char *name = req.getEventName();
  838. const char *text = req.getEventText();
  839. const char *target = NULL;
  840. if (notEmpty(name) && notEmpty(text))
  841. {
  842. Owned<IScheduleEventPusher> pusher(getScheduleEventPusher());
  843. pusher->push(name, text, target);
  844. StringBuffer redirect("/WsWorkunits/WUShowScheduled");
  845. bool first=true;
  846. appendUrlParameter(redirect, "PushEventName", name, first);
  847. appendUrlParameter(redirect, "PushEventText", text, first);
  848. resp.setRedirectUrl(redirect.str());
  849. return true;
  850. }
  851. }
  852. catch(IException* e)
  853. {
  854. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  855. }
  856. return false;
  857. }
  858. bool CWsWorkunitsEx::onWUSchedule(IEspContext &context, IEspWUScheduleRequest &req, IEspWUScheduleResponse &resp)
  859. {
  860. try
  861. {
  862. StringBuffer wuid = req.getWuid();
  863. WsWuHelpers::checkAndTrimWorkunit("WUSchedule", wuid);
  864. const char* cluster = req.getCluster();
  865. if (isEmpty(cluster))
  866. throw MakeStringException(ECLWATCH_INVALID_INPUT,"No Cluster defined.");
  867. if (!isValidCluster(cluster))
  868. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_NAME, "Invalid cluster name: %s", cluster);
  869. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  870. WorkunitUpdate wu(factory->updateWorkUnit(wuid.str()));
  871. ensureWsWorkunitAccess(context, *wu.get(), SecAccess_Write);
  872. switch(wu->getState())
  873. {
  874. case WUStateDebugPaused:
  875. case WUStateDebugRunning:
  876. case WUStateRunning:
  877. case WUStateAborting:
  878. case WUStateBlocked:
  879. throw MakeStringException(ECLWATCH_CANNOT_SCHEDULE_WORKUNIT, "Cannot schedule the workunit. Workunit state is '%s'.", wu->queryStateDesc());
  880. }
  881. wu->clearExceptions();
  882. wu->setClusterName(cluster);
  883. if (notEmpty(req.getWhen()))
  884. {
  885. WsWuDateTime dt;
  886. dt.setString(req.getWhen());
  887. wu->setTimeScheduled(dt);
  888. }
  889. if(notEmpty(req.getSnapshot()))
  890. wu->setSnapshot(req.getSnapshot());
  891. wu->setState(WUStateScheduled);
  892. if (req.getMaxRunTime())
  893. wu->setDebugValueInt("maxRunTime", req.getMaxRunTime(), true);
  894. SCMStringBuffer token;
  895. wu->setSecurityToken(createToken(wuid.str(), context.queryUserId(), context.queryPassword(), token).str());
  896. AuditSystemAccess(context.queryUserId(), true, "Scheduled %s", wuid.str());
  897. }
  898. catch(IException* e)
  899. {
  900. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  901. }
  902. return true;
  903. }
  904. bool CWsWorkunitsEx::onWUSubmit(IEspContext &context, IEspWUSubmitRequest &req, IEspWUSubmitResponse &resp)
  905. {
  906. try
  907. {
  908. StringBuffer wuid = req.getWuid();
  909. WsWuHelpers::checkAndTrimWorkunit("WUSubmit", wuid);
  910. const char *cluster = req.getCluster();
  911. if (isEmpty(cluster))
  912. throw MakeStringException(ECLWATCH_INVALID_INPUT,"No Cluster defined.");
  913. if (!isValidCluster(cluster))
  914. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_NAME, "Invalid cluster name: %s", cluster);
  915. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  916. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  917. if(!cw)
  918. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  919. if (cw->getAction()==WUActionExecuteExisting)
  920. {
  921. ExecuteExistingQueryInfo info(cw);
  922. if (info.queryset.isEmpty() || info.query.isEmpty())
  923. {
  924. WorkunitUpdate wu(&cw->lock());
  925. throw WsWuHelpers::noteException(wu, MakeStringException(ECLWATCH_INVALID_INPUT,"Queryset and/or query not specified"));
  926. }
  927. WsWuHelpers::runWsWuQuery(context, cw, info.queryset.str(), info.query.str(), cluster, NULL);
  928. }
  929. else
  930. WsWuHelpers::submitWsWorkunit(context, cw, cluster, req.getSnapshot(), req.getMaxRunTime(), true, false, false);
  931. if (req.getBlockTillFinishTimer() != 0)
  932. waitForWorkUnitToComplete(wuid.str(), req.getBlockTillFinishTimer());
  933. resp.setRedirectUrl(StringBuffer("/WsWorkunits/WUInfo?Wuid=").append(wuid).str());
  934. }
  935. catch(IException* e)
  936. {
  937. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  938. }
  939. return true;
  940. }
  941. ErrorSeverity checkGetExceptionSeverity(CWUExceptionSeverity severity)
  942. {
  943. switch (severity)
  944. {
  945. case CWUExceptionSeverity_INFO:
  946. return SeverityInformation;
  947. case CWUExceptionSeverity_WARNING:
  948. return SeverityWarning;
  949. case CWUExceptionSeverity_ERROR:
  950. return SeverityError;
  951. case CWUExceptionSeverity_ALERT:
  952. return SeverityAlert;
  953. }
  954. throw MakeStringExceptionDirect(ECLWATCH_INVALID_INPUT,"invalid exception severity");
  955. }
  956. bool CWsWorkunitsEx::onWURun(IEspContext &context, IEspWURunRequest &req, IEspWURunResponse &resp)
  957. {
  958. try
  959. {
  960. const char *cluster = req.getCluster();
  961. if (notEmpty(cluster) && !isValidCluster(cluster))
  962. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_NAME, "Invalid cluster name: %s", cluster);
  963. StringBuffer wuidStr = req.getWuid();
  964. const char* runWuid = wuidStr.trim().str();
  965. StringBuffer wuid;
  966. ErrorSeverity severity = checkGetExceptionSeverity(req.getExceptionSeverity());
  967. if (runWuid && *runWuid)
  968. {
  969. if (!looksLikeAWuid(runWuid))
  970. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid Workunit ID: %s", runWuid);
  971. if (req.getCloneWorkunit())
  972. WsWuHelpers::runWsWorkunit(context, wuid, runWuid, cluster, req.getInput(), &req.getVariables(), &req.getDebugValues());
  973. else
  974. {
  975. WsWuHelpers::submitWsWorkunit(context, runWuid, cluster, NULL, 0, false, true, true, req.getInput(), &req.getVariables(), &req.getDebugValues());
  976. wuid.set(runWuid);
  977. }
  978. }
  979. else if (notEmpty(req.getQuerySet()) && notEmpty(req.getQuery()))
  980. WsWuHelpers::runWsWuQuery(context, wuid, req.getQuerySet(), req.getQuery(), cluster, req.getInput());
  981. else
  982. throw MakeStringException(ECLWATCH_MISSING_PARAMS,"Workunit or Query required");
  983. int timeToWait = req.getWait();
  984. if (timeToWait != 0)
  985. waitForWorkUnitToComplete(wuid.str(), timeToWait);
  986. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  987. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  988. if (!cw)
  989. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT,"Cannot open workunit %s.", wuid.str());
  990. resp.setState(cw->queryStateDesc());
  991. resp.setWuid(wuid.str());
  992. switch (cw->getState())
  993. {
  994. case WUStateCompleted:
  995. case WUStateFailed:
  996. case WUStateUnknown:
  997. {
  998. SCMStringBuffer result;
  999. unsigned flags = WorkUnitXML_SeverityTags;
  1000. if (req.getNoRootTag())
  1001. flags |= WorkUnitXML_NoRoot;
  1002. getFullWorkUnitResultsXML(context.queryUserId(), context.queryPassword(), cw.get(), result, flags, severity);
  1003. resp.setResults(result.str());
  1004. break;
  1005. }
  1006. default:
  1007. break;
  1008. }
  1009. }
  1010. catch(IException* e)
  1011. {
  1012. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1013. }
  1014. return true;
  1015. }
  1016. bool CWsWorkunitsEx::onWUWaitCompiled(IEspContext &context, IEspWUWaitRequest &req, IEspWUWaitResponse &resp)
  1017. {
  1018. try
  1019. {
  1020. StringBuffer wuid = req.getWuid();
  1021. WsWuHelpers::checkAndTrimWorkunit("WUWaitCompiled", wuid);
  1022. secWaitForWorkUnitToCompile(wuid.str(), *context.querySecManager(), *context.queryUser(), req.getWait());
  1023. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  1024. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  1025. if(!cw)
  1026. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  1027. resp.setStateID(cw->getState());
  1028. }
  1029. catch(IException* e)
  1030. {
  1031. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1032. }
  1033. return true;
  1034. }
  1035. bool CWsWorkunitsEx::onWUWaitComplete(IEspContext &context, IEspWUWaitRequest &req, IEspWUWaitResponse &resp)
  1036. {
  1037. try
  1038. {
  1039. StringBuffer wuid = req.getWuid();
  1040. WsWuHelpers::checkAndTrimWorkunit("WUWaitComplete", wuid);
  1041. resp.setStateID(secWaitForWorkUnitToComplete(wuid.str(), *context.querySecManager(), *context.queryUser(), req.getWait(), req.getReturnOnWait()));
  1042. }
  1043. catch(IException* e)
  1044. {
  1045. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1046. }
  1047. return true;
  1048. }
  1049. bool CWsWorkunitsEx::onWUCDebug(IEspContext &context, IEspWUDebugRequest &req, IEspWUDebugResponse &resp)
  1050. {
  1051. try
  1052. {
  1053. StringBuffer wuid = req.getWuid();
  1054. WsWuHelpers::checkAndTrimWorkunit("WUCDebug", wuid);
  1055. StringBuffer result;
  1056. secDebugWorkunit(wuid.str(), *context.querySecManager(), *context.queryUser(), req.getCommand(), result);
  1057. resp.setResult(result);
  1058. }
  1059. catch(IException* e)
  1060. {
  1061. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1062. }
  1063. return true;
  1064. }
  1065. bool CWsWorkunitsEx::onWUSyntaxCheckECL(IEspContext &context, IEspWUSyntaxCheckRequest &req, IEspWUSyntaxCheckResponse &resp)
  1066. {
  1067. try
  1068. {
  1069. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  1070. NewWsWorkunit wu(factory, context);
  1071. wu->setAction(WUActionCheck);
  1072. if(notEmpty(req.getModuleName()) && notEmpty(req.getAttributeName()))
  1073. {
  1074. wu->setApplicationValue("SyntaxCheck", "ModuleName", req.getModuleName(), true);
  1075. wu->setApplicationValue("SyntaxCheck", "AttributeName", req.getAttributeName(), true);
  1076. }
  1077. ForEachItemIn(di, req.getDebugValues())
  1078. {
  1079. IConstDebugValue& item=req.getDebugValues().item(di);
  1080. if(notEmpty(item.getName()))
  1081. wu->setDebugValue(item.getName(), item.getValue(), true);
  1082. }
  1083. wu.setQueryText(req.getECL());
  1084. StringAttr wuid(wu->queryWuid()); // NB queryWuid() not valid after workunit,clear()
  1085. wu->commit();
  1086. wu.clear();
  1087. WsWuHelpers::submitWsWorkunit(context, wuid.str(), req.getCluster(), req.getSnapshot(), 0, true, false, false);
  1088. waitForWorkUnitToComplete(wuid.str(), req.getTimeToWait());
  1089. Owned<IConstWorkUnit> cw(factory->openWorkUnit(wuid.str(), false));
  1090. WsWUExceptions errors(*cw);
  1091. resp.setErrors(errors);
  1092. cw.clear();
  1093. factory->deleteWorkUnit(wuid.str());
  1094. }
  1095. catch(IException* e)
  1096. {
  1097. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1098. }
  1099. return true;
  1100. }
  1101. bool CWsWorkunitsEx::onWUCompileECL(IEspContext &context, IEspWUCompileECLRequest &req, IEspWUCompileECLResponse &resp)
  1102. {
  1103. try
  1104. {
  1105. ensureWsCreateWorkunitAccess(context);
  1106. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  1107. NewWsWorkunit wu(factory, context);
  1108. if(req.getIncludeComplexity())
  1109. {
  1110. wu->setAction(WUActionCompile);
  1111. wu->setDebugValueInt("calculateComplexity",1,true);
  1112. }
  1113. else
  1114. wu->setAction(WUActionCheck);
  1115. if(req.getModuleName() && req.getAttributeName())
  1116. {
  1117. wu->setApplicationValue("SyntaxCheck","ModuleName",req.getModuleName(),true);
  1118. wu->setApplicationValue("SyntaxCheck","AttributeName",req.getAttributeName(),true);
  1119. }
  1120. if(req.getIncludeDependencies())
  1121. wu->setApplicationValueInt("SyntaxCheck","IncludeDependencies",1,true);
  1122. wu.setQueryText(req.getECL());
  1123. StringAttr wuid(wu->queryWuid()); // NB queryWuid() not valid after workunit,clear() StringAttr wuid(wu->queryWuid());
  1124. WsWuHelpers::submitWsWorkunit(context, wuid.str(), req.getCluster(), req.getSnapshot(), 0, true, false, false);
  1125. waitForWorkUnitToComplete(wuid.str(),req.getTimeToWait());
  1126. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  1127. SCMStringBuffer s;
  1128. cw->getDebugValue("__Calculated__Complexity__",s);
  1129. if(s.length())
  1130. resp.setComplexity(s.str());
  1131. WsWUExceptions errors(*cw);
  1132. resp.setErrors(errors);
  1133. if(!errors.ErrCount())
  1134. {
  1135. IArrayOf<IEspWUECLAttribute> dependencies;
  1136. for(unsigned count=1;;count++)
  1137. {
  1138. SCMStringBuffer xml;
  1139. cw->getApplicationValue("SyntaxCheck",StringBuffer("Dependency").append(count).str(),xml);
  1140. if(!xml.length())
  1141. break;
  1142. Owned<IPropertyTree> dep=createPTreeFromXMLString(xml.str(), ipt_caseInsensitive);
  1143. if(!dep)
  1144. continue;
  1145. Owned<IEspWUECLAttribute> att = createWUECLAttribute("","");
  1146. att->setModuleName(dep->queryProp("@module"));
  1147. att->setAttributeName(dep->queryProp("@name"));
  1148. int flags = dep->getPropInt("@flags",0);
  1149. if(flags & ob_locked)
  1150. {
  1151. if(flags & ob_lockedself)
  1152. att->setIsCheckedOut(true);
  1153. else
  1154. att->setIsLocked(true);
  1155. }
  1156. if(flags & ob_sandbox)
  1157. att->setIsSandbox(true);
  1158. if(flags & ob_orphaned)
  1159. att->setIsOrphaned(true);
  1160. dependencies.append(*att.getLink());
  1161. }
  1162. resp.setDependencies(dependencies);
  1163. }
  1164. cw.clear();
  1165. factory->deleteWorkUnit(wuid.str());
  1166. }
  1167. catch(IException* e)
  1168. {
  1169. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1170. }
  1171. return true;
  1172. }
  1173. bool CWsWorkunitsEx::onWUGetDependancyTrees(IEspContext& context, IEspWUGetDependancyTreesRequest& req, IEspWUGetDependancyTreesResponse& resp)
  1174. {
  1175. try
  1176. {
  1177. DBGLOG("WUGetDependancyTrees");
  1178. unsigned int timeMilliSec = 500;
  1179. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  1180. NewWsWorkunit wu(factory, context);
  1181. wu->setAction(WUActionCheck);
  1182. if (notEmpty(req.getCluster()))
  1183. wu->setClusterName(req.getCluster());
  1184. if (notEmpty(req.getSnapshot()))
  1185. wu->setSnapshot(req.getSnapshot());
  1186. wu->setDebugValue("gatherDependenciesSelection",notEmpty(req.getItems()) ? req.getItems() : NULL,true);
  1187. if (context.getClientVersion() > 1.12)
  1188. {
  1189. wu->setDebugValueInt("gatherDependencies", 1, true);
  1190. const char *timeout = req.getTimeoutMilliSec();
  1191. if (notEmpty(timeout))
  1192. {
  1193. const char *finger = timeout;
  1194. while (*finger)
  1195. {
  1196. if (!isdigit(*finger++))
  1197. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Incorrect timeout value");
  1198. }
  1199. timeMilliSec = atol(timeout);
  1200. }
  1201. }
  1202. StringAttr wuid(wu->queryWuid()); // NB queryWuid() not valid after workunit,clear()
  1203. wu->commit();
  1204. wu.clear();
  1205. ensureWsWorkunitAccess(context, wuid.str(), SecAccess_Read);
  1206. WsWuHelpers::submitWsWorkunit(context, wuid.str(), req.getCluster(), req.getSnapshot(), 0, true, false, false);
  1207. int state = waitForWorkUnitToComplete(wuid.str(), timeMilliSec);
  1208. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  1209. WsWUExceptions errors(*cw);
  1210. resp.setErrors(errors);
  1211. MemoryBuffer temp;
  1212. MemoryBuffer2IDataVal xmlresult(temp);
  1213. Owned<IConstWUResult> result = cw->getResultBySequence(0);
  1214. if (result)
  1215. {
  1216. result->getResultRaw(xmlresult, NULL, NULL);
  1217. resp.setDependancyTrees(temp);
  1218. }
  1219. wu.setown(&cw->lock());
  1220. wu->setState(WUStateAborted);
  1221. wu->commit();
  1222. wu.clear();
  1223. factory->deleteWorkUnit(wuid.str());
  1224. }
  1225. catch(IException* e)
  1226. {
  1227. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1228. }
  1229. return true;
  1230. }
  1231. bool getWsWuInfoFromSasha(IEspContext &context, SocketEndpoint &ep, const char* wuid, IEspECLWorkunit *info)
  1232. {
  1233. Owned<INode> node = createINode(ep);
  1234. Owned<ISashaCommand> cmd = createSashaCommand();
  1235. cmd->addId(wuid);
  1236. cmd->setAction(SCA_GET);
  1237. if (!cmd->send(node, 1*60*1000))
  1238. {
  1239. StringBuffer url;
  1240. DBGLOG("Could not connect to Sasha server at %s", ep.getUrlStr(url).str());
  1241. throw MakeStringException(ECLWATCH_CANNOT_CONNECT_ARCHIVE_SERVER,"Cannot connect to archive server at %s.", url.str());
  1242. }
  1243. if (cmd->numIds()==0)
  1244. {
  1245. DBGLOG("Could not read archived workunit %s",wuid);
  1246. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT,"Cannot read workunit %s.",wuid);
  1247. }
  1248. unsigned num = cmd->numResults();
  1249. if (num < 1)
  1250. return false;
  1251. StringBuffer res;
  1252. cmd->getResult(0, res);
  1253. if(res.length() < 1)
  1254. return false;
  1255. Owned<IPropertyTree> wpt = createPTreeFromXMLString(res.str());
  1256. if (!wpt)
  1257. return false;
  1258. const char * owner = wpt->queryProp("@submitID");
  1259. ensureWsWorkunitAccessByOwnerId(context, owner, SecAccess_Read);
  1260. info->setWuid(wuid);
  1261. info->setArchived(true);
  1262. if (notEmpty(owner))
  1263. info->setOwner(owner);
  1264. const char * state = wpt->queryProp("@state");
  1265. if (notEmpty(state))
  1266. info->setState(state);
  1267. const char * cluster = wpt->queryProp("@clusterName");
  1268. if (notEmpty(cluster))
  1269. info->setCluster(cluster);
  1270. if (context.querySecManager())
  1271. {
  1272. const char * scope = wpt->queryProp("@scope");
  1273. if (notEmpty(scope))
  1274. info->setScope(scope);
  1275. }
  1276. const char * jobName = wpt->queryProp("@jobName");
  1277. if (notEmpty(jobName))
  1278. info->setJobname(jobName);
  1279. const char * description = wpt->queryProp("Debug/description");
  1280. if (notEmpty(description))
  1281. info->setDescription(description);
  1282. const char * queryText = wpt->queryProp("Query/Text");
  1283. if (notEmpty(queryText))
  1284. info->updateQuery().setText(queryText);
  1285. const char * protectedWU = wpt->queryProp("@protected");
  1286. info->setProtected((protectedWU && *protectedWU!='0'));
  1287. return true;
  1288. }
  1289. #define WUDETAILS_REFRESH_MINS 1
  1290. void getArchivedWUInfo(IEspContext &context, const char* sashaServerIP, unsigned sashaServerPort, const char *wuid, IEspWUInfoResponse &resp)
  1291. {
  1292. SocketEndpoint ep;
  1293. if (sashaServerIP && *sashaServerIP)
  1294. ep.set(sashaServerIP, sashaServerPort);
  1295. else
  1296. getSashaNode(ep);
  1297. if (getWsWuInfoFromSasha(context, ep, wuid, &resp.updateWorkunit()))
  1298. {
  1299. resp.setCanCompile(false);
  1300. return;
  1301. }
  1302. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Cannot find workunit %s.", wuid);
  1303. return;
  1304. }
  1305. #define WUDETAILS_REFRESH_MINS 1
  1306. bool CWsWorkunitsEx::onWUInfo(IEspContext &context, IEspWUInfoRequest &req, IEspWUInfoResponse &resp)
  1307. {
  1308. try
  1309. {
  1310. StringBuffer wuid = req.getWuid();
  1311. WsWuHelpers::checkAndTrimWorkunit("WUInfo", wuid);
  1312. double version = context.getClientVersion();
  1313. if (req.getType() && strieq(req.getType(), "archived workunits"))
  1314. getArchivedWUInfo(context, sashaServerIp.get(), sashaServerPort, wuid.str(), resp);
  1315. else
  1316. {
  1317. try
  1318. {
  1319. //The access is checked here because getArchivedWUInfo() has its own access check.
  1320. ensureWsWorkunitAccess(context, wuid.str(), SecAccess_Read);
  1321. unsigned flags=0;
  1322. if (req.getTruncateEclTo64k())
  1323. flags|=WUINFO_TruncateEclTo64k;
  1324. if (req.getIncludeExceptions())
  1325. flags|=WUINFO_IncludeExceptions;
  1326. if (req.getIncludeGraphs())
  1327. flags|=WUINFO_IncludeGraphs;
  1328. if (req.getIncludeSourceFiles())
  1329. flags|=WUINFO_IncludeSourceFiles;
  1330. if (req.getIncludeResults())
  1331. flags|=WUINFO_IncludeResults;
  1332. if (req.getIncludeVariables())
  1333. flags|=WUINFO_IncludeVariables;
  1334. if (req.getIncludeTimers())
  1335. flags|=WUINFO_IncludeTimers;
  1336. if (req.getIncludeDebugValues())
  1337. flags|=WUINFO_IncludeDebugValues;
  1338. if (req.getIncludeApplicationValues())
  1339. flags|=WUINFO_IncludeApplicationValues;
  1340. if (req.getIncludeWorkflows())
  1341. flags|=WUINFO_IncludeWorkflows;
  1342. if (!req.getSuppressResultSchemas())
  1343. flags|=WUINFO_IncludeEclSchemas;
  1344. if (req.getIncludeXmlSchemas())
  1345. flags|=WUINFO_IncludeXmlSchema;
  1346. if (req.getIncludeResultsViewNames())
  1347. flags|=WUINFO_IncludeResultsViewNames;
  1348. if (req.getIncludeResourceURLs())
  1349. flags|=WUINFO_IncludeResourceURLs;
  1350. WsWuInfo winfo(context, wuid.str());
  1351. winfo.getInfo(resp.updateWorkunit(), flags);
  1352. if (req.getIncludeResultsViewNames()||req.getIncludeResourceURLs()||(version >= 1.50))
  1353. {
  1354. StringArray views, urls;
  1355. winfo.getResourceInfo(views, urls, WUINFO_IncludeResultsViewNames|WUINFO_IncludeResourceURLs);
  1356. IEspECLWorkunit& eclWU = resp.updateWorkunit();
  1357. if (req.getIncludeResultsViewNames())
  1358. resp.setResultViews(views);
  1359. if (req.getIncludeResourceURLs())
  1360. eclWU.setResourceURLs(urls);
  1361. if (version >= 1.50)
  1362. {
  1363. eclWU.setResultViewCount(views.length());
  1364. eclWU.setResourceURLCount(urls.length());
  1365. }
  1366. }
  1367. }
  1368. catch (IException *e)
  1369. {
  1370. if (e->errorCode() != ECLWATCH_CANNOT_OPEN_WORKUNIT)
  1371. throw e;
  1372. getArchivedWUInfo(context, sashaServerIp.get(), sashaServerPort, wuid.str(), resp);
  1373. e->Release();
  1374. }
  1375. switch (resp.getWorkunit().getStateID())
  1376. {
  1377. case WUStateCompiling:
  1378. case WUStateCompiled:
  1379. case WUStateScheduled:
  1380. case WUStateSubmitted:
  1381. case WUStateRunning:
  1382. case WUStateAborting:
  1383. case WUStateWait:
  1384. case WUStateUploadingFiles:
  1385. case WUStateDebugPaused:
  1386. case WUStateDebugRunning:
  1387. resp.setAutoRefresh(WUDETAILS_REFRESH_MINS);
  1388. break;
  1389. case WUStateBlocked:
  1390. resp.setAutoRefresh(WUDETAILS_REFRESH_MINS*5);
  1391. break;
  1392. }
  1393. resp.setCanCompile(notEmpty(context.queryUserId()));
  1394. if (version > 1.24 && notEmpty(req.getThorSlaveIP()))
  1395. resp.setThorSlaveIP(req.getThorSlaveIP());
  1396. resp.setSecMethod(authMethod.get());
  1397. }
  1398. }
  1399. catch(IException* e)
  1400. {
  1401. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1402. }
  1403. return true;
  1404. }
  1405. bool CWsWorkunitsEx::onWUInfoDetails(IEspContext &context, IEspWUInfoRequest &req, IEspWUInfoResponse &resp)
  1406. {
  1407. return onWUInfo(context, req, resp);
  1408. }
  1409. bool CWsWorkunitsEx::onWUResultView(IEspContext &context, IEspWUResultViewRequest &req, IEspWUResultViewResponse &resp)
  1410. {
  1411. StringBuffer wuid = req.getWuid();
  1412. WsWuHelpers::checkAndTrimWorkunit("WUResultView", wuid);
  1413. ensureWsWorkunitAccess(context, wuid.str(), SecAccess_Read);
  1414. Owned<IWuWebView> wv = createWuWebView(wuid.str(), NULL, NULL, getCFD(), true);
  1415. StringBuffer html;
  1416. wv->renderSingleResult(req.getViewName(), req.getResultName(), html);
  1417. resp.setResult(html.str());
  1418. resp.setResult_mimetype("text/html");
  1419. return true;
  1420. }
  1421. void doWUQueryBySingleWuid(IEspContext &context, const char *wuid, IEspWUQueryResponse &resp)
  1422. {
  1423. Owned<IEspECLWorkunit> info= createECLWorkunit("","");
  1424. WsWuInfo winfo(context, wuid);
  1425. winfo.getCommon(*info, 0);
  1426. IArrayOf<IEspECLWorkunit> results;
  1427. results.append(*info.getClear());
  1428. resp.setWorkunits(results);
  1429. resp.setPageSize(1);
  1430. resp.setCount(1);
  1431. }
  1432. void doWUQueryByFile(IEspContext &context, const char *logicalFile, IEspWUQueryResponse &resp)
  1433. {
  1434. StringBuffer wuid;
  1435. getWuidFromLogicalFileName(context, logicalFile, wuid);
  1436. if (!wuid.length())
  1437. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT,"Cannot find the workunit for file %s.", logicalFile);
  1438. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  1439. Owned<IConstWorkUnit> cw= factory->openWorkUnit(wuid.str(), false);
  1440. if (!cw)
  1441. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot find the workunit for file %s.", logicalFile);
  1442. if (getWsWorkunitAccess(context, *cw) < SecAccess_Read)
  1443. throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED,"Cannot access the workunit for file %s.",logicalFile);
  1444. doWUQueryBySingleWuid(context, wuid.str(), resp);
  1445. resp.setFirst(false);
  1446. resp.setPageSize(1);
  1447. resp.setCount(1);
  1448. }
  1449. void doWUQueryByXPath(IEspContext &context, IEspWUQueryRequest & req, IEspWUQueryResponse & resp)
  1450. {
  1451. IArrayOf<IEspECLWorkunit> results;
  1452. WsWuSearch wlist(context,req.getOwner(),req.getState(),req.getCluster(),req.getStartDate(),req.getEndDate(),req.getECL(),req.getJobname(),req.getApplicationName(),req.getApplicationKey(),req.getApplicationData());
  1453. int count=(int)req.getPageSize();
  1454. if (!count)
  1455. count=100;
  1456. if (wlist.getSize() < 1)
  1457. {
  1458. resp.setNumWUs(0);
  1459. return;
  1460. }
  1461. if (wlist.getSize() < count)
  1462. count = (int) wlist.getSize() - 1;
  1463. WsWuSearch::iterator begin, end;
  1464. if(notEmpty(req.getAfter()))
  1465. {
  1466. begin=wlist.locate(req.getAfter());
  1467. end=min(begin+count,wlist.end());
  1468. }
  1469. else if (notEmpty(req.getBefore()))
  1470. {
  1471. end=wlist.locate(req.getBefore());
  1472. begin=max(end-count,wlist.begin());
  1473. }
  1474. else
  1475. {
  1476. begin=wlist.begin();
  1477. end=min(begin+count,wlist.end());
  1478. }
  1479. if(begin>wlist.begin() && begin<wlist.end())
  1480. resp.setCurrent(begin->c_str());
  1481. if (context.getClientVersion() > 1.02)
  1482. {
  1483. resp.setPageStartFrom(begin - wlist.begin() + 1);
  1484. resp.setNumWUs((int)wlist.getSize());
  1485. resp.setCount(end - begin);
  1486. }
  1487. if(end<wlist.end())
  1488. resp.setNext(end->c_str());
  1489. for(;begin!=end;begin++)
  1490. {
  1491. Owned<IEspECLWorkunit> info = createECLWorkunit("","");
  1492. WsWuInfo winfo(context, begin->c_str());
  1493. winfo.getCommon(*info, 0);
  1494. results.append(*info.getClear());
  1495. }
  1496. resp.setPageSize(abs(count));
  1497. resp.setWorkunits(results);
  1498. return;
  1499. }
  1500. bool addWUQueryFilter(WUSortField *filters, unsigned short &count, MemoryBuffer &buff, const char *name, WUSortField value)
  1501. {
  1502. if (isEmpty(name))
  1503. return false;
  1504. filters[count++] = value;
  1505. buff.append(name);
  1506. return true;
  1507. }
  1508. bool addWUQueryFilterTime(WUSortField *filters, unsigned short &count, MemoryBuffer &buff, const char *stime, WUSortField value)
  1509. {
  1510. if (isEmpty(stime))
  1511. return false;
  1512. CDateTime dt;
  1513. dt.setString(stime, NULL, true);
  1514. unsigned year, month, day, hour, minute, second, nano;
  1515. dt.getDate(year, month, day, true);
  1516. dt.getTime(hour, minute, second, nano, true);
  1517. VStringBuffer wuid("W%4d%02d%02d-%02d%02d%02d",year,month,day,hour,minute,second);
  1518. filters[count++] = value;
  1519. buff.append(wuid.str());
  1520. return true;
  1521. }
  1522. bool addWUQueryFilterApplication(WUSortField *filters, unsigned short &count, MemoryBuffer &buff, const char *appname, const char *appkey, const char *appdata)
  1523. {
  1524. if (isEmpty(appname) && isEmpty(appkey) && isEmpty(appdata)) //no application filter
  1525. return false;
  1526. VStringBuffer path("Application/%s/%s", appname && *appname ? appname : "*", appkey && *appkey ? appkey : "*");
  1527. if(appdata && *appdata)
  1528. path.append("=?~\"").append(appdata).append("\"");
  1529. filters[count++] = WUSFcustom;
  1530. buff.append(path.str());
  1531. return true;
  1532. }
  1533. void doWUQueryWithSort(IEspContext &context, IEspWUQueryRequest & req, IEspWUQueryResponse & resp)
  1534. {
  1535. SecAccessFlags accessOwn;
  1536. SecAccessFlags accessOthers;
  1537. getUserWuAccessFlags(context, accessOwn, accessOthers, true);
  1538. double version = context.getClientVersion();
  1539. IArrayOf<IEspECLWorkunit> results;
  1540. int begin = 0;
  1541. unsigned int count = 100;
  1542. int pagesize = 100;
  1543. if (version > 1.01)
  1544. {
  1545. pagesize = (int)req.getPageSize();
  1546. if (!req.getCount_isNull())
  1547. pagesize = req.getCount();
  1548. if(pagesize < 1)
  1549. pagesize = 100;
  1550. begin = (int)req.getPageStartFrom();
  1551. }
  1552. else
  1553. {
  1554. count=(unsigned)req.getCount();
  1555. if(!count)
  1556. count=100;
  1557. if (notEmpty(req.getAfter()))
  1558. begin=atoi(req.getAfter());
  1559. else if (notEmpty(req.getBefore()))
  1560. begin=atoi(req.getBefore())-count;
  1561. if (begin < 0)
  1562. begin = 0;
  1563. pagesize = count;
  1564. }
  1565. WUSortField sortorder[2] = {(WUSortField) (WUSFwuid | WUSFreverse), WUSFterm};
  1566. if(notEmpty(req.getSortby()))
  1567. {
  1568. const char *sortby = req.getSortby();
  1569. if (strieq(sortby, "Owner"))
  1570. sortorder[0] = WUSFuser;
  1571. else if (strieq(sortby, "JobName"))
  1572. sortorder[0] = WUSFjob;
  1573. else if (strieq(sortby, "Cluster"))
  1574. sortorder[0] = WUSFcluster;
  1575. else if (strieq(sortby, "RoxieCluster"))
  1576. sortorder[0] = WUSFroxiecluster;
  1577. else if (strieq(sortby, "Protected"))
  1578. sortorder[0] = WUSFprotected;
  1579. else if (strieq(sortby, "State"))
  1580. sortorder[0] = WUSFstate;
  1581. else if (strieq(sortby, "ClusterTime"))
  1582. sortorder[0] = (WUSortField) (WUSFtotalthortime+WUSFnumeric);
  1583. else
  1584. sortorder[0] = WUSFwuid;
  1585. sortorder[0] = (WUSortField) (sortorder[0] | WUSFnocase);
  1586. bool descending = req.getDescending();
  1587. if (descending)
  1588. sortorder[0] = (WUSortField) (sortorder[0] | WUSFreverse);
  1589. }
  1590. WUSortField filters[10];
  1591. unsigned short filterCount = 0;
  1592. MemoryBuffer filterbuf;
  1593. bool bDoubleCheckState = false;
  1594. if(req.getState() && *req.getState())
  1595. {
  1596. filters[filterCount++] = WUSFstate;
  1597. if (!strieq(req.getState(), "unknown"))
  1598. filterbuf.append(req.getState());
  1599. else
  1600. filterbuf.append("");
  1601. if (strieq(req.getState(), "submitted"))
  1602. bDoubleCheckState = true;
  1603. }
  1604. addWUQueryFilter(filters, filterCount, filterbuf, req.getWuid(), WUSFwildwuid);
  1605. addWUQueryFilter(filters, filterCount, filterbuf, req.getCluster(), WUSFcluster);
  1606. if(version > 1.07)
  1607. addWUQueryFilter(filters, filterCount, filterbuf, req.getRoxieCluster(), WUSFroxiecluster);
  1608. addWUQueryFilter(filters, filterCount, filterbuf, req.getLogicalFile(), WUSFfileread);
  1609. addWUQueryFilter(filters, filterCount, filterbuf, req.getOwner(), (WUSortField) (WUSFuser | WUSFnocase));
  1610. addWUQueryFilter(filters, filterCount, filterbuf, req.getJobname(), (WUSortField) (WUSFjob | WUSFnocase));
  1611. addWUQueryFilter(filters, filterCount, filterbuf, req.getECL(), (WUSortField) (WUSFecl | WUSFwild));
  1612. addWUQueryFilterTime(filters, filterCount, filterbuf, req.getStartDate(), WUSFwuid);
  1613. addWUQueryFilterTime(filters, filterCount, filterbuf, req.getEndDate(), WUSFwuidhigh);
  1614. addWUQueryFilterApplication(filters, filterCount, filterbuf, req.getApplicationName(), req.getApplicationKey(), req.getApplicationData());
  1615. filters[filterCount] = WUSFterm;
  1616. __int64 cacheHint = 0;
  1617. if (!req.getCacheHint_isNull())
  1618. cacheHint = req.getCacheHint();
  1619. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  1620. unsigned numWUs;
  1621. Owned<IConstWorkUnitIterator> it = factory->getWorkUnitsSorted(sortorder, filters, filterbuf.bufferBase(), begin, pagesize+1, "", &cacheHint, &numWUs);
  1622. if (version >= 1.41)
  1623. resp.setCacheHint(cacheHint);
  1624. unsigned actualCount = 0;
  1625. ForEach(*it)
  1626. {
  1627. IConstWorkUnitInfo& cw = it->query();
  1628. if (chooseWuAccessFlagsByOwnership(context.queryUserId(), cw, accessOwn, accessOthers) < SecAccess_Read)
  1629. {
  1630. numWUs--;
  1631. continue;
  1632. }
  1633. if (bDoubleCheckState && (cw.getState() != WUStateSubmitted))
  1634. {
  1635. numWUs--;
  1636. continue;
  1637. }
  1638. const char* wuid = cw.queryWuid();
  1639. if (!looksLikeAWuid(wuid))
  1640. {
  1641. numWUs--;
  1642. continue;
  1643. }
  1644. actualCount++;
  1645. Owned<IEspECLWorkunit> info = createECLWorkunit("","");
  1646. WsWuInfo winfo(context, wuid);
  1647. winfo.getCommon(*info, 0);
  1648. results.append(*info.getClear());
  1649. }
  1650. if (version > 1.02)
  1651. {
  1652. resp.setPageStartFrom(begin+1);
  1653. resp.setNumWUs(numWUs);
  1654. if (results.length() > (aindex_t)pagesize)
  1655. results.pop();
  1656. if(unsigned (begin + pagesize) < numWUs)
  1657. {
  1658. resp.setNextPage(begin + pagesize);
  1659. resp.setPageEndAt(begin + pagesize);
  1660. int last = begin + pagesize;
  1661. while (numWUs > (unsigned) last + pagesize)
  1662. last += pagesize;
  1663. resp.setLastPage(last);
  1664. }
  1665. else
  1666. {
  1667. resp.setNextPage(-1);
  1668. resp.setPageEndAt(numWUs);
  1669. }
  1670. if(begin > 0)
  1671. {
  1672. resp.setFirst(false);
  1673. if (begin - pagesize > 0)
  1674. resp.setPrevPage(begin - pagesize);
  1675. else
  1676. resp.setPrevPage(0);
  1677. }
  1678. resp.setPageSize(pagesize);
  1679. }
  1680. else
  1681. {
  1682. if(begin>0 && actualCount > 0)
  1683. {
  1684. char buf[10];
  1685. itoa(begin, buf, 10);
  1686. resp.setCurrent(buf);
  1687. }
  1688. if(count<actualCount)
  1689. {
  1690. char buf[10];
  1691. itoa(begin+count, buf, 10);
  1692. resp.setNext(buf);
  1693. resp.setNumWUs(numWUs);
  1694. if (results.length() > count)
  1695. results.pop();
  1696. }
  1697. if(begin == 0 && actualCount <= count)
  1698. resp.setFirst(false);
  1699. resp.setCount(count);
  1700. }
  1701. resp.setWorkunits(results);
  1702. return;
  1703. }
  1704. void doWUQueryFromArchive(IEspContext &context, const char* sashaServerIP, unsigned sashaServerPort,
  1705. ArchivedWuCache &archivedWuCache, unsigned cacheMinutes, IEspWUQueryRequest & req, IEspWUQueryResponse & resp)
  1706. {
  1707. class CArchivedWUsReader : public CInterface, implements IArchivedWUsReader
  1708. {
  1709. IEspContext& context;
  1710. IEspWUQueryRequest& req;
  1711. unsigned pageFrom, pageSize;
  1712. StringAttr sashaServerIP;
  1713. unsigned sashaServerPort;
  1714. unsigned cacheMinutes;
  1715. StringBuffer filterStr;
  1716. ArchivedWuCache& archivedWuCache;
  1717. unsigned numberOfWUsReturned;
  1718. bool hasMoreWU;
  1719. void readDateFilters(StringBuffer& from, StringBuffer& to)
  1720. {
  1721. CDateTime timeFrom, timeTo;
  1722. if(notEmpty(req.getEndDate()))
  1723. timeTo.setString(req.getEndDate(), NULL, true);
  1724. else
  1725. timeTo.setNow();
  1726. unsigned year, month, day, hour, minute, second, nano;
  1727. timeTo.getDate(year, month, day, true);
  1728. timeTo.getTime(hour, minute, second, nano, true);
  1729. to.setf("%4d%02d%02d%02d%02d", year, month, day, hour, minute);
  1730. if(!notEmpty(req.getStartDate()))
  1731. return;
  1732. timeFrom.setString(req.getStartDate(), NULL, true);
  1733. if (timeFrom >= timeTo)
  1734. return;
  1735. unsigned year0, month0, day0, hour0, minute0, second0, nano0;
  1736. timeFrom.getDate(year0, month0, day0, true);
  1737. timeFrom.getTime(hour0, minute0, second0, nano0, true);
  1738. from.setf("%4d%02d%02d%02d%02d", year0, month0, day0, hour0, minute0);
  1739. return;
  1740. }
  1741. bool addToFilterString(const char *name, const char *value)
  1742. {
  1743. if (isEmpty(name) || isEmpty(value))
  1744. return false;
  1745. if (filterStr.length())
  1746. filterStr.append(';');
  1747. filterStr.append(name).append("=").append(value);
  1748. return true;
  1749. }
  1750. bool addToFilterString(const char *name, unsigned value)
  1751. {
  1752. if (isEmpty(name))
  1753. return false;
  1754. if (filterStr.length())
  1755. filterStr.append(';');
  1756. filterStr.append(name).append("=").append(value);
  1757. return true;
  1758. }
  1759. void setFilterString()
  1760. {
  1761. addToFilterString("cluster", req.getCluster());
  1762. addToFilterString("owner", req.getOwner());
  1763. addToFilterString("jobName", req.getJobname());
  1764. addToFilterString("state", req.getState());
  1765. addToFilterString("timeFrom", req.getStartDate());
  1766. addToFilterString("timeTo", req.getEndDate());
  1767. addToFilterString("pageStart", pageFrom);
  1768. addToFilterString("pageSize", pageSize);
  1769. if (sashaServerIP && *sashaServerIP)
  1770. {
  1771. addToFilterString("sashaServerIP", sashaServerIP.get());
  1772. addToFilterString("sashaServerPort", sashaServerPort);
  1773. }
  1774. }
  1775. void setSashaCommand(INode* sashaserver, ISashaCommand* cmd)
  1776. {
  1777. cmd->setAction(SCA_LIST);
  1778. cmd->setOutputFormat("owner,jobname,cluster,state");
  1779. cmd->setOnline(false);
  1780. cmd->setArchived(true);
  1781. cmd->setStart(pageFrom);
  1782. cmd->setLimit(pageSize+1); //read an extra WU to check hasMoreWU
  1783. if (notEmpty(req.getCluster()))
  1784. cmd->setCluster(req.getCluster());
  1785. if (notEmpty(req.getOwner()))
  1786. cmd->setOwner(req.getOwner());
  1787. if (notEmpty(req.getJobname()))
  1788. cmd->setJobName(req.getJobname());
  1789. if (notEmpty(req.getState()))
  1790. cmd->setState(req.getState());
  1791. StringBuffer timeFrom, timeTo;
  1792. readDateFilters(timeFrom, timeTo);
  1793. if (timeFrom.length())
  1794. cmd->setAfter(timeFrom.str());
  1795. if (timeTo.length())
  1796. cmd->setBefore(timeTo.str());
  1797. return;
  1798. }
  1799. IEspECLWorkunit *createArchivedWUEntry(StringArray& wuDataArray, bool canAccess)
  1800. {
  1801. Owned<IEspECLWorkunit> info= createECLWorkunit("","");
  1802. const char* wuid = wuDataArray.item(0);
  1803. const char* owner = wuDataArray.item(1);
  1804. const char* jobName = wuDataArray.item(2);
  1805. const char* cluster = wuDataArray.item(3);
  1806. const char* state = wuDataArray.item(4);
  1807. info->setWuid(wuid);
  1808. if (!canAccess)
  1809. info->setState("<Hidden>");
  1810. else
  1811. {
  1812. if (notEmpty(owner))
  1813. info->setOwner(owner);
  1814. if (notEmpty(jobName))
  1815. info->setJobname(jobName);
  1816. if (notEmpty(cluster))
  1817. info->setCluster(cluster);
  1818. if (notEmpty(state))
  1819. info->setState(state);
  1820. }
  1821. return info.getClear();
  1822. }
  1823. static int compareWuids(IInterface * const *_a, IInterface * const *_b)
  1824. {
  1825. IEspECLWorkunit *a = *(IEspECLWorkunit **)_a;
  1826. IEspECLWorkunit *b = *(IEspECLWorkunit **)_b;
  1827. return strcmp(b->getWuid(), a->getWuid());
  1828. }
  1829. public:
  1830. IMPLEMENT_IINTERFACE_USING(CInterface);
  1831. CArchivedWUsReader(IEspContext& _context, const char* _sashaServerIP, unsigned _sashaServerPort, ArchivedWuCache& _archivedWuCache,
  1832. unsigned _cacheMinutes, unsigned _pageFrom, unsigned _pageSize, IEspWUQueryRequest& _req)
  1833. : context(_context), sashaServerIP(_sashaServerIP), sashaServerPort(_sashaServerPort),
  1834. archivedWuCache(_archivedWuCache), cacheMinutes(_cacheMinutes), pageFrom(_pageFrom), pageSize(_pageSize), req(_req)
  1835. {
  1836. hasMoreWU = false;
  1837. numberOfWUsReturned = 0;
  1838. }
  1839. void getArchivedWUs(IArrayOf<IEspECLWorkunit>& archivedWUs)
  1840. {
  1841. setFilterString();
  1842. Owned<ArchivedWuCacheElement> cachedResults = archivedWuCache.lookup(context, filterStr, "AddWhenAvailable", cacheMinutes);
  1843. if (cachedResults)
  1844. {
  1845. hasMoreWU = cachedResults->m_hasNextPage;
  1846. numberOfWUsReturned = cachedResults->numWUsReturned;
  1847. if (cachedResults->m_results.length())
  1848. {
  1849. ForEachItemIn(ai, cachedResults->m_results)
  1850. archivedWUs.append(*LINK(&cachedResults->m_results.item(ai)));
  1851. }
  1852. }
  1853. else
  1854. {
  1855. SocketEndpoint ep;
  1856. if (sashaServerIP && *sashaServerIP)
  1857. ep.set(sashaServerIP, sashaServerPort);
  1858. else
  1859. getSashaNode(ep);
  1860. Owned<INode> sashaserver = createINode(ep);
  1861. Owned<ISashaCommand> cmd = createSashaCommand();
  1862. setSashaCommand(sashaserver, cmd);
  1863. if (!cmd->send(sashaserver))
  1864. {
  1865. StringBuffer msg("Cannot connect to archive server at ");
  1866. sashaserver->endpoint().getUrlStr(msg);
  1867. throw MakeStringException(ECLWATCH_CANNOT_CONNECT_ARCHIVE_SERVER, "%s", msg.str());
  1868. }
  1869. numberOfWUsReturned = cmd->numIds();
  1870. hasMoreWU = (numberOfWUsReturned > pageSize);
  1871. if (hasMoreWU)
  1872. numberOfWUsReturned--;
  1873. if (numberOfWUsReturned > 0)
  1874. {
  1875. SecAccessFlags accessOwn, accessOthers;
  1876. getUserWuAccessFlags(context, accessOwn, accessOthers, true);
  1877. for (unsigned i=0; i<numberOfWUsReturned; i++)
  1878. {
  1879. const char *csline = cmd->queryId(i);
  1880. if (!csline || !*csline)
  1881. continue;
  1882. StringArray wuDataArray;
  1883. wuDataArray.appendList(csline, ",");
  1884. const char* wuid = wuDataArray.item(0);
  1885. if (isEmpty(wuid))
  1886. {
  1887. WARNLOG("Empty WUID in SCA_LIST response"); // JCS->KW - have u ever seen this happen?
  1888. continue;
  1889. }
  1890. const char* owner = wuDataArray.item(1);
  1891. bool canAccess = chooseWuAccessFlagsByOwnership(context.queryUserId(), owner, accessOwn, accessOthers) >= SecAccess_Read;
  1892. Owned<IEspECLWorkunit> info = createArchivedWUEntry(wuDataArray, canAccess);
  1893. archivedWUs.append(*info.getClear());
  1894. }
  1895. archivedWUs.sort(compareWuids);
  1896. archivedWuCache.add(filterStr, "AddWhenAvailable", hasMoreWU, numberOfWUsReturned, archivedWUs);
  1897. }
  1898. }
  1899. return;
  1900. };
  1901. bool getHasMoreWU() { return hasMoreWU; };
  1902. unsigned getNumberOfWUsReturned() { return numberOfWUsReturned; };
  1903. };
  1904. unsigned pageStart = (unsigned) req.getPageStartFrom();
  1905. unsigned pageSize = (unsigned) req.getPageSize();
  1906. if(pageSize < 1)
  1907. pageSize=500;
  1908. IArrayOf<IEspECLWorkunit> archivedWUs;
  1909. Owned<IArchivedWUsReader> archiveWUsReader = new CArchivedWUsReader(context, sashaServerIP, sashaServerPort, archivedWuCache,
  1910. cacheMinutes, pageStart, pageSize, req);
  1911. archiveWUsReader->getArchivedWUs(archivedWUs);
  1912. resp.setWorkunits(archivedWUs);
  1913. resp.setNumWUs(archiveWUsReader->getNumberOfWUsReturned());
  1914. resp.setType("archived only");
  1915. resp.setPageSize(pageSize);
  1916. resp.setPageStartFrom(pageStart+1);
  1917. resp.setPageEndAt(pageStart + archiveWUsReader->getNumberOfWUsReturned());
  1918. if(pageStart > 0)
  1919. { //This is not the first page;
  1920. resp.setFirst(false);
  1921. resp.setPrevPage((pageStart > pageSize) ? pageStart - pageSize: 0);
  1922. }
  1923. if (archiveWUsReader->getHasMoreWU())
  1924. resp.setNextPage(pageStart + pageSize);
  1925. return;
  1926. }
  1927. bool CWsWorkunitsEx::onWUQuery(IEspContext &context, IEspWUQueryRequest & req, IEspWUQueryResponse & resp)
  1928. {
  1929. try
  1930. {
  1931. StringBuffer wuidStr = req.getWuid();
  1932. const char* wuid = wuidStr.trim().str();
  1933. if (req.getType() && strieq(req.getType(), "archived workunits"))
  1934. doWUQueryFromArchive(context, sashaServerIp.get(), sashaServerPort, *archivedWuCache, awusCacheMinutes, req, resp);
  1935. else if(notEmpty(wuid) && looksLikeAWuid(wuid))
  1936. doWUQueryBySingleWuid(context, wuid, resp);
  1937. else if (notEmpty(req.getLogicalFile()) && req.getLogicalFileSearchType() && strieq(req.getLogicalFileSearchType(), "Created"))
  1938. doWUQueryByFile(context, req.getLogicalFile(), resp);
  1939. else
  1940. doWUQueryWithSort(context, req, resp);
  1941. resp.setState(req.getState());
  1942. resp.setCluster(req.getCluster());
  1943. resp.setRoxieCluster(req.getRoxieCluster());
  1944. resp.setOwner(req.getOwner());
  1945. resp.setStartDate(req.getStartDate());
  1946. resp.setEndDate(req.getEndDate());
  1947. double version = context.getClientVersion();
  1948. StringBuffer basicQuery;
  1949. addToQueryString(basicQuery, "State", req.getState());
  1950. addToQueryString(basicQuery, "Cluster", req.getCluster());
  1951. if (version > 1.07)
  1952. addToQueryString(basicQuery, "RoxieCluster", req.getRoxieCluster());
  1953. addToQueryString(basicQuery, "Owner", req.getOwner());
  1954. addToQueryString(basicQuery, "StartDate", req.getStartDate());
  1955. addToQueryString(basicQuery, "EndDate", req.getEndDate());
  1956. if (version > 1.25 && req.getLastNDays() > -1)
  1957. addToQueryString(basicQuery, "LastNDays", StringBuffer().append(req.getLastNDays()).str());
  1958. addToQueryString(basicQuery, "ECL", req.getECL());
  1959. addToQueryString(basicQuery, "Jobname", req.getJobname());
  1960. addToQueryString(basicQuery, "Type", req.getType());
  1961. if (addToQueryString(basicQuery, "LogicalFile", req.getLogicalFile()))
  1962. addToQueryString(basicQuery, "LogicalFileSearchType", req.getLogicalFileSearchType());
  1963. resp.setFilters(basicQuery.str());
  1964. if (notEmpty(req.getSortby()) && !strstr(basicQuery.str(), StringBuffer(req.getSortby()).append('=').str()))
  1965. {
  1966. resp.setSortby(req.getSortby());
  1967. addToQueryString(basicQuery, "Sortby", req.getSortby());
  1968. if (req.getDescending())
  1969. {
  1970. resp.setDescending(req.getDescending());
  1971. addToQueryString(basicQuery, "Descending", "1");
  1972. }
  1973. }
  1974. resp.setBasicQuery(basicQuery.str());
  1975. StringBuffer s;
  1976. if(notEmpty(req.getECL()))
  1977. resp.setECL(Utils::url_encode(req.getECL(), s).str());
  1978. if(notEmpty(req.getJobname()))
  1979. resp.setJobname(Utils::url_encode(req.getJobname(), s.clear()).str());
  1980. }
  1981. catch(IException* e)
  1982. {
  1983. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1984. }
  1985. return true;
  1986. }
  1987. void appendResultSet(MemoryBuffer& mb, INewResultSet* result, const char *name, __int64 start, unsigned& count, __int64& total, bool bin, bool xsd, ESPSerializationFormat fmt, const IProperties *xmlns)
  1988. {
  1989. if (!result)
  1990. return;
  1991. Owned<IResultSetCursor> cursor(result->createCursor());
  1992. total=result->getNumRows();
  1993. if(bin)
  1994. count = getResultBin(mb, result, (unsigned)start, count);
  1995. else
  1996. {
  1997. struct MemoryBuffer2IStringVal : public CInterface, implements IStringVal
  1998. {
  1999. MemoryBuffer2IStringVal(MemoryBuffer & _buffer) : buffer(_buffer) {}
  2000. IMPLEMENT_IINTERFACE;
  2001. virtual const char * str() const { UNIMPLEMENTED; }
  2002. virtual void set(const char *val) { buffer.append(strlen(val),val); }
  2003. virtual void clear() { } // support appending only
  2004. virtual void setLen(const char *val, unsigned length) { buffer.append(length, val); }
  2005. virtual unsigned length() const { return buffer.length(); };
  2006. MemoryBuffer & buffer;
  2007. } adaptor(mb);
  2008. if (fmt==ESPSerializationJSON)
  2009. count = getResultJSON(adaptor, result, name, (unsigned) start, count, (xsd) ? "myschema" : NULL);
  2010. else
  2011. count = getResultXml(adaptor, result, name, (unsigned) start, count, (xsd) ? "myschema" : NULL, xmlns);
  2012. }
  2013. }
  2014. INewResultSet* createFilteredResultSet(INewResultSet* result, IArrayOf<IConstNamedValue>* filterBy)
  2015. {
  2016. if (!result || !filterBy || !filterBy->length())
  2017. return NULL;
  2018. Owned<IFilteredResultSet> filter = result->createFiltered();
  2019. const IResultSetMetaData &meta = result->getMetaData();
  2020. unsigned columnCount = meta.getColumnCount();
  2021. ForEachItemIn(i, *filterBy)
  2022. {
  2023. IConstNamedValue &item = filterBy->item(i);
  2024. const char *name = item.getName();
  2025. const char *value = item.getValue();
  2026. if (!name || !*name || !value || !*value)
  2027. continue;
  2028. for(unsigned col = 0; col < columnCount; col++)
  2029. {
  2030. SCMStringBuffer scmbuf;
  2031. meta.getColumnLabel(scmbuf, col);
  2032. if (strieq(scmbuf.str(), name))
  2033. {
  2034. filter->addFilter(col, value);
  2035. break;
  2036. }
  2037. }
  2038. }
  2039. return filter->create();
  2040. }
  2041. void getWsWuResult(IEspContext &context, const char* wuid, const char *name, const char *logical, unsigned index, __int64 start,
  2042. unsigned& count, __int64& total, IStringVal& resname, bool bin, IArrayOf<IConstNamedValue>* filterBy, MemoryBuffer& mb, bool xsd=true)
  2043. {
  2044. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2045. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid, false);
  2046. if(!cw)
  2047. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.",wuid);
  2048. Owned<IConstWUResult> result;
  2049. if (notEmpty(name))
  2050. result.setown(cw->getResultByName(name));
  2051. else if (notEmpty(logical))
  2052. {
  2053. Owned<IConstWUResultIterator> it = &cw->getResults();
  2054. ForEach(*it)
  2055. {
  2056. IConstWUResult &r = it->query();
  2057. SCMStringBuffer filename;
  2058. if(strieq(r.getResultLogicalName(filename).str(), logical))
  2059. {
  2060. result.setown(LINK(&r));
  2061. break;
  2062. }
  2063. }
  2064. }
  2065. else
  2066. result.setown(cw->getResultBySequence(index));
  2067. if (!result)
  2068. throw MakeStringException(ECLWATCH_CANNOT_GET_WU_RESULT,"Cannot open the workunit result.");
  2069. if (!resname.length())
  2070. result->getResultName(resname);
  2071. Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), context.queryUser(), context.queryUserId(), context.queryPassword());
  2072. SCMStringBuffer logicalName;
  2073. result->getResultLogicalName(logicalName);
  2074. Owned<INewResultSet> rs;
  2075. if (logicalName.length())
  2076. {
  2077. rs.setown(resultSetFactory->createNewFileResultSet(logicalName.str(), cw->queryClusterName())); //MORE is this wrong cluster?
  2078. }
  2079. else
  2080. rs.setown(resultSetFactory->createNewResultSet(result, wuid));
  2081. if (!filterBy || !filterBy->length())
  2082. appendResultSet(mb, rs, name, start, count, total, bin, xsd, context.getResponseFormat(), result->queryResultXmlns());
  2083. else
  2084. {
  2085. Owned<INewResultSet> filteredResult = createFilteredResultSet(rs, filterBy);
  2086. appendResultSet(mb, filteredResult, name, start, count, total, bin, xsd, context.getResponseFormat(), result->queryResultXmlns());
  2087. }
  2088. }
  2089. void openSaveFile(IEspContext &context, int opt, const char* filename, const char* origMimeType, MemoryBuffer& buf, IEspWULogFileResponse &resp)
  2090. {
  2091. if (opt < 1)
  2092. {
  2093. resp.setThefile(buf);
  2094. resp.setThefile_mimetype(origMimeType);
  2095. }
  2096. else if (opt < 2)
  2097. {
  2098. StringBuffer headerStr("attachment;");
  2099. if (filename && *filename)
  2100. {
  2101. const char* pFileName = strrchr(filename, PATHSEPCHAR);
  2102. if (pFileName)
  2103. headerStr.appendf("filename=%s", pFileName+1);
  2104. else
  2105. headerStr.appendf("filename=%s", filename);
  2106. }
  2107. MemoryBuffer buf0;
  2108. unsigned i = 0;
  2109. char* p = (char*) buf.toByteArray();
  2110. while (i < buf.length())
  2111. {
  2112. if (p[0] != 10)
  2113. buf0.append(p[0]);
  2114. else
  2115. buf0.append(0x0d);
  2116. p++;
  2117. i++;
  2118. }
  2119. resp.setThefile(buf);
  2120. resp.setThefile_mimetype(origMimeType);
  2121. context.addCustomerHeader("Content-disposition", headerStr.str());
  2122. }
  2123. else
  2124. {
  2125. #ifndef _USE_ZLIB
  2126. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA,"The data cannot be compressed.");
  2127. #else
  2128. StringBuffer fileNameStr, headerStr("attachment;");
  2129. if (notEmpty(filename))
  2130. {
  2131. fileNameStr.append(filename);
  2132. headerStr.append("filename=").append(filename).append((opt>2) ? ".gz" : ".zip");
  2133. }
  2134. else
  2135. fileNameStr.append("file");
  2136. StringBuffer ifname;
  2137. ifname.appendf("%s%sT%xAT%x", TEMPZIPDIR, PATHSEPSTR, (unsigned)(memsize_t)GetCurrentThreadId(), msTick()).append((opt>2)? "" : ".zip");
  2138. IZZIPor* Zipor = createZZIPor();
  2139. int ret = 0;
  2140. if (opt > 2)
  2141. ret = Zipor->gzipToFile(buf.length(), (void*)buf.toByteArray(), ifname.str());
  2142. else
  2143. ret = Zipor->zipToFile(buf.length(), (void*)buf.toByteArray(), fileNameStr.str(), ifname.str());
  2144. releaseIZ(Zipor);
  2145. if (ret < 0)
  2146. {
  2147. Owned<IFile> rFile = createIFile(ifname.str());
  2148. if (rFile->exists())
  2149. rFile->remove();
  2150. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA,"The data cannot be compressed.");
  2151. }
  2152. Owned <IFile> rf = createIFile(ifname.str());
  2153. if (!rf->exists())
  2154. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA,"The data cannot be compressed.");
  2155. MemoryBuffer out;
  2156. Owned <IFileIO> fio = rf->open(IFOread);
  2157. read(fio, 0, (size32_t) rf->size(), out);
  2158. resp.setThefile(out);
  2159. fio.clear();
  2160. rf->remove();
  2161. resp.setThefile_mimetype((opt > 2) ? "application/x-gzip" : "application/zip");
  2162. context.addCustomerHeader("Content-disposition", headerStr.str());
  2163. #endif
  2164. }
  2165. }
  2166. bool CWsWorkunitsEx::onWUFile(IEspContext &context,IEspWULogFileRequest &req, IEspWULogFileResponse &resp)
  2167. {
  2168. try
  2169. {
  2170. StringBuffer wuidStr = req.getWuid();
  2171. const char* wuidIn = wuidStr.trim().str();
  2172. if (wuidIn && *wuidIn)
  2173. {
  2174. if (!looksLikeAWuid(wuidIn))
  2175. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid Workunit ID");
  2176. ensureWsWorkunitAccess(context, wuidIn, SecAccess_Read);
  2177. }
  2178. StringAttr wuid(wuidIn);
  2179. if (wuid.isEmpty() && notEmpty(req.getQuerySet()) && notEmpty(req.getQuery()))
  2180. {
  2181. Owned<IPropertyTree> registry = getQueryRegistry(req.getQuerySet(), false);
  2182. if (!registry)
  2183. throw MakeStringException(ECLWATCH_QUERYSET_NOT_FOUND, "Queryset %s not found", req.getQuerySet());
  2184. Owned<IPropertyTree> query = resolveQueryAlias(registry, req.getQuery());
  2185. if (!query)
  2186. throw MakeStringException(ECLWATCH_QUERYID_NOT_FOUND, "Query %s not found", req.getQuery());
  2187. resp.setQuerySet(req.getQuerySet());
  2188. resp.setQueryName(query->queryProp("@name"));
  2189. resp.setQueryId(query->queryProp("@id"));
  2190. wuid.set(query->queryProp("@wuid"));
  2191. }
  2192. int opt = req.getOption();
  2193. if (!wuid.isEmpty())
  2194. {
  2195. resp.setWuid(wuid.get());
  2196. MemoryBuffer mb;
  2197. WsWuInfo winfo(context, wuid);
  2198. if (strieq(File_ArchiveQuery, req.getType()))
  2199. {
  2200. winfo.getWorkunitArchiveQuery(mb);
  2201. openSaveFile(context, opt, "ArchiveQuery.xml", HTTP_TYPE_APPLICATION_XML, mb, resp);
  2202. }
  2203. else if (strieq(File_Cpp,req.getType()) && notEmpty(req.getName()))
  2204. {
  2205. winfo.getWorkunitCpp(req.getName(), req.getDescription(), req.getIPAddress(),mb, opt > 0);
  2206. openSaveFile(context, opt, req.getName(), HTTP_TYPE_TEXT_PLAIN, mb, resp);
  2207. }
  2208. else if (strieq(File_DLL,req.getType()))
  2209. {
  2210. StringBuffer name;
  2211. winfo.getWorkunitDll(name, mb);
  2212. resp.setFileName(name.str());
  2213. resp.setDaliServer(daliServers.get());
  2214. openSaveFile(context, opt, req.getName(), HTTP_TYPE_OCTET_STREAM, mb, resp);
  2215. }
  2216. else if (strieq(File_Res,req.getType()))
  2217. {
  2218. winfo.getWorkunitResTxt(mb);
  2219. openSaveFile(context, opt, "res.txt", HTTP_TYPE_TEXT_PLAIN, mb, resp);
  2220. }
  2221. else if (strncmp(req.getType(), File_ThorLog, 7) == 0)
  2222. {
  2223. winfo.getWorkunitThorLog(req.getName(), mb);
  2224. openSaveFile(context, opt, "thormaster.log", HTTP_TYPE_TEXT_PLAIN, mb, resp);
  2225. }
  2226. else if (strieq(File_ThorSlaveLog,req.getType()))
  2227. {
  2228. StringBuffer logDir;
  2229. getConfigurationDirectory(directories, "log", "thor", req.getProcess(), logDir);
  2230. winfo.getWorkunitThorSlaveLog(req.getClusterGroup(), req.getIPAddress(), req.getLogDate(), logDir.str(), req.getSlaveNumber(), mb, false);
  2231. openSaveFile(context, opt, "ThorSlave.log", HTTP_TYPE_TEXT_PLAIN, mb, resp);
  2232. }
  2233. else if (strieq(File_EclAgentLog,req.getType()))
  2234. {
  2235. winfo.getWorkunitEclAgentLog(req.getName(), req.getProcess(), mb);
  2236. openSaveFile(context, opt, "eclagent.log", HTTP_TYPE_TEXT_PLAIN, mb, resp);
  2237. }
  2238. else if (strieq(File_XML,req.getType()) && notEmpty(req.getName()))
  2239. {
  2240. const char* name = req.getName();
  2241. const char* ptr = strrchr(name, '/');
  2242. if (ptr)
  2243. ptr++;
  2244. else
  2245. ptr = name;
  2246. winfo.getWorkunitAssociatedXml(name, req.getIPAddress(), req.getPlainText(), req.getDescription(), opt > 0, mb);
  2247. openSaveFile(context, opt, ptr, HTTP_TYPE_APPLICATION_XML, mb, resp);
  2248. }
  2249. else if (strieq(File_XML,req.getType()) || strieq(File_WUECL,req.getType()))
  2250. {
  2251. StringBuffer mimeType, fileName;
  2252. if (strieq(File_WUECL,req.getType()))
  2253. {
  2254. fileName.setf("%s.ecl", wuid.get());
  2255. winfo.getWorkunitQueryShortText(mb);
  2256. mimeType.set(HTTP_TYPE_TEXT_PLAIN);
  2257. }
  2258. else
  2259. {
  2260. fileName.setf("%s.xml", wuid.get());
  2261. winfo.getWorkunitXml(req.getPlainText(), mb);
  2262. if (opt < 2)
  2263. {
  2264. const char* plainText = req.getPlainText();
  2265. if (plainText && (!stricmp(plainText, "yes")))
  2266. mimeType.set(HTTP_TYPE_TEXT_PLAIN);
  2267. else
  2268. mimeType.set(HTTP_TYPE_APPLICATION_XML);
  2269. }
  2270. else
  2271. {
  2272. mimeType.set(HTTP_TYPE_APPLICATION_XML);
  2273. }
  2274. }
  2275. openSaveFile(context, opt, fileName.str(), mimeType.str(), mb, resp);
  2276. }
  2277. }
  2278. }
  2279. catch(IException* e)
  2280. {
  2281. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2282. }
  2283. return true;
  2284. }
  2285. bool CWsWorkunitsEx::onWUResultBin(IEspContext &context,IEspWUResultBinRequest &req, IEspWUResultBinResponse &resp)
  2286. {
  2287. try
  2288. {
  2289. StringBuffer wuidStr = req.getWuid();
  2290. const char* wuidIn = wuidStr.trim().str();
  2291. if (wuidIn && *wuidIn)
  2292. {
  2293. if (!looksLikeAWuid(wuidIn))
  2294. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid Workunit ID: %s", wuidIn);
  2295. ensureWsWorkunitAccess(context, wuidIn, SecAccess_Read);
  2296. }
  2297. MemoryBuffer mb;
  2298. __int64 total=0;
  2299. __int64 start = req.getStart() > 0 ? req.getStart() : 0;
  2300. unsigned count = req.getCount(), requested=count;
  2301. IArrayOf<IConstNamedValue>* filterBy = &req.getFilterBy();
  2302. SCMStringBuffer name;
  2303. bool bin = (req.getFormat() && strieq(req.getFormat(),"raw"));
  2304. if (notEmpty(wuidIn) && notEmpty(req.getResultName()))
  2305. getWsWuResult(context, wuidIn, req.getResultName(), NULL, 0, start, count, total, name, bin, filterBy, mb);
  2306. else if (notEmpty(wuidIn) && (req.getSequence() >= 0))
  2307. getWsWuResult(context, wuidIn, NULL, NULL, req.getSequence(), start, count, total, name, bin,filterBy, mb);
  2308. else if (notEmpty(req.getLogicalName()))
  2309. {
  2310. const char* logicalName = req.getLogicalName();
  2311. StringBuffer wuid;
  2312. getWuidFromLogicalFileName(context, logicalName, wuid);
  2313. if (!wuid.length())
  2314. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT,"Cannot find the workunit for file %s.",logicalName);
  2315. getWsWuResult(context, wuid.str(), NULL, logicalName, 0, start, count, total, name, bin, filterBy, mb);
  2316. }
  2317. else
  2318. throw MakeStringException(ECLWATCH_CANNOT_GET_WU_RESULT,"Cannot open the workunit result.");
  2319. if(stricmp(req.getFormat(),"xls")==0)
  2320. {
  2321. Owned<IProperties> params(createProperties());
  2322. params->setProp("showCount",0);
  2323. StringBuffer xml;
  2324. xml.append("<WUResultExcel><Result>").append(mb.length(), mb.toByteArray()).append("</Result></WUResultExcel>");
  2325. if (xml.length() > MAXXLSTRANSFER)
  2326. throw MakeStringException(ECLWATCH_TOO_BIG_DATA_SET, "The data set is too big to be converted to an Excel file. Please use the gzip link to download a compressed XML data file.");
  2327. StringBuffer xls;
  2328. xsltTransform(xml.str(), StringBuffer(getCFD()).append("./smc_xslt/result.xslt").str(), params, xls);
  2329. MemoryBuffer out;
  2330. out.setBuffer(xls.length(), (void*)xls.str());
  2331. resp.setResult(out);
  2332. resp.setResult_mimetype("application/vnd.ms-excel");
  2333. }
  2334. #ifdef _USE_ZLIB
  2335. else if(strieq(req.getFormat(),"zip") || strieq(req.getFormat(),"gzip"))
  2336. {
  2337. bool gzip = strieq(req.getFormat(),"gzip");
  2338. StringBuffer xml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  2339. xml.append("<Result>").append(mb.length(),mb.toByteArray()).append("</Result>");
  2340. VStringBuffer ifname("%s%sT%xAT%x%s", TEMPZIPDIR, PATHSEPSTR, (unsigned)(memsize_t)GetCurrentThreadId(), msTick(), gzip ? "" : ".zip");
  2341. IZZIPor* Zipor = createZZIPor();
  2342. int ret = 0;
  2343. if (gzip)
  2344. ret = Zipor->gzipToFile(xml.length(), (void*)xml.str(), ifname.str());
  2345. else
  2346. ret = Zipor->zipToFile(xml.length(), (void*)xml.str(), "WUResult.xml", ifname.str());
  2347. releaseIZ(Zipor);
  2348. if (ret < 0)
  2349. {
  2350. Owned<IFile> rFile = createIFile(ifname.str());
  2351. if (rFile->exists())
  2352. rFile->remove();
  2353. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA, "The data cannot be compressed.");
  2354. }
  2355. MemoryBuffer out;
  2356. Owned <IFile> rf = createIFile(ifname.str());
  2357. if (rf->exists())
  2358. {
  2359. Owned <IFileIO> fio = rf->open(IFOread);
  2360. read(fio, 0, (size32_t) rf->size(), out);
  2361. resp.setResult(out);
  2362. }
  2363. if (gzip)
  2364. {
  2365. resp.setResult_mimetype("application/x-gzip");
  2366. context.addCustomerHeader("Content-disposition", "attachment;filename=WUResult.xml.gz");
  2367. }
  2368. else
  2369. {
  2370. resp.setResult_mimetype("application/zip");
  2371. context.addCustomerHeader("Content-disposition", "attachment;filename=WUResult.xml.zip");
  2372. }
  2373. Owned<IFile> rFile = createIFile(ifname.str());
  2374. if (rFile->exists())
  2375. rFile->remove();
  2376. }
  2377. #endif
  2378. else
  2379. {
  2380. resp.setResult(mb);
  2381. }
  2382. resp.setName(name.str());
  2383. resp.setWuid(wuidIn);
  2384. resp.setSequence(req.getSequence());
  2385. resp.setStart(start);
  2386. if (requested > total)
  2387. requested = (unsigned)total;
  2388. resp.setRequested(requested);
  2389. resp.setCount(count);
  2390. resp.setTotal(total);
  2391. resp.setFormat(req.getFormat());
  2392. }
  2393. catch(IException* e)
  2394. {
  2395. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2396. }
  2397. return true;
  2398. }
  2399. bool CWsWorkunitsEx::onWUResultSummary(IEspContext &context, IEspWUResultSummaryRequest &req, IEspWUResultSummaryResponse &resp)
  2400. {
  2401. try
  2402. {
  2403. StringBuffer wuid = req.getWuid();
  2404. WsWuHelpers::checkAndTrimWorkunit("WUResultSummary", wuid);
  2405. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2406. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  2407. if(!cw)
  2408. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  2409. ensureWsWorkunitAccess(context, *cw, SecAccess_Read);
  2410. resp.setWuid(wuid.str());
  2411. resp.setSequence(req.getSequence());
  2412. IArrayOf<IEspECLResult> results;
  2413. Owned<IConstWUResult> r = cw->getResultBySequence(req.getSequence());
  2414. if (r)
  2415. {
  2416. WsWuInfo winfo(context, cw);
  2417. winfo.getResult(*r, results, 0);
  2418. resp.setFormat(r->getResultFormat());
  2419. resp.setResult(results.item(0));
  2420. }
  2421. }
  2422. catch(IException* e)
  2423. {
  2424. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2425. }
  2426. return true;
  2427. }
  2428. void getFileResults(IEspContext &context, const char* logicalName, const char* cluster,__int64 start, unsigned& count,__int64& total,
  2429. IStringVal& resname,bool bin, IArrayOf<IConstNamedValue>* filterBy, MemoryBuffer& buf, bool xsd)
  2430. {
  2431. Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), context.queryUser(), context.queryUserId(), context.queryPassword());
  2432. Owned<INewResultSet> result(resultSetFactory->createNewFileResultSet(logicalName, cluster));
  2433. if (!filterBy || !filterBy->length())
  2434. appendResultSet(buf, result, resname.str(), start, count, total, bin, xsd, context.getResponseFormat(), NULL);
  2435. else
  2436. {
  2437. Owned<INewResultSet> filteredResult = createFilteredResultSet(result, filterBy);
  2438. appendResultSet(buf, filteredResult, resname.str(), start, count, total, bin, xsd, context.getResponseFormat(), NULL);
  2439. }
  2440. }
  2441. void getWorkunitCluster(IEspContext &context, const char* wuid, SCMStringBuffer& cluster, bool checkArchiveWUs)
  2442. {
  2443. if (isEmpty(wuid))
  2444. return;
  2445. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2446. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid, false);
  2447. if (cw)
  2448. cluster.set(cw->queryClusterName());
  2449. else if (checkArchiveWUs)
  2450. {
  2451. Owned<IPropertyTree> wuProps;// = getArchivedWorkUnitProperties(wuid);
  2452. if (wuProps)
  2453. cluster.set(wuProps->queryProp("@clusterName"));
  2454. }
  2455. }
  2456. bool CWsWorkunitsEx::onWUResult(IEspContext &context, IEspWUResultRequest &req, IEspWUResultResponse &resp)
  2457. {
  2458. try
  2459. {
  2460. StringBuffer wuidStr = req.getWuid();
  2461. const char* wuid = wuidStr.trim().str();
  2462. if (wuid && *wuid)
  2463. {
  2464. if (!looksLikeAWuid(wuid))
  2465. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid Workunit ID: %s", wuid);
  2466. ensureWsWorkunitAccess(context, wuid, SecAccess_Read);
  2467. }
  2468. MemoryBuffer mb;
  2469. SCMStringBuffer name;
  2470. __int64 total=0;
  2471. __int64 start = req.getStart() > 0 ? req.getStart() : 0;
  2472. unsigned count=req.getCount() ? req.getCount() : 100, requested=count;
  2473. unsigned seq = req.getSequence();
  2474. bool inclXsd = !req.getSuppressXmlSchema();
  2475. VStringBuffer filter("start=%"I64F"d;count=%d", start, count);
  2476. addToQueryString(filter, "clusterName", req.getCluster(), ';');
  2477. addToQueryString(filter, "logicalName", req.getLogicalName(), ';');
  2478. if (wuid && *wuid)
  2479. addToQueryString(filter, "wuid", wuid, ';');
  2480. addToQueryString(filter, "resultName", req.getResultName(), ';');
  2481. filter.appendf(";seq=%d;", seq);
  2482. if (inclXsd)
  2483. filter.append("xsd;");
  2484. if (context.getResponseFormat()==ESPSerializationJSON)
  2485. filter.append("json;");
  2486. IArrayOf<IConstNamedValue>* filterBy = &req.getFilterBy();
  2487. ForEachItemIn(i, *filterBy)
  2488. {
  2489. IConstNamedValue &item = filterBy->item(i);
  2490. const char *name = item.getName();
  2491. const char *value = item.getValue();
  2492. if (name && *name && value && *value)
  2493. addToQueryString(filter, name, value, ';');
  2494. }
  2495. const char* logicalName = req.getLogicalName();
  2496. const char* clusterName = req.getCluster();
  2497. const char* resultName = req.getResultName();
  2498. Owned<DataCacheElement> data = dataCache->lookup(context, filter, awusCacheMinutes);
  2499. if (data)
  2500. {
  2501. mb.append(data->m_data.c_str());
  2502. name.set(data->m_name.c_str());
  2503. logicalName = data->m_logicalName.c_str();
  2504. wuid = data->m_wuid.c_str();
  2505. resultName = data->m_resultName.c_str();
  2506. seq = data->m_seq;
  2507. start = data->m_start;
  2508. count = data->m_rowcount;
  2509. requested = (unsigned)data->m_requested;
  2510. total = data->m_total;
  2511. if (notEmpty(logicalName))
  2512. resp.setLogicalName(logicalName);
  2513. else
  2514. {
  2515. if (notEmpty(wuid))
  2516. resp.setWuid(wuid);
  2517. resp.setSequence(seq);
  2518. }
  2519. }
  2520. else
  2521. {
  2522. if(logicalName && *logicalName)
  2523. {
  2524. StringBuffer lwuid;
  2525. getWuidFromLogicalFileName(context, logicalName, lwuid);
  2526. SCMStringBuffer cluster;
  2527. if (lwuid.length())
  2528. getWorkunitCluster(context, lwuid.str(), cluster, true);
  2529. if (cluster.length())
  2530. {
  2531. getFileResults(context, logicalName, cluster.str(), start, count, total, name, false, filterBy, mb, inclXsd);
  2532. resp.setLogicalName(logicalName);
  2533. }
  2534. else if (notEmpty(clusterName))
  2535. {
  2536. getFileResults(context, logicalName, clusterName, start, count, total, name, false, filterBy, mb, inclXsd);
  2537. resp.setLogicalName(logicalName);
  2538. }
  2539. else
  2540. throw MakeStringException(ECLWATCH_INVALID_INPUT,"Need valid target cluster to browse file %s.",logicalName);
  2541. }
  2542. else if (notEmpty(wuid) && notEmpty(resultName))
  2543. {
  2544. name.set(resultName);
  2545. getWsWuResult(context, wuid, resultName, NULL, 0, start, count, total, name, false, filterBy, mb, inclXsd);
  2546. resp.setWuid(wuid);
  2547. resp.setSequence(seq);
  2548. }
  2549. else
  2550. {
  2551. getWsWuResult(context, wuid, NULL, NULL, seq, start, count, total, name, false, filterBy, mb, inclXsd);
  2552. resp.setWuid(wuid);
  2553. resp.setSequence(seq);
  2554. }
  2555. mb.append(0);
  2556. if (requested > total)
  2557. requested = (unsigned)total;
  2558. dataCache->add(filter, mb.toByteArray(), name.str(), logicalName, wuid, resultName, seq, start, count, requested, total);
  2559. }
  2560. resp.setName(name.str());
  2561. resp.setStart(start);
  2562. if (clusterName && *clusterName)
  2563. resp.setCluster(clusterName);
  2564. resp.setRequested(requested);
  2565. resp.setCount(count);
  2566. resp.setTotal(total);
  2567. resp.setResult(mb.toByteArray());
  2568. context.queryXslParameters()->setProp("escapeResults","1");
  2569. }
  2570. catch(IException* e)
  2571. {
  2572. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2573. }
  2574. return true;
  2575. }
  2576. void getScheduledWUs(IEspContext &context, const char *stateReq, const char *serverName, const char *eventName, IArrayOf<IEspScheduledWU> & results)
  2577. {
  2578. double version = context.getClientVersion();
  2579. if (notEmpty(serverName))
  2580. {
  2581. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2582. Owned<IScheduleReader> reader = getScheduleReader(serverName, eventName);
  2583. Owned<IScheduleReaderIterator> it(reader->getIterator());
  2584. while(it->isValidEventName())
  2585. {
  2586. StringBuffer ieventName;
  2587. it->getEventName(ieventName);
  2588. while(it->isValidEventText())
  2589. {
  2590. StringBuffer ieventText;
  2591. it->getEventText(ieventText);
  2592. while(it->isValidWuid())
  2593. {
  2594. StringBuffer wuid;
  2595. it->getWuid(wuid);
  2596. if (wuid.length())
  2597. {
  2598. bool match = false;
  2599. unsigned stateID = WUStateUnknown;
  2600. StringBuffer jobName, owner;
  2601. SCMStringBuffer state;
  2602. try
  2603. {
  2604. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  2605. if (!cw && (!stateReq || !*stateReq))
  2606. match = true;
  2607. else if (cw)
  2608. {
  2609. if ((cw->getState() == WUStateScheduled) && cw->aborting())
  2610. {
  2611. stateID = WUStateAborting;
  2612. state.set("aborting");
  2613. }
  2614. else
  2615. {
  2616. stateID = cw->getState();
  2617. state.set(cw->queryStateDesc());
  2618. }
  2619. if (!stateReq || !*stateReq || strieq(stateReq, state.str()))
  2620. {
  2621. match = true;
  2622. jobName.set(cw->queryJobName());
  2623. owner.set(cw->queryUser());
  2624. }
  2625. }
  2626. }
  2627. catch (IException *e)
  2628. {
  2629. EXCLOG(e, "Get scheduled WUs");
  2630. e->Release();
  2631. }
  2632. if (!match)
  2633. {
  2634. it->nextWuid();
  2635. continue;
  2636. }
  2637. Owned<IEspScheduledWU> scheduledWU = createScheduledWU("");
  2638. scheduledWU->setWuid(wuid.str());
  2639. scheduledWU->setCluster(serverName);
  2640. if (ieventName.length())
  2641. scheduledWU->setEventName(ieventName.str());
  2642. if (ieventText.str())
  2643. scheduledWU->setEventText(ieventText.str());
  2644. if (jobName.length())
  2645. scheduledWU->setJobName(jobName.str());
  2646. if (version >= 1.51)
  2647. {
  2648. if (owner.length())
  2649. scheduledWU->setOwner(owner.str());
  2650. if (state.length())
  2651. {
  2652. scheduledWU->setStateID(stateID);
  2653. scheduledWU->setState(state.str());
  2654. }
  2655. }
  2656. results.append(*scheduledWU.getLink());
  2657. }
  2658. it->nextWuid();
  2659. }
  2660. it->nextEventText();
  2661. }
  2662. it->nextEventName();
  2663. }
  2664. }
  2665. return;
  2666. }
  2667. bool CWsWorkunitsEx::onWUShowScheduled(IEspContext &context, IEspWUShowScheduledRequest & req, IEspWUShowScheduledResponse & resp)
  2668. {
  2669. try
  2670. {
  2671. DBGLOG("WUShowScheduled");
  2672. const char *clusterName = req.getCluster();
  2673. const char *eventName = req.getEventName();
  2674. const char *state = req.getState();
  2675. IArrayOf<IEspScheduledWU> results;
  2676. if(notEmpty(req.getPushEventName()))
  2677. resp.setPushEventName(req.getPushEventName());
  2678. if(notEmpty(req.getPushEventText()))
  2679. resp.setPushEventText(req.getPushEventText());
  2680. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  2681. Owned<IConstEnvironment> environment = factory->openEnvironment();
  2682. Owned<IPropertyTree> root = &environment->getPTree();
  2683. if (!root)
  2684. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get environment information.");
  2685. unsigned i = 0;
  2686. Owned<IPropertyTreeIterator> ic = root->getElements("Software/Topology/Cluster");
  2687. IArrayOf<IEspServerInfo> servers;
  2688. ForEach(*ic)
  2689. {
  2690. IPropertyTree &cluster = ic->query();
  2691. const char *iclusterName = cluster.queryProp("@name");
  2692. if (isEmpty(iclusterName))
  2693. continue;
  2694. if(isEmpty(clusterName))
  2695. getScheduledWUs(context, state, iclusterName, eventName, results);
  2696. else if (strieq(clusterName, iclusterName))
  2697. {
  2698. getScheduledWUs(context, state, clusterName, eventName, results);
  2699. resp.setClusterSelected(i+1);
  2700. }
  2701. Owned<IEspServerInfo> server = createServerInfo("");
  2702. server->setName(iclusterName);
  2703. servers.append(*server.getLink());
  2704. i++;
  2705. }
  2706. if (servers.length())
  2707. resp.setClusters(servers);
  2708. if (results.length())
  2709. resp.setWorkunits(results);
  2710. bool first=false;
  2711. StringBuffer Query("PageFrom=Scheduler");
  2712. appendUrlParameter(Query, "EventName", eventName, first);
  2713. appendUrlParameter(Query, "ECluster", clusterName, first);
  2714. resp.setQuery(Query.str());
  2715. }
  2716. catch(IException* e)
  2717. {
  2718. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2719. }
  2720. return true;
  2721. }
  2722. bool CWsWorkunitsEx::onWUExport(IEspContext &context, IEspWUExportRequest &req, IEspWUExportResponse &resp)
  2723. {
  2724. try
  2725. {
  2726. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2727. WsWuSearch ws(context, req.getOwner(), req.getState(), req.getCluster(), req.getStartDate(), req.getEndDate(), req.getECL(), req.getJobname());
  2728. StringBuffer xml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workunits>");
  2729. for(WsWuSearch::iterator it=ws.begin(); it!=ws.end(); it++)
  2730. {
  2731. Owned<IConstWorkUnit> cw = factory->openWorkUnit(it->c_str(), false);
  2732. if (cw)
  2733. exportWorkUnitToXML(cw, xml, true, false);
  2734. }
  2735. xml.append("</Workunits>");
  2736. MemoryBuffer mb;
  2737. mb.setBuffer(xml.length(),(void*)xml.str());
  2738. resp.setExportData(mb);
  2739. resp.setExportData_mimetype(HTTP_TYPE_APPLICATION_XML);
  2740. }
  2741. catch(IException* e)
  2742. {
  2743. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2744. }
  2745. return true;
  2746. }
  2747. bool CWsWorkunitsEx::onWUListLocalFileRequired(IEspContext& context, IEspWUListLocalFileRequiredRequest& req, IEspWUListLocalFileRequiredResponse& resp)
  2748. {
  2749. try
  2750. {
  2751. StringBuffer wuid = req.getWuid();
  2752. WsWuHelpers::checkAndTrimWorkunit("WUListLocalFileRequired", wuid);
  2753. ensureWsWorkunitAccess(context, wuid.str(), SecAccess_Read);
  2754. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2755. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  2756. if (!cw)
  2757. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT, "Workunit %s not found.", wuid.str());
  2758. IArrayOf<IEspLogicalFileUpload> localFiles;
  2759. Owned<IConstLocalFileUploadIterator> it = cw->getLocalFileUploads();
  2760. ForEach(*it)
  2761. {
  2762. Owned<IConstLocalFileUpload> file = it->get();
  2763. if(!file)
  2764. continue;
  2765. Owned<IEspLogicalFileUpload> up = createLogicalFileUpload();
  2766. SCMStringBuffer s;
  2767. up->setType(file->queryType());
  2768. up->setSource(file->getSource(s).str());
  2769. up->setDestination(file->getDestination(s).str());
  2770. up->setEventTag(file->getEventTag(s).str());
  2771. localFiles.append(*up.getLink());
  2772. }
  2773. resp.setLocalFileUploads(localFiles);
  2774. }
  2775. catch(IException* e)
  2776. {
  2777. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2778. }
  2779. return true;
  2780. }
  2781. typedef enum wsEclTypes_
  2782. {
  2783. wsEclTypeUnknown,
  2784. xsdString,
  2785. xsdBoolean,
  2786. xsdDecimal,
  2787. xsdFloat,
  2788. xsdDouble,
  2789. xsdDuration,
  2790. xsdDateTime,
  2791. xsdTime,
  2792. xsdDate,
  2793. xsdYearMonth,
  2794. xsdYear,
  2795. xsdMonthDay,
  2796. xsdDay,
  2797. xsdMonth,
  2798. xsdHexBinary,
  2799. xsdBase64Binary,
  2800. xsdAnyURI,
  2801. xsdQName,
  2802. xsdNOTATION,
  2803. xsdNormalizedString,
  2804. xsdToken,
  2805. xsdLanguage,
  2806. xsdNMTOKEN,
  2807. xsdNMTOKENS,
  2808. xsdName,
  2809. xsdNCName,
  2810. xsdID,
  2811. xsdIDREF,
  2812. xsdIDREFS,
  2813. xsdENTITY,
  2814. xsdENTITIES,
  2815. xsdInteger,
  2816. xsdNonPositiveInteger,
  2817. xsdNegativeInteger,
  2818. xsdLong,
  2819. xsdInt,
  2820. xsdShort,
  2821. xsdByte,
  2822. xsdNonNegativeInteger,
  2823. xsdUnsignedLong,
  2824. xsdUnsignedInt,
  2825. xsdUnsignedShort,
  2826. xsdUnsignedByte,
  2827. xsdPositiveInteger,
  2828. tnsRawDataFile,
  2829. tnsCsvDataFile,
  2830. tnsEspStringArray,
  2831. tnsEspIntArray,
  2832. tnsXmlDataSet,
  2833. maxWsEclType
  2834. } wsEclType;
  2835. bool CWsWorkunitsEx::onWUAddLocalFileToWorkunit(IEspContext& context, IEspWUAddLocalFileToWorkunitRequest& req, IEspWUAddLocalFileToWorkunitResponse& resp)
  2836. {
  2837. try
  2838. {
  2839. StringBuffer wuid = req.getWuid();
  2840. WsWuHelpers::checkAndTrimWorkunit("WUAddLocalFileToWorkunit", wuid);
  2841. ensureWsWorkunitAccess(context, wuid.str(), SecAccess_Write);
  2842. resp.setWuid(wuid.str());
  2843. const char* varname = req.getName();
  2844. if (isEmpty(varname))
  2845. {
  2846. resp.setResult("Name is not defined!");
  2847. return true;
  2848. }
  2849. resp.setName(varname);
  2850. wsEclType type = (wsEclType) req.getType();
  2851. const char *val = req.getVal();
  2852. unsigned len = req.getLength();
  2853. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2854. WorkunitUpdate wu(factory->updateWorkUnit(wuid.str()));
  2855. if (!wu)
  2856. {
  2857. resp.setResult("Workunit not found!");
  2858. return true;
  2859. }
  2860. Owned<IWUResult> wuRslt = wu->updateResultByName(varname);
  2861. if (isEmpty(val))
  2862. val=req.getDefVal();
  2863. if (notEmpty(val))
  2864. {
  2865. switch (type)
  2866. {
  2867. case xsdBoolean:
  2868. wuRslt->setResultBool((strieq(val, "1") || strieq(val, "true") || strieq(val, "on")));
  2869. wuRslt->setResultStatus(ResultStatusSupplied);
  2870. break;
  2871. case xsdDecimal:
  2872. case xsdFloat:
  2873. case xsdDouble:
  2874. wuRslt->setResultReal(atof(val));
  2875. wuRslt->setResultStatus(ResultStatusSupplied);
  2876. break;
  2877. case xsdInteger:
  2878. case xsdNonPositiveInteger:
  2879. case xsdNegativeInteger:
  2880. case xsdLong:
  2881. case xsdInt:
  2882. case xsdShort:
  2883. case xsdByte:
  2884. case xsdNonNegativeInteger:
  2885. case xsdUnsignedLong:
  2886. case xsdUnsignedInt:
  2887. case xsdUnsignedShort:
  2888. case xsdUnsignedByte:
  2889. case xsdPositiveInteger:
  2890. wuRslt->setResultInt(_atoi64(val));
  2891. wuRslt->setResultStatus(ResultStatusSupplied);
  2892. break;
  2893. case tnsEspIntArray:
  2894. case tnsEspStringArray:
  2895. wuRslt->setResultRaw(len, val, ResultFormatXmlSet);
  2896. break;
  2897. case tnsRawDataFile:
  2898. wuRslt->setResultRaw(len, val, ResultFormatRaw);
  2899. break;
  2900. case tnsXmlDataSet:
  2901. wuRslt->setResultRaw(len, val, ResultFormatXml);
  2902. break;
  2903. case tnsCsvDataFile:
  2904. case xsdBase64Binary: //tbd
  2905. case xsdHexBinary:
  2906. break;
  2907. default:
  2908. wuRslt->setResultString(val, len);
  2909. wuRslt->setResultStatus(ResultStatusSupplied);
  2910. break;
  2911. }
  2912. }
  2913. resp.setResult("Result has been set as required!");
  2914. }
  2915. catch(IException* e)
  2916. {
  2917. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2918. }
  2919. return true;
  2920. }
  2921. void getClusterConfig(char const * clusterType, char const * clusterName, char const * processName, StringBuffer& netAddress)
  2922. {
  2923. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  2924. Owned<IConstEnvironment> environment = factory->openEnvironment();
  2925. Owned<IPropertyTree> pRoot = &environment->getPTree();
  2926. VStringBuffer xpath("Software/%s[@name='%s']", clusterType, clusterName);
  2927. IPropertyTree* pCluster = pRoot->queryPropTree(xpath.str());
  2928. if (!pCluster)
  2929. throw MakeStringException(ECLWATCH_CLUSTER_NOT_IN_ENV_INFO, "'%s %s' is not defined.", clusterType, clusterName);
  2930. const char* port = pCluster->queryProp(xpath.set(processName).append("@port").str());
  2931. const char* computer = pCluster->queryProp(xpath.set(processName).append("@computer").str());
  2932. if (isEmpty(computer))
  2933. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_INFO, "'%s %s: %s' is not defined.", clusterType, clusterName, processName);
  2934. Owned<IConstMachineInfo> pMachine = environment->getMachine(computer);
  2935. if (pMachine)
  2936. {
  2937. StringBufferAdaptor s(netAddress);
  2938. pMachine->getNetAddress(s);
  2939. #ifdef MACHINE_IP
  2940. if (streq(netAddress.str(), "."))
  2941. netAddress = MACHINE_IP;
  2942. #endif
  2943. netAddress.append(':').append(port);
  2944. }
  2945. return;
  2946. }
  2947. bool CWsWorkunitsEx::onWUProcessGraph(IEspContext &context,IEspWUProcessGraphRequest &req, IEspWUProcessGraphResponse &resp)
  2948. {
  2949. try
  2950. {
  2951. StringBuffer wuid = req.getWuid();
  2952. WsWuHelpers::checkAndTrimWorkunit("WUProcessGraph", wuid);
  2953. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2954. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  2955. if(!cw)
  2956. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  2957. ensureWsWorkunitAccess(context, *cw, SecAccess_Read);
  2958. Owned <IConstWUGraph> graph = cw->getGraph(req.getName());
  2959. Owned <IPropertyTree> xgmml = graph->getXGMMLTree(true); // merge in graph progress information
  2960. StringBuffer xml;
  2961. resp.setTheGraph(toXML(xgmml.get(), xml).str());
  2962. }
  2963. catch(IException* e)
  2964. {
  2965. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2966. }
  2967. return true;
  2968. }
  2969. bool isRunning(IConstWorkUnit &cw)
  2970. {
  2971. // MORE - move into workunit interface
  2972. switch (cw.getState())
  2973. {
  2974. case WUStateFailed:
  2975. case WUStateAborted:
  2976. case WUStateCompleted:
  2977. return false;
  2978. default:
  2979. return true;
  2980. }
  2981. }
  2982. bool CWsWorkunitsEx::onWUGetGraph(IEspContext& context, IEspWUGetGraphRequest& req, IEspWUGetGraphResponse& resp)
  2983. {
  2984. try
  2985. {
  2986. StringBuffer wuid = req.getWuid();
  2987. WsWuHelpers::checkAndTrimWorkunit("WUGetGraph", wuid);
  2988. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2989. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  2990. if(!cw)
  2991. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  2992. ensureWsWorkunitAccess(context, *cw, SecAccess_Read);
  2993. WUGraphIDType id;
  2994. SCMStringBuffer runningGraph;
  2995. bool running = (isRunning(*cw) && cw->getRunningGraph(runningGraph,id));
  2996. IArrayOf<IEspECLGraphEx> graphs;
  2997. Owned<IConstWUGraphIterator> it;
  2998. IConstWUGraph *graph = NULL;
  2999. if (isEmpty(req.getGraphName())) // JCS->GS - is this really required??
  3000. {
  3001. it.setown(&cw->getGraphs(GraphTypeAny));
  3002. if (it->first())
  3003. graph = &it->query();
  3004. }
  3005. else
  3006. graph = cw->getGraph(req.getGraphName());
  3007. while (graph)
  3008. {
  3009. SCMStringBuffer name, label, type;
  3010. graph->getName(name);
  3011. graph->getLabel(label);
  3012. graph->getTypeName(type);
  3013. Owned<IEspECLGraphEx> g = createECLGraphEx("","");
  3014. g->setName(name.str());
  3015. g->setLabel(label.str());
  3016. g->setType(type.str());
  3017. WUGraphState graphState = graph->getState();
  3018. if (running && (WUGraphRunning == graphState))
  3019. {
  3020. g->setRunning(true);
  3021. g->setRunningId(id);
  3022. }
  3023. else if (context.getClientVersion() > 1.20)
  3024. {
  3025. if (WUGraphComplete == graphState)
  3026. g->setComplete(true);
  3027. else if (WUGraphFailed == graphState)
  3028. g->setFailed(true);
  3029. }
  3030. Owned<IPropertyTree> xgmml = graph->getXGMMLTree(true);
  3031. // New functionality, if a subgraph id is specified and we only want to load the xgmml for that subgraph
  3032. // then we need to conditionally pull a propertytree from the xgmml graph one and use that for the xgmml.
  3033. //JCSMORE this should be part of the API and therefore allow *only* the subtree to be pulled from the backend.
  3034. StringBuffer xml;
  3035. if (notEmpty(req.getSubGraphId()))
  3036. {
  3037. VStringBuffer xpath("//node[@id='%s']", req.getSubGraphId());
  3038. toXML(xgmml->queryPropTree(xpath.str()), xml);
  3039. }
  3040. else
  3041. toXML(xgmml, xml);
  3042. g->setGraph(xml.str());
  3043. graphs.append(*g.getClear());
  3044. if (!it || !it->next())
  3045. break;
  3046. graph = &it->query();
  3047. }
  3048. resp.setGraphs(graphs);
  3049. }
  3050. catch(IException* e)
  3051. {
  3052. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3053. }
  3054. return true;
  3055. }
  3056. bool CWsWorkunitsEx::onGVCAjaxGraph(IEspContext &context, IEspGVCAjaxGraphRequest &req, IEspGVCAjaxGraphResponse &resp)
  3057. {
  3058. try
  3059. {
  3060. resp.setName(req.getName());
  3061. resp.setGraphName(req.getGraphName());
  3062. resp.setGraphType("eclwatch");
  3063. double version = context.getClientVersion();
  3064. if (version > 1.19)
  3065. resp.setSubGraphId(req.getSubGraphId());
  3066. if (version > 1.20)
  3067. resp.setSubGraphOnly(req.getSubGraphOnly());
  3068. }
  3069. catch(IException* e)
  3070. {
  3071. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3072. }
  3073. return true;
  3074. }
  3075. bool CWsWorkunitsEx::onWUGraphInfo(IEspContext &context,IEspWUGraphInfoRequest &req, IEspWUGraphInfoResponse &resp)
  3076. {
  3077. try
  3078. {
  3079. StringBuffer wuid = req.getWuid();
  3080. WsWuHelpers::checkAndTrimWorkunit("WUGraphInfo", wuid);
  3081. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  3082. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  3083. if(!cw)
  3084. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  3085. ensureWsWorkunitAccess(context, *cw, SecAccess_Write);
  3086. resp.setWuid(wuid.str());
  3087. resp.setName(req.getName());
  3088. resp.setRunning(isRunning(*cw));
  3089. if (notEmpty(req.getGID()))
  3090. resp.setGID(req.getGID());
  3091. if(!req.getBatchWU_isNull())
  3092. resp.setBatchWU(req.getBatchWU());
  3093. }
  3094. catch(IException* e)
  3095. {
  3096. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3097. }
  3098. return true;
  3099. }
  3100. bool CWsWorkunitsEx::onWUGVCGraphInfo(IEspContext &context,IEspWUGVCGraphInfoRequest &req, IEspWUGVCGraphInfoResponse &resp)
  3101. {
  3102. try
  3103. {
  3104. StringBuffer wuid = req.getWuid();
  3105. WsWuHelpers::checkAndTrimWorkunit("WUGVCGraphInfo", wuid);
  3106. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  3107. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  3108. if(!cw)
  3109. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  3110. ensureWsWorkunitAccess(context, *cw, SecAccess_Read);
  3111. resp.setWuid(wuid.str());
  3112. resp.setName(req.getName());
  3113. resp.setRunning(isRunning(*cw));
  3114. if (notEmpty(req.getGID()))
  3115. resp.setGID(req.getGID());
  3116. if(!req.getBatchWU_isNull())
  3117. resp.setBatchWU(req.getBatchWU());
  3118. StringBuffer xml("<Control><Endpoint><Query id=\"Gordon.Extractor.0\">");
  3119. xml.appendf("<Graph id=\"%s\">", req.getName());
  3120. if (context.getClientVersion() > 1.17)
  3121. {
  3122. xml.append("<Subgraph>");
  3123. xml.append(req.getSubgraphId_isNull() ? 0 : req.getSubgraphId());
  3124. xml.append("</Subgraph>");
  3125. }
  3126. xml.append("</Graph></Query></Endpoint></Control>");
  3127. resp.setTheGraph(xml.str());
  3128. }
  3129. catch(IException* e)
  3130. {
  3131. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3132. }
  3133. return true;
  3134. }
  3135. bool CWsWorkunitsEx::onWUGraphTiming(IEspContext &context, IEspWUGraphTimingRequest &req, IEspWUGraphTimingResponse &resp)
  3136. {
  3137. try
  3138. {
  3139. StringBuffer wuid = req.getWuid();
  3140. WsWuHelpers::checkAndTrimWorkunit("WUGraphTiming", wuid);
  3141. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  3142. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid.str(), false);
  3143. if(!cw)
  3144. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  3145. ensureWsWorkunitAccess(context, *cw, SecAccess_Read);
  3146. resp.updateWorkunit().setWuid(wuid.str());
  3147. WsWuInfo winfo(context, cw);
  3148. IArrayOf<IConstECLTimingData> timingData;
  3149. winfo.getGraphTimingData(timingData, 0);
  3150. resp.updateWorkunit().setTimingData(timingData);
  3151. }
  3152. catch(IException* e)
  3153. {
  3154. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3155. }
  3156. return true;
  3157. }
  3158. int CWsWorkunitsSoapBindingEx::onGetForm(IEspContext &context, CHttpRequest* request, CHttpResponse* response, const char *service, const char *method)
  3159. {
  3160. try
  3161. {
  3162. StringBuffer xml;
  3163. StringBuffer xslt;
  3164. if(strieq(method,"WUQuery") || strieq(method,"WUJobList"))
  3165. {
  3166. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  3167. Owned<IConstEnvironment> environment = factory->openEnvironment();
  3168. Owned<IPropertyTree> root = &environment->getPTree();
  3169. if (!root)
  3170. throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO, "Failed to get environment information.");
  3171. if(strieq(method,"WUQuery"))
  3172. {
  3173. SecAccessFlags accessOwn;
  3174. SecAccessFlags accessOthers;
  3175. getUserWuAccessFlags(context, accessOwn, accessOthers, false);
  3176. xml.append("<WUQuery>");
  3177. if ((accessOwn == SecAccess_None) && (accessOthers == SecAccess_None))
  3178. xml.appendf("<ErrorMessage>Access to workunit is denied.</ErrorMessage>");
  3179. else
  3180. {
  3181. MapStringTo<bool> added;
  3182. Owned<IPropertyTreeIterator> it = root->getElements("Software/Topology/Cluster");
  3183. ForEach(*it)
  3184. {
  3185. const char *name = it->query().queryProp("@name");
  3186. if (notEmpty(name) && !added.getValue(name))
  3187. {
  3188. added.setValue(name, true);
  3189. appendXMLTag(xml, "Cluster", name);
  3190. }
  3191. }
  3192. }
  3193. xml.append("</WUQuery>");
  3194. xslt.append(getCFD()).append("./smc_xslt/wuid_search.xslt");
  3195. }
  3196. else if (strieq(method,"WUJobList"))
  3197. {
  3198. StringBuffer cluster, defaultProcess, range;
  3199. request->getParameter("Cluster", cluster);
  3200. request->getParameter("Process",defaultProcess);
  3201. request->getParameter("Range",range);
  3202. Owned<IConstWUClusterInfo> clusterInfo = getTargetClusterInfo(cluster);
  3203. xml.append("<WUJobList>");
  3204. if (range.length())
  3205. appendXMLTag(xml, "Range", range.str());
  3206. if (clusterInfo)
  3207. {
  3208. const StringArray &thorInstances = clusterInfo->getThorProcesses();
  3209. ForEachItemIn(i, thorInstances)
  3210. {
  3211. const char* instance = thorInstances.item(i);
  3212. if (defaultProcess.length() && strieq(instance, defaultProcess.str()))
  3213. xml.append("<Cluster selected=\"1\">").append(instance).append("</Cluster>");
  3214. else
  3215. xml.append("<Cluster>").append(instance).append("</Cluster>");
  3216. }
  3217. }
  3218. xml.append("<TargetCluster>").append(cluster).append("</TargetCluster>");
  3219. xml.append("</WUJobList>");
  3220. xslt.append(getCFD()).append("./smc_xslt/jobs_search.xslt");
  3221. response->addHeader("Expires", "0");
  3222. }
  3223. }
  3224. if (xslt.length() && xml.length())
  3225. {
  3226. StringBuffer html;
  3227. xsltTransform(xml.str(), xslt.str(), NULL, html);
  3228. response->setContent(html.str());
  3229. response->setContentType(HTTP_TYPE_TEXT_HTML_UTF8);
  3230. response->send();
  3231. return 0;
  3232. }
  3233. }
  3234. catch(IException* e)
  3235. {
  3236. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3237. }
  3238. return onGetNotFound(context, request, response, service);
  3239. }
  3240. bool isDeploymentTypeCompressed(const char *type)
  3241. {
  3242. if (type && *type)
  3243. return (0==strncmp(type, "compressed_", strlen("compressed_")));
  3244. return false;
  3245. }
  3246. const char *skipCompressedTypeQualifier(const char *type)
  3247. {
  3248. if (isDeploymentTypeCompressed(type))
  3249. type += strlen("compressed_");
  3250. return type;
  3251. }
  3252. void deployEclOrArchive(IEspContext &context, IEspWUDeployWorkunitRequest & req, IEspWUDeployWorkunitResponse & resp)
  3253. {
  3254. NewWsWorkunit wu(context);
  3255. StringAttr wuid(wu->queryWuid()); // NB queryWuid() not valid after workunit,clear()
  3256. wu->setAction(WUActionCompile);
  3257. StringBuffer name(req.getName());
  3258. if (!name.trim().length() && notEmpty(req.getFileName()))
  3259. splitFilename(req.getFileName(), NULL, NULL, &name, NULL);
  3260. if (name.length())
  3261. wu->setJobName(name.str());
  3262. if (req.getObject().length())
  3263. {
  3264. MemoryBuffer mb;
  3265. const MemoryBuffer *uncompressed = &req.getObject();
  3266. if (isDeploymentTypeCompressed(req.getObjType()))
  3267. {
  3268. fastLZDecompressToBuffer(mb, req.getObject().bufferBase());
  3269. uncompressed = &mb;
  3270. }
  3271. StringBuffer text(uncompressed->length(), uncompressed->toByteArray());
  3272. wu.setQueryText(text.str());
  3273. }
  3274. if (req.getQueryMainDefinition())
  3275. wu.setQueryMain(req.getQueryMainDefinition());
  3276. if (req.getSnapshot())
  3277. wu->setSnapshot(req.getSnapshot());
  3278. if (!req.getResultLimit_isNull())
  3279. wu->setResultLimit(req.getResultLimit());
  3280. wu->commit();
  3281. wu.clear();
  3282. WsWuHelpers::submitWsWorkunit(context, wuid.str(), req.getCluster(), NULL, 0, true, false, false, NULL, NULL, &req.getDebugValues());
  3283. waitForWorkUnitToCompile(wuid.str(), req.getWait());
  3284. WsWuInfo winfo(context, wuid.str());
  3285. winfo.getCommon(resp.updateWorkunit(), WUINFO_All);
  3286. winfo.getExceptions(resp.updateWorkunit(), WUINFO_All);
  3287. name.clear();
  3288. if (notEmpty(resp.updateWorkunit().getJobname()))
  3289. origValueChanged(req.getName(), resp.updateWorkunit().getJobname(), name, false);
  3290. if (name.length()) //non generated user specified name, so override #Workunit('name')
  3291. {
  3292. WorkunitUpdate wx(&winfo.cw->lock());
  3293. wx->setJobName(name.str());
  3294. resp.updateWorkunit().setJobname(name.str());
  3295. }
  3296. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wuid.str());
  3297. }
  3298. StringBuffer &sharedObjectFileName(StringBuffer &filename, const char *name, const char *ext, unsigned copy)
  3299. {
  3300. filename.append((name && *name) ? name : "workunit");
  3301. if (copy)
  3302. filename.append('-').append(copy);
  3303. if (notEmpty(ext))
  3304. filename.append(ext);
  3305. return filename;
  3306. }
  3307. inline StringBuffer &buildFullDllPath(StringBuffer &dllpath, StringBuffer &dllname, const char *dir, const char *name, const char *ext, unsigned copy)
  3308. {
  3309. return addPathSepChar(dllpath.set(dir)).append(sharedObjectFileName(dllname, name, ext, copy));
  3310. }
  3311. void writeSharedObject(const char *srcpath, const MemoryBuffer &obj, const char *dir, StringBuffer &dllpath, StringBuffer &dllname, unsigned crc)
  3312. {
  3313. StringBuffer name, ext;
  3314. if (srcpath && *srcpath)
  3315. splitFilename(srcpath, NULL, NULL, &name, &ext);
  3316. unsigned copy=0;
  3317. buildFullDllPath(dllpath.clear(), dllname.clear(), dir, name.str(), ext.str(), copy);
  3318. while (checkFileExists(dllpath.str()))
  3319. {
  3320. if (crc && crc == crc_file(dllpath.str()))
  3321. {
  3322. DBGLOG("Workunit dll already exists: %s", dllpath.str());
  3323. return;
  3324. }
  3325. buildFullDllPath(dllpath.clear(), dllname.clear(), dir, name.str(), ext.str(), ++copy);
  3326. }
  3327. DBGLOG("Writing workunit dll: %s", dllpath.str());
  3328. Owned<IFile> f = createIFile(dllpath.str());
  3329. Owned<IFileIO> io = f->open(IFOcreate);
  3330. io->write(0, obj.length(), obj.toByteArray());
  3331. }
  3332. void deploySharedObject(IEspContext &context, StringBuffer &wuid, const char *filename, const char *cluster, const char *name, const MemoryBuffer &obj, const char *dir, const char *xml)
  3333. {
  3334. StringBuffer dllpath, dllname;
  3335. StringBuffer srcname(filename);
  3336. unsigned crc = 0;
  3337. Owned<IPropertyTree> srcxml;
  3338. if (xml && *xml)
  3339. {
  3340. srcxml.setown(createPTreeFromXMLString(xml));
  3341. if (srcxml && wuid.length())
  3342. {
  3343. crc = srcxml->getPropInt("Query[1]/Associated[1]/File[@type='dll'][1]/@crc", 0);
  3344. if (crc)
  3345. {
  3346. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  3347. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid, false);
  3348. if (cw)
  3349. {
  3350. //is this a previous copy of same query, or a WUID collision?
  3351. if (cw->getHash() == (unsigned) srcxml->getPropInt64("@hash", 0))
  3352. {
  3353. Owned<IConstWUQuery> query = cw->getQuery();
  3354. if (query && crc == query->getQueryDllCrc())
  3355. return;
  3356. }
  3357. }
  3358. }
  3359. }
  3360. }
  3361. if (!srcname.length())
  3362. srcname.append(name).append(SharedObjectExtension);
  3363. writeSharedObject(srcname.str(), obj, dir, dllpath, dllname, crc);
  3364. NewWsWorkunit wu(context, wuid); //duplicate wuid made unique
  3365. wuid.set(wu->queryWuid());
  3366. wu->setClusterName(cluster);
  3367. wu->commit();
  3368. StringBuffer dllXML;
  3369. if (getWorkunitXMLFromFile(dllpath.str(), dllXML))
  3370. {
  3371. Owned<ILocalWorkUnit> embeddedWU = createLocalWorkUnit(dllXML.str());
  3372. queryExtendedWU(wu)->copyWorkUnit(embeddedWU, true);
  3373. }
  3374. wu.associateDll(dllpath.str(), dllname.str());
  3375. if (name && *name)
  3376. wu->setJobName(name);
  3377. //clean slate, copy only select items from processed workunit xml
  3378. if (srcxml)
  3379. {
  3380. if (srcxml->hasProp("@jobName"))
  3381. wu->setJobName(srcxml->queryProp("@jobName"));
  3382. if (srcxml->hasProp("@token"))
  3383. wu->setSecurityToken(srcxml->queryProp("@token"));
  3384. if (srcxml->hasProp("Query/Text"))
  3385. wu.setQueryText(srcxml->queryProp("Query/Text"));
  3386. }
  3387. wu->setState(WUStateCompiled);
  3388. wu->commit();
  3389. wu.clear();
  3390. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wuid.str());
  3391. }
  3392. void CWsWorkunitsEx::deploySharedObjectReq(IEspContext &context, IEspWUDeployWorkunitRequest & req, IEspWUDeployWorkunitResponse & resp, const char *dir, const char *xml)
  3393. {
  3394. if (isEmpty(req.getFileName()))
  3395. throw MakeStringException(ECLWATCH_INVALID_INPUT, "File name required when deploying a shared object.");
  3396. const char *cluster = req.getCluster();
  3397. if (isEmpty(cluster))
  3398. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Cluster name required when deploying a shared object.");
  3399. const MemoryBuffer *uncompressed = &req.getObject();
  3400. MemoryBuffer mb;
  3401. if (isDeploymentTypeCompressed(req.getObjType()))
  3402. {
  3403. fastLZDecompressToBuffer(mb, req.getObject().bufferBase());
  3404. uncompressed = &mb;
  3405. }
  3406. StringBuffer wuid;
  3407. deploySharedObject(context, wuid, req.getFileName(), cluster, req.getName(), *uncompressed, dir, xml);
  3408. WsWuInfo winfo(context, wuid.str());
  3409. winfo.getCommon(resp.updateWorkunit(), WUINFO_All);
  3410. AuditSystemAccess(context.queryUserId(), true, "Updated %s", wuid.str());
  3411. }
  3412. bool CWsWorkunitsEx::onWUDeployWorkunit(IEspContext &context, IEspWUDeployWorkunitRequest & req, IEspWUDeployWorkunitResponse & resp)
  3413. {
  3414. const char *type = skipCompressedTypeQualifier(req.getObjType());
  3415. try
  3416. {
  3417. if (!context.validateFeatureAccess(OWN_WU_ACCESS, SecAccess_Write, false))
  3418. throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "Failed to create workunit. Permission denied.");
  3419. if (notEmpty(req.getCluster()) && !isValidCluster(req.getCluster()))
  3420. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_NAME, "Invalid cluster name: %s", req.getCluster());
  3421. if (!type || !*type)
  3422. throw MakeStringExceptionDirect(ECLWATCH_INVALID_INPUT, "WUDeployWorkunit unspecified object type.");
  3423. if (strieq(type, "archive")|| strieq(type, "ecl_text"))
  3424. deployEclOrArchive(context, req, resp);
  3425. else if (strieq(type, "shared_object"))
  3426. deploySharedObjectReq(context, req, resp, queryDirectory.str());
  3427. else
  3428. throw MakeStringException(ECLWATCH_INVALID_INPUT, "WUDeployWorkunit '%s' unknown object type.", type);
  3429. }
  3430. catch(IException* e)
  3431. {
  3432. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3433. }
  3434. return true;
  3435. }
  3436. void CWsWorkunitsEx::createZAPFile(const char* fileName, size32_t len, const void* data)
  3437. {
  3438. if (!fileName || !*fileName)
  3439. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA,"File name not specified.");
  3440. Owned<IFile> wuInfoIFile = createIFile(fileName);
  3441. Owned<IFileIO> wuInfoIO = wuInfoIFile->open(IFOcreate);
  3442. if (wuInfoIO)
  3443. wuInfoIO->write(0, len, data);
  3444. }
  3445. void CWsWorkunitsEx::cleanZAPFolder(IFile* zipDir, bool removeFolder)
  3446. {
  3447. if (!zipDir)
  3448. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA,"Invalid file interface for the zip folder.");
  3449. Owned<IDirectoryIterator> iter = zipDir->directoryFiles(NULL,false,false);
  3450. ForEach(*iter)
  3451. {
  3452. OwnedIFile thisFile = createIFile(iter->query().queryFilename());
  3453. if (thisFile->isFile() == foundYes)
  3454. thisFile->remove();
  3455. }
  3456. if (removeFolder)
  3457. zipDir->remove();
  3458. }
  3459. void CWsWorkunitsEx::addProcessLogfile(Owned<IConstWorkUnit>& cwu, WsWuInfo& winfo, const char* process, const char* path)
  3460. {
  3461. Owned<IPropertyTreeIterator> procs = cwu->getProcesses(process, NULL);
  3462. ForEach (*procs)
  3463. {
  3464. StringBuffer logSpec;
  3465. IPropertyTree& proc = procs->query();
  3466. proc.getProp("@log",logSpec);
  3467. if (!logSpec.length())
  3468. continue;
  3469. StringBuffer pid;
  3470. pid.appendf("%d",proc.getPropInt("@pid"));
  3471. MemoryBuffer mb;
  3472. try
  3473. {
  3474. if (strieq(process, "EclAgent"))
  3475. winfo.getWorkunitEclAgentLog(logSpec.str(), pid.str(), mb);
  3476. else if (strieq(process, "Thor"))
  3477. winfo.getWorkunitThorLog(logSpec.str(), mb);
  3478. }
  3479. catch(IException *e)
  3480. {
  3481. StringBuffer s;
  3482. e->errorMessage(s);
  3483. DBGLOG("Error accessing Process Log file %s: %s", logSpec.str(), s.str());
  3484. mb.append(s.str());
  3485. e->Release();
  3486. }
  3487. if (!mb.length())
  3488. continue;
  3489. const char * logName = logSpec.str();
  3490. for (const char * p=logSpec; *p; p++)
  3491. {
  3492. if (*p == '\\' || *p == '/')
  3493. logName = p+1;
  3494. }
  3495. VStringBuffer fileName("%s%c%s", path, PATHSEPCHAR, logName);
  3496. createZAPFile(fileName.str(), mb.length(), mb.bufferBase());
  3497. }
  3498. }
  3499. void CWsWorkunitsEx::createZAPWUInfoFile(IEspWUCreateZAPInfoRequest &req, Owned<IConstWorkUnit>& cwu, const char* pathNameStr)
  3500. {
  3501. StringBuffer sb;
  3502. sb.append("Workunit: ").append(cwu->queryWuid()).append("\r\n");
  3503. sb.append("User: ").append(cwu->queryUser()).append("\r\n");
  3504. sb.append("Build Version:").append(req.getBuildVersion()).append("\r\n");
  3505. sb.append("Cluster: ").append(cwu->queryClusterName()).append("\r\n");
  3506. if (req.getESPIPAddress())
  3507. sb.append("ESP: ").append(req.getESPIPAddress()).append("\r\n");
  3508. if (req.getThorIPAddress())
  3509. sb.append("Thor: ").append(req.getThorIPAddress()).append("\r\n");
  3510. //Exceptions/Warnings/Info
  3511. Owned<IConstWUExceptionIterator> exceptions = &cwu->getExceptions();
  3512. StringBuffer info, warn, err, alert;
  3513. ForEach(*exceptions)
  3514. {
  3515. SCMStringBuffer temp;
  3516. switch (exceptions->query().getSeverity())
  3517. {
  3518. case SeverityInformation:
  3519. info.append("\t").append(exceptions->query().getExceptionMessage(temp)).append("\r\n\r\n");
  3520. break;
  3521. case SeverityWarning:
  3522. warn.append("\t").append(exceptions->query().getExceptionMessage(temp)).append("\r\n\r\n");
  3523. break;
  3524. case SeverityError:
  3525. err.append("\t").append(exceptions->query().getExceptionMessage(temp)).append("\r\n\r\n");
  3526. break;
  3527. case SeverityAlert:
  3528. alert.append("\t").append(exceptions->query().getExceptionMessage(temp)).append("\r\n\r\n");
  3529. break;
  3530. }
  3531. }
  3532. if (err.length())
  3533. sb.append("Exceptions: ").append("\r\n").append(err);
  3534. if (warn.length())
  3535. sb.append("Warnings: ").append("\r\n").append(warn);
  3536. if (info.length())
  3537. sb.append("Information: ").append("\r\n").append(info);
  3538. if (alert.length())
  3539. sb.append("Alert: ").append("\r\n").append(alert);
  3540. //User provided Information
  3541. sb.append("Problem: ").append(req.getProblemDescription()).append("\r\n\r\n");
  3542. sb.append("What Changed: ").append(req.getWhatChanged()).append("\r\n\r\n");
  3543. sb.append("Timing: ").append(req.getWhereSlow()).append("\r\n\r\n");
  3544. VStringBuffer fileName("%s.txt", pathNameStr);
  3545. createZAPFile(fileName.str(), sb.length(), sb.str());
  3546. }
  3547. void CWsWorkunitsEx::createZAPWUXMLFile(WsWuInfo &winfo, const char* pathNameStr)
  3548. {
  3549. MemoryBuffer mb;
  3550. winfo.getWorkunitXml(NULL, mb);
  3551. VStringBuffer fileName("%s.xml", pathNameStr);
  3552. createZAPFile(fileName.str(), mb.length(), mb.bufferBase());
  3553. }
  3554. void CWsWorkunitsEx::createZAPECLQueryArchiveFiles(Owned<IConstWorkUnit>& cwu, const char* pathNameStr)
  3555. {
  3556. Owned<IConstWUQuery> query = cwu->getQuery();
  3557. if(!query)
  3558. return;
  3559. //Add archive if present
  3560. Owned<IConstWUAssociatedFileIterator> iter = &query->getAssociatedFiles();
  3561. ForEach(*iter)
  3562. {
  3563. IConstWUAssociatedFile & cur = iter->query();
  3564. SCMStringBuffer ssb;
  3565. cur.getDescription(ssb);
  3566. if (!strieq(ssb.str(), "archive"))
  3567. continue;
  3568. cur.getName(ssb);
  3569. if (!ssb.length())
  3570. continue;
  3571. StringBuffer fileName, archiveContents;
  3572. try
  3573. {
  3574. archiveContents.loadFile(ssb.str());
  3575. }
  3576. catch (IException *e)
  3577. {
  3578. StringBuffer s;
  3579. e->errorMessage(s);
  3580. DBGLOG("Error accessing archive file %s: %s", ssb.str(), s.str());
  3581. archiveContents.insert(0, "Error accessing archive file ").appendf("%s: %s\r\n\r\n", ssb.str(), s.str());
  3582. e->Release();
  3583. }
  3584. fileName.setf("%s.archive", pathNameStr);
  3585. createZAPFile(fileName.str(), archiveContents.length(), archiveContents.str());
  3586. break;
  3587. }
  3588. //Add Query
  3589. SCMStringBuffer temp;
  3590. query->getQueryText(temp);
  3591. if (temp.length())
  3592. {
  3593. VStringBuffer fileName("%s.ecl", pathNameStr);
  3594. createZAPFile(fileName.str(), temp.length(), temp.str());
  3595. }
  3596. }
  3597. bool CWsWorkunitsEx::onWUCreateZAPInfo(IEspContext &context, IEspWUCreateZAPInfoRequest &req, IEspWUCreateZAPInfoResponse &resp)
  3598. {
  3599. try
  3600. {
  3601. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  3602. Owned<IConstWorkUnit> cwu = factory->openWorkUnit(req.getWuid(), false);
  3603. if(!cwu.get())
  3604. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT, "Cannot open workunit %s.", req.getWuid());
  3605. StringBuffer userName, nameStr, fileName;
  3606. StringBuffer zipFileName, zipFileNameWithPath, zipCommand, folderToZIP;
  3607. if (context.queryUser())
  3608. userName.append(context.queryUser()->getName());
  3609. nameStr.append("ZAPReport_").append(req.getWuid()).append('_').append(userName.str());
  3610. //create a folder for WU ZAP files
  3611. const char* zipFolder = "tempzipfiles"PATHSEPSTR;
  3612. folderToZIP.append(zipFolder).append(nameStr.str());
  3613. Owned<IFile> zipDir = createIFile(folderToZIP.str());
  3614. if (!zipDir->exists())
  3615. zipDir->createDirectory();
  3616. else
  3617. cleanZAPFolder(zipDir, false);
  3618. //create WU ZAP files
  3619. VStringBuffer pathNameStr("%s/%s", folderToZIP.str(), nameStr.str());
  3620. createZAPWUInfoFile(req, cwu, pathNameStr.str());
  3621. createZAPECLQueryArchiveFiles(cwu, pathNameStr.str());
  3622. WsWuInfo winfo(context, cwu);
  3623. createZAPWUXMLFile(winfo, pathNameStr.str());
  3624. addProcessLogfile(cwu, winfo, "EclAgent", folderToZIP.str());
  3625. addProcessLogfile(cwu, winfo, "Thor", folderToZIP.str());
  3626. //Write out to ZIP file
  3627. zipFileName.append(nameStr.str()).append(".zip");
  3628. zipFileNameWithPath.append(zipFolder).append(zipFileName.str());
  3629. pathNameStr.set(folderToZIP.str()).append("/*");
  3630. const char* password = req.getPassword();
  3631. if (password && *password)
  3632. zipCommand.appendf("zip -j --password %s %s %s", password, zipFileNameWithPath.str(), pathNameStr.str());
  3633. else
  3634. zipCommand.appendf("zip -j %s %s", zipFileNameWithPath.str(), pathNameStr.str());
  3635. int zipRet = system(zipCommand.str());
  3636. //Remove the temporary files and the folder
  3637. cleanZAPFolder(zipDir, true);
  3638. if (zipRet != 0)
  3639. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA,"Failed to execute system command 'zip'. Please make sure that zip utility is installed.");
  3640. //Download ZIP file to user
  3641. Owned<IFile> f = createIFile(zipFileNameWithPath.str());
  3642. Owned<IFileIO> io = f->open(IFOread);
  3643. MemoryBuffer mb;
  3644. void * data = mb.reserve((unsigned)io->size());
  3645. size32_t read = io->read(0, (unsigned)io->size(), data);
  3646. mb.setLength(read);
  3647. resp.setThefile(mb);
  3648. resp.setThefile_mimetype(HTTP_TYPE_OCTET_STREAM);
  3649. StringBuffer headerStr("attachment;filename=");
  3650. headerStr.append(zipFileName.str());
  3651. context.addCustomerHeader("Content-disposition", headerStr.str());
  3652. io->close();
  3653. f->remove();
  3654. }
  3655. catch(IException* e)
  3656. {
  3657. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3658. }
  3659. return true;
  3660. }
  3661. bool CWsWorkunitsEx::onWUGetZAPInfo(IEspContext &context, IEspWUGetZAPInfoRequest &req, IEspWUGetZAPInfoResponse &resp)
  3662. {
  3663. try
  3664. {
  3665. StringBuffer wuid = req.getWUID();
  3666. WsWuHelpers::checkAndTrimWorkunit("WUGetZAPInfo", wuid);
  3667. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  3668. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid, false);
  3669. if(!cw)
  3670. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.",wuid.str());
  3671. StringBuffer EspIP, ThorIP;
  3672. resp.setWUID(wuid.str());
  3673. resp.setBuildVersion(getBuildVersion());
  3674. IpAddress ipaddr = queryHostIP();
  3675. ipaddr.getIpText(EspIP);
  3676. resp.setESPIPAddress(EspIP.str());
  3677. //Get Archive
  3678. Owned<IConstWUQuery> query = cw->getQuery();
  3679. if(query)
  3680. {
  3681. SCMStringBuffer queryText;
  3682. query->getQueryText(queryText);
  3683. if (queryText.length() && isArchiveQuery(queryText.str()))
  3684. resp.setArchive(queryText.str());
  3685. }
  3686. //Get Thor IP
  3687. BoolHash uniqueProcesses;
  3688. Owned<IStringIterator> thorInstances = cw->getProcesses("Thor");
  3689. ForEach (*thorInstances)
  3690. {
  3691. SCMStringBuffer processName;
  3692. thorInstances->str(processName);
  3693. if (processName.length() < 1)
  3694. continue;
  3695. bool* found = uniqueProcesses.getValue(processName.str());
  3696. if (found && *found)
  3697. continue;
  3698. uniqueProcesses.setValue(processName.str(), true);
  3699. Owned<IStringIterator> thorLogs = cw->getLogs("Thor", processName.str());
  3700. ForEach (*thorLogs)
  3701. {
  3702. SCMStringBuffer logName;
  3703. thorLogs->str(logName);
  3704. if (!logName.length())
  3705. continue;
  3706. const char* thorIPPtr = NULL;
  3707. const char* ptr = logName.str();
  3708. while (ptr)
  3709. {
  3710. if (!thorIPPtr && (*ptr != '/'))
  3711. thorIPPtr = ptr;
  3712. else if (thorIPPtr && (*ptr == '/'))
  3713. break;
  3714. ptr++;
  3715. }
  3716. if (!thorIPPtr)
  3717. continue;
  3718. //Found a thor IP
  3719. if (ThorIP.length())
  3720. ThorIP.append(",");
  3721. if (!*ptr)
  3722. ThorIP.append(thorIPPtr);
  3723. else
  3724. ThorIP.append(ptr-thorIPPtr, thorIPPtr);
  3725. }
  3726. }
  3727. if (ThorIP.length())
  3728. resp.setThorIPAddress(ThorIP.str());
  3729. }
  3730. catch(IException* e)
  3731. {
  3732. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3733. }
  3734. return true;
  3735. }
  3736. bool CWsWorkunitsEx::onWUCheckFeatures(IEspContext &context, IEspWUCheckFeaturesRequest &req, IEspWUCheckFeaturesResponse &resp)
  3737. {
  3738. resp.setBuildVersionMajor(BUILD_VERSION_MAJOR);
  3739. resp.setBuildVersionMinor(BUILD_VERSION_MINOR);
  3740. resp.setBuildVersionPoint(BUILD_VERSION_POINT);
  3741. resp.setMaxRequestEntityLength(maxRequestEntityLength);
  3742. resp.updateDeployment().setUseCompression(true);
  3743. return true;
  3744. }
  3745. static const char * checkGetStatsInput(const char * s)
  3746. {
  3747. if (!s || !*s)
  3748. return "*";
  3749. return s;
  3750. }
  3751. bool CWsWorkunitsEx::onWUGetStats(IEspContext &context, IEspWUGetStatsRequest &req, IEspWUGetStatsResponse &resp)
  3752. {
  3753. try
  3754. {
  3755. StringBuffer wuid = req.getWUID();
  3756. WsWuHelpers::checkAndTrimWorkunit("WUInfo", wuid);
  3757. ensureWsWorkunitAccess(context, wuid.str(), SecAccess_Read);
  3758. const char* creatorType = checkGetStatsInput(req.getCreatorType());
  3759. const char* creator = checkGetStatsInput(req.getCreator());
  3760. const char* scopeType = checkGetStatsInput(req.getScopeType());
  3761. const char* scope = checkGetStatsInput(req.getScope());
  3762. const char* kind = checkGetStatsInput(req.getKind());
  3763. const char* measure = req.getMeasure();
  3764. StatisticsFilter filter(creatorType, creator, scopeType, scope, measure, kind);
  3765. if (!req.getMinScopeDepth_isNull() && !req.getMaxScopeDepth_isNull())
  3766. filter.setScopeDepth(req.getMinScopeDepth(), req.getMaxScopeDepth());
  3767. else if (!req.getMinScopeDepth_isNull())
  3768. filter.setScopeDepth(req.getMinScopeDepth());
  3769. if (!req.getIncludeGraphs_isNull())
  3770. filter.setMergeSources(req.getIncludeGraphs());
  3771. bool createDescriptions = false;
  3772. if (!req.getCreateDescriptions_isNull())
  3773. createDescriptions = req.getCreateDescriptions();
  3774. WsWuInfo winfo(context, wuid.str());
  3775. IArrayOf<IEspWUStatisticItem> statistics;
  3776. winfo.getStats(filter, createDescriptions, statistics);
  3777. resp.setStatistics(statistics);
  3778. resp.setWUID(wuid.str());
  3779. }
  3780. catch(IException* e)
  3781. {
  3782. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3783. }
  3784. return true;
  3785. }