/
jobaf.cpp
1672 lines (1404 loc) · 43.3 KB
/
jobaf.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' *\
* .NN. _____ _____ _____ _ _ This file is part of CGRU
* hMMh / ____/ ____| __ \| | | | - The Free And Open Source CG Tools Pack.
* sMMMMs | | | | __| |__) | | | | CGRU is licensed under the terms of LGPLv3, see files
* <yMMMMMMMMMMMMMMy> | | | |_ | _ /| | | | COPYING and COPYING.lesser inside of this folder.
* `+mMMMMMMMMNo` | |___| |__| | | \ \| |__| | Project-Homepage: http://cgru.info
* :MMMMMMMM: \_____\_____|_| \_\\____/ Sourcecode: https://github.com/CGRU/cgru
* dMMMdmMMMd A F A N A S Y
* -Mmo. -omM: Copyright © by The CGRU team
* ' '
\* ....................................................................................................... */
/*
This is a server side of an Afanasy job.
*/
#include "jobaf.h"
#include "../include/afanasy.h"
#include "../libafanasy/blockdata.h"
#include "../libafanasy/environment.h"
#include "../libafanasy/jobprogress.h"
#include "../libafanasy/msgqueue.h"
#include "action.h"
#include "afcommon.h"
#include "block.h"
#include "branchescontainer.h"
#include "branchsrv.h"
#include "jobcontainer.h"
#include "monitorcontainer.h"
#include "renderaf.h"
#include "rendercontainer.h"
#include "sysjob.h"
#include "task.h"
#include "useraf.h"
#include "usercontainer.h"
#define AFOUTPUT
#undef AFOUTPUT
#include "../include/macrooutput.h"
#include "../libafanasy/logger.h"
JobContainer *JobAf::ms_jobs = NULL;
JobAf::JobAf( JSON & i_object):
af::Job(),
AfNodeSolve( this)
{
initializeValues();
if( false == jsonRead( i_object))
return;
if (false == isValid())
return;
m_progress = new af::JobProgress( this);
construct();
m_serial = AFCommon::getJobSerial();
// System job never constructed from incoming JSON object and always has zero serial
for (int b = 0; b < m_blocks_num; b++)
m_blocks[b]->constructDependTasks();
}
JobAf::JobAf( const std::string & i_store_dir, bool i_system):
af::Job(),
AfNodeSolve( this, i_store_dir)
{
AF_DEBUG << "store dir = " << i_store_dir;
initializeValues();
// We do not read store for system job
// as some virtual functions from system job
// will not work in a base class constructor
if( i_system ) return;
readStore();
// Zero serial means that the job was created serials appeared in the project:
// ( system job has zero serial )
if( m_serial == 0 && ( false == i_system ))
{
m_serial = AFCommon::getJobSerial();
store();
}
for (int b = 0; b < m_blocks_num; b++)
m_blocks[b]->constructDependTasks();
}
void JobAf::readStore()
{
initStoreDirs();
int size;
char * data = af::fileRead( getStoreFile(), &size);
if( NULL == data ) return;
rapidjson::Document document;
char * res = af::jsonParseData( document, data, size);
if( NULL == res )
{
delete [] data;
return;
}
jsonRead( document);
delete [] res;
delete [] data;
m_progress = new af::JobProgress( this);
construct();
for( int b = 0; b < m_blocks_num; b++)
if( false == m_blocks[b]->readStoredTasks())
return;
}
void JobAf::initializeValues()
{
m_force_refresh = true;
m_branch_srv = NULL;
m_user = NULL;
m_blocks = NULL;
m_progress = NULL;
m_deletion = false;
m_thumb_changed = false;
m_report_changed = false;
m_logsWeight = 0;
m_blackListsWeight = 0;
}
void JobAf::initStoreDirs()
{
m_store_dir_tasks = getStoreDir() + AFGENERAL::PATH_SEPARATOR + "tasks";
}
void JobAf::construct(int alreadyConstructed)
{
AF_DEBUG << '"' << m_name << "\": from store: " << isFromStore();
if( NULL != m_blocks && alreadyConstructed == 0 )
{
AF_ERR << "Already constructed.";
return;
}
if( m_blocks_num < 1 )
{
AF_ERR << "invalid number of blocks = " << m_blocks_num;
return;
}
Block ** old_blocks = m_blocks;
m_blocks = new Block*[m_blocks_num];
if( NULL == m_blocks )
{
AF_ERR << "Can't allocate memory for blocks.";
return;
}
for( int b = 0; b < m_blocks_num; b++)
m_blocks[b] = b < alreadyConstructed ? old_blocks[b] : NULL;
if( NULL != old_blocks)
delete [] old_blocks;
for( int b = alreadyConstructed; b < m_blocks_num; b++)
{
m_blocks[b] = v_newBlock(b);
if( m_blocks[b] == NULL )
{
AF_ERR << "Can't create block " << b << " of " << m_blocks_num;
return;
}
}
}
bool JobAf::isValidConstructed() const
{
bool valid = true;
std::string err;
if ((NULL == m_blocks) || (NULL == m_progress))
{
err += "Is not constructed.";
valid = false;
}
if (valid)
{
valid = isValid(&err);
}
if (false == valid)
{
err = std::string("Invalid job '") + m_name + "': " + err;
if( isFromStore())
AF_ERR << err;
else
AFCommon::QueueLogError( err);
return false;
}
AF_DEBUG << '"' << m_name << "\": TRUE; from store: " << isFromStore();
return true;
}
JobAf::~JobAf()
{
if( m_blocks )
{
for( int b = 0; b < m_blocks_num; b++) if( m_blocks[b]) delete m_blocks[b];
delete [] m_blocks;
}
if( m_progress ) delete m_progress;
}
Block * JobAf::v_newBlock( int numBlock)
{
return new Block( this, m_blocks_data[numBlock], m_progress);
}
void JobAf::setUser( UserAf * i_user)
{
m_user = i_user;
for( int b = 0; b < m_blocks_num; b++)
{
m_blocks[b]->setUser( i_user);
}
m_user_name = i_user->getName();
}
void JobAf::setBranch(BranchSrv * i_branch)
{
m_branch_srv = i_branch;
m_branch = m_branch_srv->getName();
}
bool JobAf::initialize()
{
AF_DEBUG << "'" << m_name << "'[" << m_id << "]:";
//
// Set job ID to blocks and progress classes:
m_progress->setJobId( m_id);
for( int b = 0; b < m_blocks_num; b++)
{
m_blocks_data[b]->setJobId( m_id);
}
//
// Store job ( if not stored )
if( isFromStore() == false )
{
setStoreDir( AFCommon::getStoreDirJob( *this));
initStoreDirs();
// Write blocks tasks data:
for( int b = 0; b < m_blocks_num; b++)
m_blocks[b]->storeTasks();
std::ostringstream ostr;
v_jsonWrite( ostr, 0);
std::string str = ostr.str();
AFCommon::writeFile( str.c_str(), str.size(), getStoreFile());
}
// Create tasks store folder (if does not exists any)
if(( af::pathIsFolder( m_store_dir_tasks) == false ) && ( af::pathMakePath( m_store_dir_tasks) == false ))
{
AFCommon::QueueLogError( std::string("Unable to create tasks store folder:\n") + m_store_dir_tasks);
return false;
}
//
// Executing pre commands ( if not from database )
if( isFromStore() == false )
{
if( false == m_command_pre.empty())
{
AFCommon::executeCmd( m_command_pre);
appendLog( std::string("Job pre command executed:\n") + m_command_pre);
}
for( int b = 0; b < m_blocks_num; b++)
{
if( m_blocks_data[b]->hasCmdPre() )
{
AFCommon::executeCmd( m_blocks_data[b]->getCmdPre());
appendLog( std::string("Block[") + m_blocks_data[b]->getName() + "] pre command executed:\n" + m_blocks_data[b]->getCmdPre());
}
}
// Process event
if( m_id != AFJOB::SYSJOB_ID ) // skip system job
{
if (hasCustomData() || m_user->hasCustomData())
{
std::vector<std::string> events;
events.push_back("JOB_CREATED");
emitEvents(events);
}
}
appendLog("Initialized.");
}
else
{
appendLog("Initialized from database.");
}
checkStates();
v_refresh( time(NULL), NULL, NULL);
return true;
}
void JobAf::checkStates()
{
// This function is called on a job initialization,
// When a new job created, or from database on server restart.
// Also it called on a block(s) appending.
for( int b = 0; b < m_blocks_num; b++)
{
int numtasks = m_blocks_data[b]->getTasksNum();
for( int t = 0; t < numtasks; t++)
{
uint32_t taskstate = m_progress->tp[b][t]->state;
if (taskstate == 0)
{
// This is a new job, not from database
if (m_blocks_data[b]->isSuspendingNewTasks())
taskstate = AFJOB::STATE_SUSPENDED_MASK;
else
taskstate = AFJOB::STATE_READY_MASK;
}
else if (taskstate == AFJOB::STATE_WARNING_MASK)
{
taskstate = AFJOB::STATE_READY_MASK;
}
else if( taskstate & AFJOB::STATE_RUNNING_MASK && ( false == m_blocks_data[b]->isMultiHost()))
{
taskstate = taskstate | AFJOB::STATE_WAITRECONNECT_MASK;
taskstate = taskstate & (~AFJOB::STATE_RUNNING_MASK );
m_progress->tp[b][t]->time_done = time(NULL);
m_blocks[b]->m_tasks[t]->v_appendLog(
"Task was running at server start. Waiting for render reconnect...");
}
m_progress->tp[b][t]->state = taskstate;
}
}
// If job is not done, just set WAITDEP state to not make it to run before a refresh.
if(( m_state & AFJOB::STATE_DONE_MASK) == false ) m_state = m_state | AFJOB::STATE_WAITDEP_MASK;
}
void JobAf::checkStatesOnAppend()
{
for( int b = 0; b < m_blocks_num; b++)
{
int numtasks = m_blocks_data[b]->getTasksNum();
for( int t = 0; t < numtasks; t++)
{
uint32_t taskstate = m_progress->tp[b][t]->state;
if( taskstate == 0 )
{
if (m_blocks_data[b]->isSuspendingNewTasks())
taskstate = AFJOB::STATE_SUSPENDED_MASK;
else
taskstate = AFJOB::STATE_READY_MASK;
m_progress->tp[b][t]->state = taskstate;
}
}
}
if(( m_state & AFJOB::STATE_DONE_MASK) == false ) m_state = m_state | AFJOB::STATE_WAITDEP_MASK;
}
int JobAf::getUid() const { return m_user->getId(); }
void JobAf::deleteNode( RenderContainer * renders, MonitorContainer * monitoring)
{
if (m_id == AFJOB::SYSJOB_ID)
{
AF_ERR << "An attempt to delete a system job.";
return;
}
if (false == m_deletion)
{
m_state |= AFJOB::STATE_OFFLINE_MASK;
m_state &= ~AFJOB::STATE_READY_MASK;
lock();
m_deletion = true;
if (hasCustomData() || m_user->hasCustomData())
{
std::vector<std::string> events;
events.push_back("JOB_DELETED");
emitEvents(events);
}
if (getRunningTasksNum() && (renders != NULL) && (monitoring != NULL))
{
restartAllTasks("Job deletion.", renders, monitoring, AFJOB::STATE_RUNNING_MASK);
if( monitoring ) monitoring->addJobEvent( af::Monitor::EVT_jobs_change, getId(), getUid());
return;
}
}
// Wait for all running tasks stop:
if (getRunningTasksNum())
{
return;
}
// Process job post command:
if (false == m_command_post.empty())
{
SysJob::AddPostCommand( m_command_post, m_blocks_num > 0 ? m_blocks_data[0]->getWDir(): "", m_user_name, m_name);
appendLog( std::string("Executing job post command:\n") + m_command_post);
}
// Process blocks post commands:
for (int b = 0; b < m_blocks_num; b++)
{
if (m_blocks_data[b]->hasCmdPost())
{
SysJob::AddPostCommand( m_blocks_data[b]->getCmdPost(), m_blocks_data[b]->getWDir(), m_user_name, m_name);
appendLog( std::string("Executing block[") + m_blocks_data[b]->getName() + "] post command:\n" + m_blocks_data[b]->getCmdPost());
}
}
setZombie();
AFCommon::DBAddJob( this);
m_branch_srv->removeJob(this, m_user);
if (monitoring)
{
monitoring->addJobEvent(af::Monitor::EVT_jobs_del, getId(), getUid());
monitoring->addEvent(af::Monitor::EVT_branches_change, m_branch_srv->getId());
}
AFCommon::QueueLog("Deleting a job: " + v_generateInfoString());
unLock();
}
void JobAf::v_action( Action & i_action)
{
// If action has blocks ids array - action to for blocks
if( i_action.data->HasMember("block_ids") || i_action.data->HasMember("block_mask"))
{
std::vector<int32_t> block_ids;
// Try to get block ids from array:
const JSON & j_block_ids = (*i_action.data)["block_ids"];
if( j_block_ids.IsArray())
af::jr_int32vec("block_ids", block_ids, *i_action.data);
// -1 id is specified = all blocks
if(( block_ids.size() == 1 ) && ( block_ids[0] == -1 ))
{
block_ids.clear();
for( int b = 0; b < m_blocks_num; b++)
block_ids.push_back( m_blocks[b]->m_data->getBlockNum());
}
// Try to fill ids from mask:
const JSON & j_block_mask = (*i_action.data)["block_mask"];
if( j_block_mask.IsString())
{
std::string mask;
af::jr_string("block_mask", mask, *i_action.data);
af::RegExp re;
std::string re_err;
if( false == re.setPattern( mask, &re_err))
{
appendLog("Invalid block mask = " + mask + " from " + i_action.author);
return;
}
for( int b = 0; b < m_blocks_num; b++)
if( re.match( m_blocks[b]->m_data->getName()))
block_ids.push_back( m_blocks[b]->m_data->getBlockNum());
}
if( block_ids.empty())
{
appendLog("\"block_ids\" array does not contain any integers or invalid \"block_mask\" from " + i_action.author);
return;
}
bool job_changed = false;
for( int b = 0; b < block_ids.size(); b++)
{
if(( block_ids[b] >= getBlocksNum()) || ( block_ids[b] < 0 ))
{
appendLog("Invalid block number = " + af::itos(block_ids[b]) + " " + i_action.author);
continue;
}
if( m_blocks[block_ids[b]]->action( i_action))
job_changed = true;
}
if (job_changed)
{
i_action.monitors->addJobEvent( af::Monitor::EVT_jobs_change, getId(), getUid());
m_force_refresh = true;
store();
}
return;
}
const JSON & operation = (*i_action.data)["operation"];
if( operation.IsObject())
{
std::string type;
af::jr_string("type", type, operation);
if( type == "delete")
{
if( m_id == AFJOB::SYSJOB_ID )
{
appendLog("System job can't be deleted by " + i_action.author);
return;
}
appendLog("Deleted by " + i_action.author);
m_user->appendLog( "Job \"" + m_name + "\" deleted by " + i_action.author);
deleteNode( i_action.renders, i_action.monitors);
return;
}
else if( type == "start")
{
m_state = m_state & (~AFJOB::STATE_OFFLINE_MASK);
}
else if( type == "pause")
{
m_state = m_state | AFJOB::STATE_OFFLINE_MASK;
}
else if( type == "stop")
{
restartAllTasks("Job stopped by " + i_action.author, i_action.renders, i_action.monitors, AFJOB::STATE_RUNNING_MASK);
m_state = m_state | AFJOB::STATE_OFFLINE_MASK;
}
else if( type == "restart_warnings")
{
restartAllTasks("Job restart warnings by " + i_action.author, i_action.renders, i_action.monitors, AFJOB::STATE_WARNING_MASK);
}
else if( type == "restart_running")
{
restartAllTasks("Job restart running by " + i_action.author, i_action.renders, i_action.monitors, AFJOB::STATE_RUNNING_MASK);
}
else if( type == "restart_skipped")
{
restartAllTasks("Job restart skipped by " + i_action.author, i_action.renders, i_action.monitors, AFJOB::STATE_SKIPPED_MASK);
}
else if( type == "restart_done")
{
restartAllTasks("Job restart done by " + i_action.author, i_action.renders, i_action.monitors, AFJOB::STATE_DONE_MASK);
}
else if( type == "reset_error_hosts")
{
for( int b = 0; b < m_blocks_num; b++)
m_blocks[b]->action( i_action);
}
else if(type == "reset_trying_next_tasks")
{
resetTryTasksNext();
}
else if( type == "restart")
{
restartAllTasks("Job restarted by " + i_action.author, i_action.renders, i_action.monitors);
checkDepends();
m_time_started = 0;
}
else if( type == "restart_errors")
{
restartAllTasks("Job errors restarted by " + i_action.author, i_action.renders, i_action.monitors, AFJOB::STATE_ERROR_MASK);
}
else if( type == "restart_pause")
{
restartAllTasks("Job restarted ( and paused ) by " + i_action.author, i_action.renders, i_action.monitors);
checkDepends();
m_state = m_state | AFJOB::STATE_OFFLINE_MASK;
m_time_started = 0;
}
else if( type == "append_blocks")
{
appendBlocks(i_action, operation);
return;
}
else
{
appendLog("Unknown operation \"" + type + "\" by " + i_action.author);
i_action.answerError("Unknown operation: " + type);
return;
}
appendLog("Operation \"" + type + "\" by " + i_action.author);
i_action.monitors->addJobEvent( af::Monitor::EVT_jobs_change, getId(), getUid());
m_force_refresh = true;
store();
return;
}
// Store some parameters before read, to check whether it changed
const std::string _user_name = m_user_name;
const std::string _branch = m_branch;
const JSON & params = (*i_action.data)["params"];
if( params.IsObject())
jsonRead( params, &i_action.log);
if( m_user_name != _user_name )
{
// User name was changed
UserAf * user = i_action.users->getUser( m_user_name);
if( user == NULL )
{
i_action.answerError("User does not exist: " + m_user_name);
m_user_name = _user_name;
return;
}
i_action.monitors->addEvent( af::Monitor::EVT_users_change, m_user->getId());
i_action.monitors->addJobEvent( af::Monitor::EVT_jobs_del, getId(), m_user->getId());
m_branch_srv->changeJobUser(m_user, this, user);
m_user->removeJob( this);
user->addJob( this); //< UserAf::addJob() updates JobAf::m_user
i_action.monitors->addEvent( af::Monitor::EVT_users_change, m_user->getId());
i_action.monitors->addJobEvent( af::Monitor::EVT_jobs_add, getId(), m_user->getId());
store();
return;
}
if (m_branch != _branch)
{
// Branch was changed
BranchSrv * new_branch_srv = i_action.branches->getBranch(m_branch);
if (new_branch_srv == NULL)
{
i_action.answerError("New job branch not found: " + m_branch);
m_branch = _branch;
return;
}
m_branch_srv->removeJob(this, m_user);
i_action.monitors->addEvent(af::Monitor::EVT_branches_change, m_branch_srv->getId());
new_branch_srv->addJob(this, m_user);
i_action.monitors->addEvent(af::Monitor::EVT_branches_change, m_branch_srv->getId());
}
if (i_action.log.size())
{
// Not empty log means some parameter change:
store();
m_force_refresh = true;
i_action.monitors->addJobEvent( af::Monitor::EVT_jobs_change, getId(), getUid());
}
}
void JobAf::v_priorityChanged( MonitorContainer * i_monitoring)
{
m_user->jobPriorityChanged( this, i_monitoring);
}
void JobAf::setUserListOrder( int index, bool updateDtabase)
{
int old_index = m_user_list_order;
m_user_list_order = index;
// user list order == -1 at job creation, and we do not store here in any case:
if(( old_index != -1 ) && ( index != old_index ) && updateDtabase )
store();
}
void JobAf::checkDepends()
{
m_state = m_state & (~AFJOB::STATE_WAITDEP_MASK);
bool depend_local = false;
bool depend_global = false;
// check global depends:
if( hasDependMaskGlobal())
{
JobContainerIt jobsIt( ms_jobs);
for( Job *job = jobsIt.job(); job != NULL; jobsIt.next(), job = jobsIt.job())
{
if( job == this ) continue;
if(( job->isDone() == false ) && ( checkDependMaskGlobal( job->getName()) ))
{
depend_global = true;
break;
}
}
}
// check local depends:
if( hasDependMask())
{
AfListIt jobsListIt( m_user->getJobsList());
for( AfNodeSrv *job = jobsListIt.node(); job != NULL; jobsListIt.next(), job = jobsListIt.node())
{
if( job == this ) continue;
if(( ((JobAf*)job)->isDone() == false ) && ( checkDependMask( ((JobAf*)job)->getName()) ))
{
depend_local = true;
break;
}
}
}
if( depend_local || depend_global ) m_state = m_state | AFJOB::STATE_WAITDEP_MASK;
}
bool JobAf::v_canRun()
{
if( isLocked() )
{
return false;
}
// Check some validness:
if( m_blocks_num < 1)
{
AF_ERR << "job has no blocks";
return false;
}
// check job state:
if( m_state & AFJOB::STATE_OFFLINE_MASK )
{
return false;
}
if( false == ( m_state & AFJOB::STATE_READY_MASK ))
{
return false;
}
return true;
}
bool JobAf::v_canRunOn( RenderAf * i_render)
{
// Check Render Nimby:
if( false == isIgnoreNimbyFlag())
{
if( i_render->isNIMBY())
return false;
if( i_render->isNimby())
if( i_render->getUserName() != m_user_name )
return false;
}
// Check Render Paused:
if( false == isIgnorePausedFlag())
{
if( i_render->isPaused())
return false;
}
// check at least one block can run on render
bool blockCanRunOn = false;
for( int b = 0; b < m_blocks_num; b++)
{
if( m_blocks[b]->canRunOn( i_render))
{
blockCanRunOn = true;
break;
}
}
if( false == blockCanRunOn )
return false;
return true;
}
void JobAf::addSolveCounts(MonitorContainer * i_monitoring, af::TaskExec * i_exec, RenderAf * i_render)
{
m_user->addSolveCounts(i_monitoring, i_exec, i_render);
m_branch_srv->addSolveCounts(i_monitoring, i_exec, i_render, m_user);
AfNodeSolve::addSolveCounts(i_exec, i_render);
i_monitoring->addJobEvent(af::Monitor::EVT_jobs_change, getId(), m_user->getId());
}
void JobAf::remSolveCounts(MonitorContainer * i_monitoring, af::TaskExec * i_exec, RenderAf * i_render)
{
m_user->remSolveCounts(i_monitoring, i_exec, i_render);
m_branch_srv->remSolveCounts(i_monitoring, i_exec, i_render, m_user);
AfNodeSolve::remSolveCounts(i_exec, i_render);
i_monitoring->addJobEvent(af::Monitor::EVT_jobs_change, getId(), m_user->getId());
}
bool JobAf::checkTryTasksNext()
{
if (m_try_this_tasks_num.size() == m_try_this_blocks_num.size())
{
for (int i = 0; i < m_try_this_tasks_num.size(); i++)
{
int t = m_try_this_tasks_num[i];
int b = m_try_this_blocks_num[i];
if (b < 0)
{
AF_ERR << "Tasks to try next has a negative block number.";
resetTryTasksNext();
return false;
}
if (t < 0)
{
AF_ERR << "Tasks to try next has a negative task number.";
resetTryTasksNext();
return false;
}
if (b >= m_blocks_num)
{
AF_ERR << "Tasks to try next has an invalid block number.";
resetTryTasksNext();
return false;
}
if (t >= m_blocks_data[b]->getTasksNum())
{
AF_ERR << "Tasks to try next has an invalid task number.";
resetTryTasksNext();
return false;
}
}
return true;
}
AF_ERR << "Try next tasks num and blocks num mismatch: "
<< m_try_this_tasks_num.size() << " != " << m_try_this_blocks_num.size();
resetTryTasksNext();
return false;
}
void JobAf::resetTryTasksNext()
{
for (int b = 0; b < m_blocks_num; b++)
for (int t = 0; t < m_blocks_data[b]->getTasksNum(); t++)
m_progress->tp[b][t]->state &= (~AFJOB::STATE_TRYTHISTASKNEXT_MASK);
m_try_this_tasks_num.clear();
m_try_this_blocks_num.clear();
}
void JobAf::tryTaskNext(bool i_append, int i_block_num, int i_task_num)
{
if (false == checkTryTasksNext())
return;
std::vector<int32_t>::iterator tIt = m_try_this_tasks_num.begin();
std::vector<int32_t>::iterator bIt = m_try_this_blocks_num.begin();
while (tIt != m_try_this_tasks_num.end())
{
if ((*tIt == i_task_num) && (*bIt == i_block_num))
{
if (i_append)
{
AF_ERR << "Try this task next: Already has a task " << *bIt << ":" << *tIt;
return;
}
m_try_this_tasks_num.erase(tIt);
m_try_this_blocks_num.erase(bIt);
return;
}
tIt++;
bIt++;
}
if (i_append)
{
m_try_this_tasks_num.push_back(i_task_num);
m_try_this_blocks_num.push_back(i_block_num);
}
else
AF_ERR << "Try this task next: Do not trying a task " << *bIt << ":" << *tIt;
}
RenderAf * JobAf::v_solve( std::list<RenderAf*> & i_renders_list, MonitorContainer * i_monitoring, BranchSrv * i_branch)
{
for( std::list<RenderAf*>::iterator rIt = i_renders_list.begin(); rIt != i_renders_list.end(); rIt++)
{
if( solveOnRender( *rIt, i_monitoring))
return *rIt;
}
return NULL;
}
bool JobAf::solveOnRender( RenderAf * i_render, MonitorContainer * i_monitoring)
{
// Prepare for the new solving (reset previous solvind stored data):
for( int b = 0; b < m_blocks_num; b++)
{
int numtasks = m_blocks_data[b]->getTasksNum();
for( int t = 0; t < numtasks; t++)
{
// Needed for recursion function, to not to try to solve the same task again
m_blocks[b]->m_tasks[t]->m_solved = false;
// Needed to store tasks that was tried, for nonsequential case:
m_progress->tp[b][t]->setNotSolved();
}
}
// First we solving tasks if users asked to try them next
if (hasTasksToTryNext() && checkTryTasksNext())
{
for (int i = 0; i < m_try_this_tasks_num.size(); i++)
{
int t = m_try_this_tasks_num[i];
int b = m_try_this_blocks_num[i];
if (false == (m_progress->tp[b][t]->state & AFJOB::STATE_READY_MASK))
continue;
if (false == (m_progress->tp[b][t]->state & AFJOB::STATE_TRYTHISTASKNEXT_MASK))
m_progress->tp[b][t]->state |= AFJOB::STATE_TRYTHISTASKNEXT_MASK;
if (solveTaskOnRender(i_render, b, t, i_monitoring))
return true;
}
}
// Next we just solving blocks in a cycle
for( int b = 0; b < m_blocks_num; b++)
{
if( false == ( m_blocks_data[b]->getState() & AFJOB::STATE_READY_MASK )) continue;
int task_num = m_blocks_data[b]->getReadyTaskNumber( m_progress->tp[b], m_flags, i_render);
if( task_num == AFJOB::TASK_NUM_NO_TASK )
{
// Block has no ready tasks
continue;
}
if( task_num == AFJOB::TASK_NUM_NO_SEQUENTIAL )
{
// All non sequential tasks solved, and job is PPA:
m_state = m_state | AFJOB::STATE_PPAPPROVAL_MASK;
continue;
}
if( m_state & AFJOB::STATE_PPAPPROVAL_MASK )
m_state = m_state & ( ~ AFJOB::STATE_PPAPPROVAL_MASK );
if( task_num < 0 )
{
AF_ERR << "Invalid task number returned from af::BlockData::getReadyTaskNumber()";
continue;
}
if (solveTaskOnRender(i_render, b, task_num, i_monitoring))
return true;
}
return false;
}
bool JobAf::solveTaskOnRender(RenderAf * i_render, int i_block_num, int i_task_num, MonitorContainer * i_monitoring)
{