1 /*  $Id: wn_cmds.cpp 574069 2018-11-05 20:24:42Z sadyrovr $
2  * ===========================================================================
3  *
4  *                            PUBLIC DOMAIN NOTICE
5  *               National Center for Biotechnology Information
6  *
7  *  This software/database is a "United States Government Work" under the
8  *  terms of the United States Copyright Act.  It was written as part of
9  *  the author's official duties as a United States Government employee and
10  *  thus cannot be copyrighted.  This software/database is freely available
11  *  to the public for use. The National Library of Medicine and the U.S.
12  *   Government have not placed any restriction on its use or reproduction.
13  *
14  *  Although all reasonable efforts have been taken to ensure the accuracy
15  *  and reliability of the software and data, the NLM and the U.S.
16  *  Government do not and cannot warrant the performance or results that
17  *  may be obtained by using this software or data. The NLM and the U.S.
18  *  Government disclaim all warranties, express or implied, including
19  *  warranties of performance, merchantability or fitness for any particular
20  *  purpose.
21  *
22  *  Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors:  Dmitry Kazimirov
27  *
28  * File Description: Worker node-specific commands of grid_cli.
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
34 #include "grid_cli.hpp"
35 
36 #include <connect/services/grid_rw_impl.hpp>
37 #include <connect/services/ns_job_serializer.hpp>
38 
39 #include <cgi/ncbicgi.hpp>
40 
41 USING_NCBI_SCOPE;
42 
Cmd_Replay()43 int CGridCommandLineInterfaceApp::Cmd_Replay()
44 {
45     SetUp_NetScheduleCmd(eNetScheduleSubmitter);
46 
47     CNetScheduleJob job;
48     job.job_id = m_Opts.id;
49 
50     CNetScheduleAPI::EJobStatus job_status =
51             m_NetScheduleAPI.GetJobDetails(job);
52 
53     if (job_status == CNetScheduleAPI::eJobNotFound) {
54         fprintf(stderr, GRID_APP_NAME ": job %s has expired.\n",
55             job.job_id.c_str());
56         return 3;
57     }
58 
59     if (IsOptionSet(eDumpCGIEnv) || IsOptionSet(eDumpCGIStdIn)) {
60         CStringOrBlobStorageReader reader(job.input, m_NetCacheAPI);
61         CRStream input_stream(&reader, 0, 0, CRWStreambuf::fLeakExceptions);
62 
63         input_stream.exceptions(IOS_BASE::badbit | IOS_BASE::failbit);
64 
65         unique_ptr<CCgiRequest> request;
66 
67         try {
68             request.reset(new CCgiRequest(input_stream,
69                 CCgiRequest::fIgnoreQueryString |
70                 CCgiRequest::fDoNotParseContent |
71                 CCgiRequest::fSaveRequestContent));
72         }
73         catch (CException& e) {
74             fprintf(stderr, GRID_APP_NAME
75                     ": Error while parsing CGI request stream: %s.\n",
76                     e.what());
77             return 3;
78         }
79 
80         if (IsOptionSet(eDumpCGIEnv)) {
81             const CNcbiEnvironment& env(request->GetEnvironment());
82 
83             list<string> var_names;
84 
85             env.Enumerate(var_names);
86 
87             ITERATE(list<string>, var, var_names) {
88                 fprintf(m_Opts.output_stream, "%s=\"%s\"\n", var->c_str(),
89                         NStr::PrintableString(env.Get(*var)).c_str());
90             }
91         }
92 
93         if (IsOptionSet(eDumpCGIStdIn))
94             fwrite(request->GetContent().c_str(), 1,
95                     request->GetContent().length(), m_Opts.output_stream);
96     }
97 
98     CNetScheduleJobSerializer job_serializer(job, m_CompoundIDPool);
99 
100     if (IsOptionSet(eJobInputDir))
101         job_serializer.SaveJobInput(m_Opts.job_input_dir, m_NetCacheAPI);
102 
103     if (IsOptionSet(eJobOutputDir))
104         switch (job_status) {
105         case CNetScheduleAPI::eCanceled:
106         case CNetScheduleAPI::eFailed:
107         case CNetScheduleAPI::eDone:
108         case CNetScheduleAPI::eReading:
109         case CNetScheduleAPI::eConfirmed:
110         case CNetScheduleAPI::eReadFailed:
111             job_serializer.SaveJobOutput(job_status,
112                     m_Opts.job_output_dir, m_NetCacheAPI);
113             break;
114 
115         default:
116             fprintf(stderr, GRID_APP_NAME
117                 ": cannot retrieve job output while the job is %s.\n",
118                 CNetScheduleAPI::StatusToString(job_status).c_str());
119             return 4;
120         }
121 
122     return 0;
123 }
124