~vbursian/research-assistant/trunk

« back to all changes in this revision

Viewing changes to RAdmin/ScriptJob.cpp

  • Committer: Viktor Bursian
  • Date: 2020-07-16 22:38:41 UTC
  • mfrom: (9.25.2 DIST) (9.16.21 VB)
  • Revision ID: vik@pryanik-20200716223841-0gp0sf6m5imdjtb3
Tags: version_1.5.0
releasing version 1.5.0, closing v.1.4 development

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////////
 
2
/*! @file ScriptJob.cpp Background script execution.
 
3
- Part of RAdmin project.
 
4
- Uses  RANet - Research Assistant Net Library.
 
5
- Uses  Qt v.5  - http://qt.io/
 
6
- Copyright(C) 2020, Viktor E. Bursian, St.Petersburg, Russia.
 
7
                     VBursian AT gmail DOT com
 
8
*///////////////////////////////////////////////////////////////////////////////
 
9
#include "ScriptJob.h"
 
10
#include <QRegExp>
 
11
#include "Log.h"
 
12
namespace RA {
 
13
//------------------------------------------------------------------------------
 
14
//--------------------------------------------------------------- sScriptJob ---
 
15
 
 
16
sScriptJob::~sScriptJob ()
 
17
{
 
18
  RANet::Log.Put(sLog::Debug,"NetTools","sScriptJob destructor");
 
19
  Mutex.lock();
 
20
  if( isRunning() ){
 
21
    terminate();
 
22
    wait();
 
23
  }
 
24
  Mutex.unlock();
 
25
}
 
26
 
 
27
 
 
28
sScriptJob::sScriptJob (const QString &  script
 
29
                       ,QObject *        parent)
 
30
    :QThread(parent)
 
31
    ,AbortRequestedFlag(false)
 
32
    ,FailureHappendFlag(false)
 
33
    ,TheProgress(-1)
 
34
    ,TheProgressTarget(100)
 
35
{
 
36
  Jobs = QString(script)
 
37
      .replace(QRegExp("\\n"),"||")
 
38
      .replace(QRegExp("\\r"),"||")
 
39
      .replace(QRegExp("\\s+\\|\\|"),"||")
 
40
      .replace(QRegExp("\\|\\|\\s+"),"||")
 
41
      .split("||",QString::SkipEmptyParts)
 
42
      .replaceInStrings(QRegExp("^\\s+"),"")
 
43
      .replaceInStrings(QRegExp("\\s+$"),"")
 
44
      .replaceInStrings(QRegExp("\\s+\\|"),"|")
 
45
      .replaceInStrings(QRegExp("\\|\\s+"),"|")
 
46
      ;
 
47
}
 
48
 
 
49
 
 
50
int  sScriptJob::Progress ()
 
51
{
 
52
  QMutexLocker locker(&Mutex);
 
53
  return (100 * TheProgress) / TheProgressTarget;
 
54
}
 
55
 
 
56
 
 
57
void  sScriptJob::Abort ()
 
58
{
 
59
  QMutexLocker locker(&Mutex);
 
60
  AbortRequestedFlag = true;
 
61
}
 
62
 
 
63
 
 
64
void  sScriptJob::Failure (const QString &  message)
 
65
{
 
66
  emit OutPar(QString("<font color=red><b>%1</b></font>")
 
67
              .arg(message));
 
68
  {
 
69
    QMutexLocker locker(&Mutex);
 
70
    FailureHappendFlag = true;
 
71
  }
 
72
}
 
73
 
 
74
 
 
75
void  sScriptJob::ReportNewJobStart (QString  job_title)
 
76
{
 
77
  emit OutPar(QString("<font color=navy><h1>%1. %2</h1></font>")
 
78
              .arg(TheCurrentJobNum)
 
79
              .arg(job_title)
 
80
              );
 
81
}
 
82
 
 
83
 
 
84
void  sScriptJob::ReportNewStageStart (QString  stage_title)
 
85
{
 
86
  emit OutPar(QString("<h3>______ %1 ______</h3>")
 
87
              .arg(stage_title));
 
88
  emit NewStage();
 
89
}
 
90
 
 
91
 
 
92
void  sScriptJob::ReportJobConclusion (QString  conclusion)
 
93
{
 
94
  emit OutPar(QString("<font color=navy><b>____________ ")
 
95
                 +conclusion+"</b></font>");
 
96
}
 
97
 
 
98
 
 
99
bool  sScriptJob::AbortRequested ()
 
100
{
 
101
  QMutexLocker locker(&Mutex);
 
102
  return AbortRequestedFlag;
 
103
}
 
104
 
 
105
 
 
106
bool  sScriptJob::FailureHappend()
 
107
{
 
108
  QMutexLocker locker(&Mutex);
 
109
  return FailureHappendFlag;
 
110
}
 
111
 
 
112
 
 
113
void  sScriptJob::SetProgress (int  progress)
 
114
{
 
115
  QMutexLocker locker(&Mutex);
 
116
  TheProgress = progress;
 
117
}
 
118
 
 
119
 
 
120
void  sScriptJob::SetProgressTarget (int  progress_target)
 
121
{
 
122
  QMutexLocker locker(&Mutex);
 
123
  TheProgressTarget = progress_target;
 
124
}
 
125
 
 
126
 
 
127
void sScriptJob::FindAndExecute (const QString & job_name
 
128
                                ,QStringList &   params)
 
129
{
 
130
  Failure( QString("Unknown command in the script: '%1|%2'%3.")
 
131
           .arg(job_name)
 
132
           .arg(params.join("|"))
 
133
           .arg( job_name.isEmpty() ? " (empty command name)" : "" )
 
134
         );
 
135
}
 
136
 
 
137
 
 
138
void  sScriptJob::run ()
 
139
{
 
140
  emit OutPar("<font color=brown><h1>Script</h1></font>");
 
141
  for( int i = 0 ; i < Jobs.size() ; ++i ){
 
142
    emit OutPar(QString("<font color=brown><code>%1</code></font>")
 
143
                .arg(Jobs.at(i)));
 
144
  }
 
145
  emit TotalJobs(Jobs.size());
 
146
  for( int i = 0 ; i < Jobs.size() ; ++i ){
 
147
    TheCurrentJobNum = i+1;
 
148
    //emit OutPar("<hr>"); -- потом вставляет черту после каждого абзаца
 
149
    //emit OutPar("<br/>"); //it works
 
150
    emit NewJob();
 
151
    QStringList  Pars( Jobs.at(i).split("|") );
 
152
    if( Pars.size() > 0 ){
 
153
      QString      Job( Pars.front() );
 
154
      Pars.pop_front();
 
155
      FindAndExecute(Job,Pars);
 
156
    }
 
157
    if( FailureHappend() || AbortRequested() ){
 
158
      break;
 
159
    }
 
160
  }
 
161
  if( AbortRequested() ){
 
162
    emit OutPar(QString("<font color=red><big>"
 
163
                            "______________________ Execution ABORTED"
 
164
                            "</big></font>"));
 
165
    emit Aborted();
 
166
  }else if( FailureHappend() ){
 
167
    emit OutPar(QString("<font color=red><big>"
 
168
                            "______________________ Execution FAILED"
 
169
                            "</big></font>"));
 
170
 
 
171
    emit Failed();
 
172
  }else{
 
173
    emit OutPar(QString("<font color=green><big>"
 
174
                            "______________________ ALL DONE"
 
175
                            "</big></font>"));
 
176
    emit AllDone();
 
177
  }
 
178
}
 
179
 
 
180
//------------------------------------------------------------------------------
 
181
} //namespace RA