-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJobTicket.cpp
166 lines (140 loc) · 4.43 KB
/
JobTicket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "JobTicket.h"
#include "DefaultValues.h"
#include "Log.h"
#include <boost/lexical_cast.hpp>
#include "Utils.h"
#include "Node.h"
using namespace FCPLib;
JobTicket::Ptr
JobTicket::factory(Node* n, std::string id, Message::Ptr cmd)
{
log().log(NOISY, "Creating " + cmd->getHeader());
Ptr ret( new JobTicket() );
ret->init(n, id, cmd);
return ret;
}
void
JobTicket::init(Node* n, std::string &id, Message::Ptr cmd)
{
this->node = n;
this->id = id;
this->cmd = cmd;
this->lock.acquire();
this->reqSentLock.acquire();
log().log(DEBUG, this->toString());
}
void
JobTicket::wait(unsigned int timeout)
{
if (!timeout){
while (!lock.tryAcquire(100)) {
if (! node->isAlive() ) {
if ( node->hasFailure() ) throw node->getFailure();
else throw FCPNodeFailure( "node thread is not alive" );
}
ZThread::Thread::sleep(100);
}
lock.release();
return;
}
unsigned int then = (unsigned int) time(0);
unsigned int elapsed;
while (!reqSentLock.tryAcquire(100)){
if (! node->isAlive() ) {
if ( node->hasFailure() ) throw node->getFailure();
else throw FCPNodeFailure( "node thread is not alive" );
}
elapsed = (unsigned int) time(0) - then;
if (elapsed < timeout){
ZThread::Thread::sleep(1000);
log().log(DEBUG, "wait:"+this->getCommandName()+":"+this->getId()+": job not dispatched, timeout in " +
boost::lexical_cast<std::string>(timeout-elapsed));
continue;
}
log().log(DEBUG, "wait:"+this->getCommandName()+":"+this->getId()+": timeout on send command");
throw CommandTimeout("Timeout sending " + this->getCommandName());
}
log().log(DEBUG, "wait:"+this->getCommandName()+":"+this->getId()+": job now dispatched");
while (!lock.tryAcquire(100)){
if (! node->isAlive() ) {
if ( node->hasFailure() ) throw node->getFailure();
else throw FCPNodeFailure( "node thread is not alive" );
}
elapsed = (unsigned int) time(0) - then;
if (elapsed < timeout) {
ZThread::Thread::sleep(2000);
log().log(DEBUG, "wait:"+this->getCommandName()+":"+this->getId()+": waiting for node response, timeout in " +
boost::lexical_cast<std::string>(timeout-elapsed));
continue;
}
log().log(DEBUG, "wait:"+this->getCommandName()+":"+this->getId()+": timeout on node response");
throw CommandTimeout("Timeout sending " + this->getCommandName());
}
log().log(DEBUG, "wait:"+this->getCommandName()+":"+this->getId()+": job complete");
lock.release();
}
void
JobTicket::waitTillReqSent(unsigned int timeout)
{
unsigned int then = (unsigned int) time(0);
unsigned int elapsed;
while (!reqSentLock.tryAcquire(100)){
if (! node->isAlive() ) {
if ( node->hasFailure() ) throw node->getFailure();
else throw FCPNodeFailure( "node thread is not alive" );
}
elapsed = (unsigned int) time(0) - then;
if (elapsed < timeout){
ZThread::Thread::sleep(1000);
log().log(DEBUG, "wait:"+this->getCommandName()+":"+this->getId()+": job not dispatched, timeout in " +
boost::lexical_cast<std::string>(timeout-elapsed));
continue;
}
log().log(DEBUG, "wait:"+this->getCommandName()+":"+this->getId()+": timeout on send command");
throw CommandTimeout("Timeout sending " + this->getCommandName());
}
}
const std::string&
JobTicket::toString()
{
if (isReprValid)
return repr;
repr = "";
isReprValid = true;
repr += "Job id=" + id +
" keepJob=" + Converter::toString( keep ) +
" global=" + Converter::toString( global ) +
" persistent=" + Converter::toString( persistent ) + "\n";
repr += "Message=" + cmd->getHeader();
return repr;
}
void
JobTicket::putResult()
{
ZThread::Guard<ZThread::Mutex> g(access);
_isFinished = true;
lock.release();
}
GetJob::Ptr
GetJob::factory(Node *n, std::string id, Message::Ptr cmd)
{
log().log(NOISY, "Creating " + cmd->getHeader());
Ptr ret( new GetJob() );
ret->init(n, id, cmd);
return ret;
}
const std::string&
GetJob::toString()
{
if (isReprValid)
return repr;
repr = "";
isReprValid = true;
repr += "Job id=" + id + " " +
" keepJob=" + Converter::toString( keep ) +
" global=" + Converter::toString( global ) +
" persistent=" + Converter::toString( persistent ) +
" returnType=" + boost::lexical_cast<std::string>( retType )+ "\n";
repr += "Message=" + cmd->getHeader();
return repr;
}