/
EYiiron.php
1327 lines (1238 loc) · 41.1 KB
/
EYiiron.php
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
<?php
/**
* This class is a Yii application component. It handles the communication with the services on iron.io.
* It is built on top of this
* IronMQ - A cloud message queue. Using https://github.com/iron-io/iron_mq_php
* IronWorker - A service for parallel workers. Using https://github.com/iron-io/iron_worker_php
* IronCache - A cache service https://github.com/iron-io/iron_cache_php
* The classes are using https://github.com/iron-io/iron_core_php
*
* @author John Eskilsson <john.eskilsson@gmail.com>
* @link https://github.com/br0sk/yiiron
* @link http://br0sk.blogspot.co.uk/
* @copyright 2013
* @license New BSD License
*/
class EYiiron extends CApplicationComponent
{
/**
* This is the iron.io token. It is used for the REST authentication. You can find the token here
* @link https://hud.iron.io/dashboard
* @var integer
*/
public $token;
/**
* This is the project id. This is useful since we can create several connectors to different projects.
* You can find the project id here
* @link https://hud.iron.io/dashboard
* @var string
*/
public $projectId;
/**
* An array with the list of iron services to activate.
* Can be array('mq','worker','cache')
* @var array
*/
public $services = array('mq', 'worker', 'cache');
/**
* This array is used to exclude the files and folders we do not need to copy
* to the iron worker target environment. Supply it on the format of the options parameter
* for CFileHelper::copyDirectory().
*
* It has a reasonable default value set.
*
* @var array
*/
public $workerFileCopyOptions = array();
/**
* Needs to be set to true if the extension has been installed via Composer.
* In case of the installation being installed by composer the iron.io classes have been installed
* to the vendor library. If the extension is installed directly from the zip file the iron.io files
* are bundled.
*
* @var bool
*/
public $composer = false;
/**
* This is the path to the config file that shall be used when running as an IronWorker
* @var string
*/
public $configFile = 'config/console_ironworker.php';
/**
* This is the PHP version we are using, default is php-5.5.
* @var string
*/
public $stack = 'php-5.5';
/**
* This is the request object for iron MQ
* @var IronMQ
*/
private $_mq = null;
/**
* This is the request object for the Iron Workers
* @var IronWorker
*/
private $_worker = null;
/**
* This is the request object for the Iron Cache
* @var IronCache
*/
private $_cache = null;
/**
* Constructor.
*
* Just set the config parameters.
* @param string $token This is the IronIo token. It is used for the auth.
* @param string $projectId This is the project id. This is useful since we can create several connectors to different projects.
* @param array $workerFileCopyOptions Supply it on the format of the options parameter for CFileHelper::copyDirectory().
* @param boolean $composer Indicate if the extension has been installed via composer
* @param array $services
*/
public function __construct($token='',$projectId='',$services=array('mq', 'worker', 'cache'),$workerFileCopyOptions=array('exclude' => array('.git', '.csv', '.svn', '.zip', "/runtime", "/config")),$composer=false, $configFile="config/console_ironworker.php", $stack="php-5.5")
{
$this->token=$token;
$this->projectId=$projectId;
$this->services=$services;
$this->workerFileCopyOptions=$workerFileCopyOptions;
$this->composer=$composer;
$this->configFile=$configFile;
$this->stack=$stack;
/**
* Fix to not include the class twice in Unit tests. This fixes a problem with installing PHPUnit using Composer.
* note: If you are using a PEAR installation of PHPUnit you might need to remove the if statement.
*/
if(Yii::app()->getComponent('fixture') === null)
{
if($this->composer)
{
Yii::import('application.vendors.*');
require_once('iron-io/iron_core/IronCore.class.php');
require_once('iron-io/iron_worker/IronWorker.class.php');
require_once('iron-io/iron_mq/IronMQ.class.php');
require_once('iron-io/iron_cache/IronCache.class.php');
}
else
{
require_once('lib/IronCore.class.php');
require_once('lib/IronWorker.class.php');
require_once('lib/IronMQ.class.php');
require_once('lib/IronCache.class.php');
}
}
}
/**
* Make the service automatically connect to the specified iron.io services
*/
public function init()
{
$this->connect();
parent::init();
}
/**
* Connect to the allowed iron.io services.
* @throws CException If the service is not known this exception is thrown
*/
public function connect()
{
foreach ($this->services AS $service)
{
if($service == 'mq')
{
if($this->_mq == null){
try{
$this->_mq = new IronMQ(array(
'token' => $this->token,
'project_id' => $this->projectId,
));
Yii::log('Iron MQ in project '. $this->projectId.' is connected!', 'info', 'ext.yiiron');
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException('Error in IronMQ: '. $e->getMessage());
}
}
}
else if($service == 'worker')
{
if($this->_worker == null){
try{
$this->_worker = new IronWorker(array(
'token' => $this->token,
'project_id' => $this->projectId
));
Yii::log('Iron Worker in project '. $this->projectId.' is connected!', 'info', 'ext.yiiron');
}
catch(Exception $e){
Yii::log('Error in IronWorker: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException('Error in IronWorker: '. $e->getMessage());
}
}
}
else if($service == 'cache')
{
if($this->_cache == null){
try{
$this->_cache = new IronCache(array(
'token' => $this->token,
'project_id' => $this->projectId
));
Yii::log('Iron Cache in project'. $this->projectId.' is connected!', 'info', 'ext.yiiron');
}
catch(Exception $e){
Yii::log('Error in IronCache: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException('Error in IronCache: '. $e->getMessage());
}
}
}
else
{
Yii::log('Service '.$service.' is not available', 'error', 'ext.yiiron');
throw new CException('Service '.$service.' is not available');
}
}
}
/**
* This function shall only be used to make sure that the connection works initially and that the credentials are OK.
* It lists the queues to test the connection.
* @return bool
*/
public function testConnection()
{
//Create a connection to the iron mq service if it doesn't exist so we can check for if
//a connection can be made.
if($this->_mq == null)
{
$this->_mq = new IronMQ(array(
'token' => $this->token,
'project_id' => $this->projectId,
));
}
$conTestResult = $this->mqGetQueues();
if(is_array($conTestResult))
{
return true;
}
else
{
return false;
}
}
/**
* Disconnects all services previously connected.
*/
public function disconnect()
{
$this->_mq = null;
$this->_worker = null;
$this->_cache = null;
}
/**
* Returns the active Iron Worker object.
* @return IronWorker This is a reference to IronWorker object
*/
public function getRawWorker()
{
return $this->_worker;
}
/**
* Returns the active Iron Cache object.
* @return IronCache This is a reference to the IronCache object
*/
public function getRawCache()
{
return $this->_cache;
}
/**
* Returns the active IronMQ
* @return IronMQ This is a reference to IronMQ object
* @see I
*/
public function getRawMq()
{
return $this->_mq;
}
/********** All the improved worker functionality **************/
/**
* This method can execute any command line action as an IronWorker. The command line actions are normally
* executed directly in a shell. This method has been build so that we can fire off the command line actions
* directly from any Yii code. This way we don't have to fork off a process just to push an action to IronWorkers.
*
* Here is an example:
* This is how we can run a command from the command line to push it to IronWorkers
* ./yiic cronjobs myAction --param1=34 --ironWorker=true
*
* In order to run this action directly from for instance a controller you can do this:
* $yiiron = Yii::app()->yiiron;
* $yiiron->workerRunYiiAction('cronjobs', 'myAction', array('--param1=34', '--ironWorker=true'));
*
* If you leave out '--ironWorker=true' you can run the same command but locally not pushing it to IronWorkers.
*
* @note Remember that only none interactive command line actions can be run this way.
*
* @param null $command This is the command name. If the command class is CronjobsCommand this will be "cronjobs".
* @param null $action This is the name of the command. If the command is called actionDownloadFile this will be "downloadFile"
* @param array $options This is the array of parameters that can be sent in to the action.
* It is an array of strings on this format array("--filePath=/tmp/my_file.txt", "--newFileName=my_new_file.txt")
* @param boolean $silent Set this to true to suppress any output coming from the command line action. This is only
* valid for the code being run locally. When you check the log in the iron.io hub you will still see the trace.
* @param string $entryScript This is normally the string "yiic". You will only have to set this if you are using a custom
* entry script.
* @return integer The IronWorker id
*/
public function workerRunYiiAction($command=null, $action=null, $options=array(), $silent=true, $entryScript="yiic") {
$commandPath = Yii::app()->getBasePath() . DIRECTORY_SEPARATOR . 'commands';
$runner = new CConsoleCommandRunner();
$runner->addCommands($commandPath);
$args = array($entryScript, $command, $action);
//Add in the options
foreach ($options AS $option)
{
$args[] = $option;
}
//Buffer the output to go silent not outputting text when using the commands in non CLI code
ob_start();
$res = $runner->run($args);
//Discard the output if silent
if($silent)
ob_end_clean();
else
echo htmlentities(ob_get_clean(), null, Yii::app()->charset);
return $res;
}
/********** All the MQ wrappers **************/
/**
* Get list of message queues
*
* @param int $page
* Zero-indexed page to view
* @param int $per_page
* Number of queues per page
* @throws CException
* @return mixed
*/
public function mqGetQueues($page = 0, $per_page = IronMQ::LIST_QUEUES_PER_PAGE) {
try{
return $this->_mq->getQueues($page, $per_page);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Get information about queue.
* Also returns queue size.
*
* @param string $queue_name
* @return mixed
* @throws CException
*/
public function getQueue($queue_name) {
try{
return $this->_mq->getQueue($queue_name);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Clear all messages from queue.
*
* @param string $queue_name
* @return mixed
* @throws CException
*/
public function mqClearQueue($queue_name) {
try{
return $this->_mq->clearQueue($queue_name);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Push a message on the queue
*
* Examples:
* <code>
* $ironmq->postMessage("test_queue", "Hello world");
* </code>
* <code>
* $ironmq->postMessage("test_queue", "Test Message",e array(
* 'timeout' => 120,
* 'delay' => 2,
* 'expires_in' => 2*24*3600 # 2 days
* ));
* </code>
*
* @param string $queue_name Name of the queue.
* @param string $message
* @param array $properties
* @return mixed
* @throws CException
*/
public function mqPostMessage($queue_name, $message, $properties = array()) {
try{
return $this->_mq->PostMessage($queue_name, $message, $properties);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Push multiple messages on the queue
*
* Example:
* <code>
* $ironmq->postMessages("test_queue", array("Lorem", "Ipsum"), array(
* 'timeout' => 120,
* 'delay' => 2,
* 'expires_in' => 2*24*3600 # 2 days
* ));
* </code>
*
* @param string $queue_name Name of the queue.
* @param array $messages array of messages, each message same as for postMessage() method
* @param array $properties array of message properties, applied to each message in $messages
* @return mixed
* @throws CException
*/
public function mqPostMessages($queue_name, $messages, $properties = array()) {
try{
return $this->_mq->postMessages($queue_name, $messages, $properties);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Get multiple messages from queue
*
* @param string $queue_name Queue name
* @param int $count
* @param int $timeout
* @return array|null array of messages or null
* @throws CException
*/
public function mqGetMessages($queue_name, $count = 1, $timeout = IronMQ::GET_MESSAGE_TIMEOUT) {
try{
return $this->_mq->getMessages($queue_name, $count, $timeout);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Get single message from queue
*
* @param string $queue_name Queue name
* @param int $timeout
* @return mixed|null single message or null
* @throws CException
*/
public function mqGetMessage($queue_name, $timeout = IronMQ::GET_MESSAGE_TIMEOUT) {
try{
return $this->_mq->GetMessage($queue_name, $timeout);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Delete a Message from a Queue
* This call will delete the message. Be sure you call this after you’re done with a message or it will be placed back on the queue.
*
* @param $queue_name
* @param $message_id
* @return mixed
* @throws CException
*/
public function mqDeleteMessage($queue_name, $message_id) {
try{
return $this->_mq->deleteMessage($queue_name, $message_id);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Peek Messages on a Queue
* Peeking at a queue returns the next messages on the queue, but it does not reserve them.
*
* @param string $queue_name
* @return object|null message or null if queue is empty
* @throws CException
*/
public function mqPeekMessage($queue_name) {
try{
return $this->_mq->peekMessage($queue_name);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Peek Messages on a Queue
* Peeking at a queue returns the next messages on the queue, but it does not reserve them.
*
* @param string $queue_name
* @param int $count The maximum number of messages to peek. Maximum is 100.
* @return array|null array of messages or null if queue is empty
* @throws CException
*/
public function mqPeekMessages($queue_name, $count) {
try{
return $this->_mq->peekMessages($queue_name, $count);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Touch a Message on a Queue
* Touching a reserved message extends its timeout by the duration specified when the message was created, which is 60 seconds by default.
*
* @param string $queue_name
* @param string $message_id
* @return mixed
* @throws CException
*/
public function mqTouchMessage($queue_name, $message_id) {
try{
return $this->_mq->touchMessage($queue_name, $message_id);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Release a Message on a Queue
* Releasing a reserved message unreserves the message and puts it back on the queue as if the message had timed out.
*
* @param string $queue_name
* @param string $message_id
* @param int $delay The item will not be available on the queue until this many seconds have passed. Default is 0 seconds. Maximum is 604,800 seconds (7 days).
* @return mixed
* @throws CException
*/
public function mqReleaseMessage($queue_name, $message_id, $delay = 0) {
try{
return $this->_mq->releaseMessage($queue_name, $message_id, $delay);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Updates the queue object
*
* @param string $queue_name
* @param array $options Parameters to change. keys:
* - "subscribers" url's to subscribe to
* - "push_type" multicast (default) or unicast.
* - "retries" Number of retries. 3 by default
* - "retries_delay" Delay between retries. 60 (seconds) by default
* @throws CException
*/
public function mqUpdateQueue($queue_name, $options) {
try{
return $this->_mq->updateQueue($queue_name, $options);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function mqDeleteQueue($queue_name) {
try{
return $this->_mq->deleteQueue($queue_name);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Add Subscriber to a Queue
*
* Example:
* <code>
* $ironmq->addSubscriber("test_queue", array("url" => "http://example.com"));
* </code>
*
* @param string $queue_name
* @param array $subscriber_hash Subscriber. keys:
* - "url" Subscriber url
* @return mixed
* @throws CException
*/
public function mqAddSubscriber($queue_name, $subscriber_hash) {
try{
return $this->_mq->addSubscriber($queue_name, $subscriber_hash);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Remove Subscriber from a Queue
*
* Example:
* <code>
* $ironmq->removeSubscriber("test_queue", array("url" => "http://example.com"));
* </code>
*
* @param string $queue_name
* @param array $subscriber_hash Subscriber. keys:
* - "url" Subscriber url
* @return mixed
* @throws CException
*/
public function mqRemoveSubscriber($queue_name, $subscriber_hash) {
try{
return $this->_mq->removeSubscriber($queue_name, $subscriber_hash);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Get Message's Push Statuses (for Push Queues only)
*
* Example:
* <code>
* statuses = $ironmq->getMessagePushStatuses("test_queue", $message_id)
* </code>
*
* @param string $queue_name
* @param string $message_id
* @throws CException
* @return array
*/
public function mqGetMessagePushStatuses($queue_name, $message_id) {
try{
return $this->_mq->GetMessagePushStatuses($queue_name, $message_id);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Delete Message's Push Status (for Push Queues only)
*
* Example:
* <code>
* $ironmq->deleteMessagePushStatus("test_queue", $message_id, $subscription_id)
* </code>
*
* @param string $queue_name
* @param string $message_id
* @param string $subscription_id
* @throws CException
* @return mixed
*/
public function mqDeleteMessagePushStatus($queue_name, $message_id, $subscription_id) {
try{
return $this->_mq->deleteMessagePushStatus($queue_name, $message_id, $subscription_id);
}
catch(Exception $e){
Yii::log('Error in IronMQ: '. $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/********** All the Worker wrappers **************/
/**
* Zips and uploads your code
*
* Shortcut for zipDirectory() + postCode()
*
* @param string $directory Directory with worker files
* @param string $run_filename This file will be launched as worker
* @param string $code_name Referenceable (unique) name for your worker
* @param array $options Optional parameters:
* - "max_concurrency" The maximum number of tasks that should be run in parallel.
* - "retries" The number of auto-retries of failed task.
* - "retries_delay" Delay in seconds between retries.
* @return bool Result of operation
* @throws CException
*/
public function workerUpload($directory, $run_filename, $code_name, $options = array()){
try {
return $this->_worker->upload($directory, $run_filename, $code_name, $options);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function workerGetProjects(){
try {
return $this->_worker->getProjects();
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* List Tasks
*
* @param int $page Page. Default is 0, maximum is 100.
* @param int $per_page The number of tasks to return per page. Default is 30, maximum is 100.
* @param array $options Optional URL Parameters
* Filter by Status: the parameters queued, running, complete, error, cancelled, killed, and timeout will all filter by their respective status when given a value of 1. These parameters can be mixed and matched to return tasks that fall into any of the status filters. If no filters are provided, tasks will be displayed across all statuses.
* - "from_time" Limit the retrieved tasks to only those that were created after the time specified in the value. Time should be formatted as the number of seconds since the Unix epoch.
* - "to_time" Limit the retrieved tasks to only those that were created before the time specified in the value. Time should be formatted as the number of seconds since the Unix epoch.
* @return mixed
* @throws CException
*/
public function workerGetTasks($page = 0, $per_page = 30, $options = array()){
try {
return $this->_worker->getTasks($page, $per_page, $options);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function workerGetProjectDetails(){
try {
return $this->_worker->getProjectDetails();
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function workerGetCodes($page = 0, $per_page = 30){
try {
return $this->_worker->getCodes($page, $per_page);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function workerGetCodeDetails($code_id){
try {
return $this->_worker->getCodeDetails($code_id);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Uploads your code package
*
* @param string $filename This file will be launched as worker
* @param string $zipFilename zip file containing code to execute
* @param string $name referenceable (unique) name for your worker
* @param array $options Optional parameters:
* - "max_concurrency" The maximum number of tasks that should be run in parallel.
* - "retries" The number of auto-retries of failed task.
* - "retries_delay" Delay in seconds between retries.
* @return mixed
* @throws CException
*/
public function workerPostCode($filename, $zipFilename, $name, $options = array()){
try {
return $this->_worker->postCode($filename, $zipFilename, $name, $options);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function workerDeleteCode($code_id){
try {
return $this->_worker->deleteCode($code_id);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function workerDeleteSchedule($schedule_id){
try {
return $this->_worker->deleteSchedule($schedule_id);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Get information about all schedules for project
*
* @param int $page
* @param int $per_page
* @return mixed
* @throws CException
*/
public function workerGetSchedules($page = 0, $per_page = 30){
try {
return $this->_worker->getSchedules($page, $per_page);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Get information about schedule
*
* @param string $schedule_id Schedule ID
* @return mixed
* @throws CException
*/
public function workerGetSchedule($schedule_id){
try {
return $this->_worker->getSchedule($schedule_id);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Schedules task
*
* @param string $name Package name
* @param array $payload Payload for task
* @param int $delay Delay in seconds
* @return string Created Schedule id
* @throws CException
*/
public function workerPostScheduleSimple($name, $payload = array(), $delay = 1){
try {
return $this->_worker->postScheduleSimple($name, $payload, $delay);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Schedules task
*
* @param string $name Package name
* @param array $payload Payload for task
* @param int|DateTime $start_at Time of first run in unix timestamp format or as DateTime instance. Example: time()+2*60
* @param int $run_every Time in seconds between runs. If omitted, task will only run once.
* @param int|DateTime $end_at Time tasks will stop being enqueued in unix timestamp or as DateTime instance format.
* @param int $run_times Number of times to run task.
* @param int $priority Priority queue to run the job in (0, 1, 2). p0 is default.
* @return string Created Schedule id
* @throws CException
*/
public function workerPostScheduleAdvanced($name, $payload = array(), $start_at, $run_every = null, $end_at = null, $run_times = null, $priority = null){
try {
return $this->_worker->postScheduleAdvanced($name, $payload, $start_at, $run_every, $end_at, $run_times, $priority);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Queues already uploaded worker
*
* @param string $name Package name
* @param array $payload Payload for task
* @param array $options Optional parameters:
* - "priority" priority queue to run the job in (0, 1, 2). 0 is default.
* - "timeout" maximum runtime of your task in seconds. Maximum time is 3600 seconds (60 minutes). Default is 3600 seconds (60 minutes).
* - "delay" delay before actually queueing the task in seconds. Default is 0 seconds.
* @return string Created Task ID
* @throws CException
*/
public function workerPostTask($name, $payload = array(), $options = array()){
try {
return $this->_worker->postTask($name, $payload, $options);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function getLog($task_id){
try {
return $this->_worker->getLog($task_id);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function workerGetTaskDetails($task_id){
try {
return $this->_worker->getTaskDetails($task_id);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function workerCancelTask($task_id){
try {
return $this->_worker->cancelTask($task_id);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
public function workerDeleteTask($task_id){
try {
return $this->_worker->deleteTask($task_id);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}
/**
* Wait while the task specified by task_id executes
*
* @param string $task_id Task ID
* @param int $sleep Delay between API invocations in seconds
* @param int $max_wait_time Maximum waiting time in seconds, 0 for infinity
* @return mixed $details Task details or false
* @throws CException
*/
public function workerWaitFor($task_id, $sleep = 5, $max_wait_time = 0){
try {
return $this->_worker->waitFor($task_id, $sleep, $max_wait_time);
}
catch (Exception $e) {
Yii::log('Error in IronWorker: ' . $e->getMessage(), 'error', 'ext.yiiron');
throw new CException($e->getMessage());
}
}