forked from jamesgao/ipython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientconnector.py
More file actions
788 lines (674 loc) · 30.2 KB
/
clientconnector.py
File metadata and controls
788 lines (674 loc) · 30.2 KB
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
# encoding: utf-8
"""Facilities for handling client connections to the controller."""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2009 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from __future__ import with_statement
import os
from IPython.kernel.fcutil import (
Tub,
find_furl,
is_valid_furl,
is_valid_furl_file,
is_valid_furl_or_file,
validate_furl_or_file,
FURLError
)
from IPython.kernel.clusterdir import ClusterDir, ClusterDirError
from IPython.kernel.launcher import IPClusterLauncher
from IPython.kernel.twistedutil import (
gatherBoth,
make_deferred,
blockingCallFromThread,
sleep_deferred
)
from IPython.utils.importstring import import_item
from IPython.utils.path import get_ipython_dir
from twisted.internet import defer
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.python import failure, log
#-----------------------------------------------------------------------------
# The ClientConnector class
#-----------------------------------------------------------------------------
DELAY = 0.2
MAX_TRIES = 9
class ClientConnectorError(Exception):
pass
class AsyncClientConnector(object):
"""A class for getting remote references and clients from furls.
This start a single :class:`Tub` for all remote reference and caches
references.
"""
def __init__(self):
self._remote_refs = {}
self.tub = Tub()
self.tub.startService()
def _find_furl(self, profile='default', cluster_dir=None,
furl_or_file=None, furl_file_name=None,
ipython_dir=None):
"""Find a FURL file.
If successful, this returns a FURL file that exists on the file
system. The contents of the file have not been checked though. This
is because we often have to deal with FURL file whose buffers have
not been flushed.
This raises an :exc:`~IPython.kernel.fcutil.FURLError` exception
if a FURL file can't be found.
This tries the following:
1. By the name ``furl_or_file``.
2. By ``cluster_dir`` and ``furl_file_name``.
3. By cluster profile with a default of ``default``. This uses
``ipython_dir``.
"""
# Try by furl_or_file
if furl_or_file is not None:
if is_valid_furl_or_file(furl_or_file):
return furl_or_file
if furl_file_name is None:
raise FURLError('A furl_file_name must be provided if furl_or_file is not')
# Try by cluster_dir
if cluster_dir is not None:
cluster_dir_obj = ClusterDir.find_cluster_dir(cluster_dir)
sdir = cluster_dir_obj.security_dir
furl_file = os.path.join(sdir, furl_file_name)
validate_furl_or_file(furl_file)
return furl_file
# Try by profile
if ipython_dir is None:
ipython_dir = get_ipython_dir()
if profile is not None:
cluster_dir_obj = ClusterDir.find_cluster_dir_by_profile(
ipython_dir, profile)
sdir = cluster_dir_obj.security_dir
furl_file = os.path.join(sdir, furl_file_name)
validate_furl_or_file(furl_file)
return furl_file
raise FURLError('Could not find a valid FURL file.')
def get_reference(self, furl_or_file):
"""Get a remote reference using a furl or a file containing a furl.
Remote references are cached locally so once a remote reference
has been retrieved for a given furl, the cached version is
returned.
Parameters
----------
furl_or_file : str
A furl or a filename containing a furl. This should already be
validated, but might not yet exist.
Returns
-------
A deferred to a remote reference
"""
furl = furl_or_file
if furl in self._remote_refs:
d = defer.succeed(self._remote_refs[furl])
else:
d = self.tub.getReference(furl)
d.addCallback(self._save_ref, furl)
return d
def _save_ref(self, ref, furl):
"""Cache a remote reference by its furl."""
self._remote_refs[furl] = ref
return ref
def get_task_client(self, profile='default', cluster_dir=None,
furl_or_file=None, ipython_dir=None,
delay=DELAY, max_tries=MAX_TRIES):
"""Get the task controller client.
This method is a simple wrapper around `get_client` that passes in
the default name of the task client FURL file. Usually only
the ``profile`` option will be needed. If a FURL file can't be
found by its profile, use ``cluster_dir`` or ``furl_or_file``.
Parameters
----------
profile : str
The name of a cluster directory profile (default="default"). The
cluster directory "cluster_<profile>" will be searched for
in ``os.getcwd()``, the ipython_dir and then in the directories
listed in the :env:`IPCLUSTER_DIR_PATH` environment variable.
cluster_dir : str
The full path to a cluster directory. This is useful if profiles
are not being used.
furl_or_file : str
A furl or a filename containing a FURL. This is useful if you
simply know the location of the FURL file.
ipython_dir : str
The location of the ipython_dir if different from the default.
This is used if the cluster directory is being found by profile.
delay : float
The initial delay between re-connection attempts. Susequent delays
get longer according to ``delay[i] = 1.5*delay[i-1]``.
max_tries : int
The max number of re-connection attempts.
Returns
-------
A deferred to the actual client class.
"""
return self.get_client(
profile, cluster_dir, furl_or_file,
'ipcontroller-tc.furl', ipython_dir,
delay, max_tries
)
def get_multiengine_client(self, profile='default', cluster_dir=None,
furl_or_file=None, ipython_dir=None,
delay=DELAY, max_tries=MAX_TRIES):
"""Get the multiengine controller client.
This method is a simple wrapper around `get_client` that passes in
the default name of the task client FURL file. Usually only
the ``profile`` option will be needed. If a FURL file can't be
found by its profile, use ``cluster_dir`` or ``furl_or_file``.
Parameters
----------
profile : str
The name of a cluster directory profile (default="default"). The
cluster directory "cluster_<profile>" will be searched for
in ``os.getcwd()``, the ipython_dir and then in the directories
listed in the :env:`IPCLUSTER_DIR_PATH` environment variable.
cluster_dir : str
The full path to a cluster directory. This is useful if profiles
are not being used.
furl_or_file : str
A furl or a filename containing a FURL. This is useful if you
simply know the location of the FURL file.
ipython_dir : str
The location of the ipython_dir if different from the default.
This is used if the cluster directory is being found by profile.
delay : float
The initial delay between re-connection attempts. Susequent delays
get longer according to ``delay[i] = 1.5*delay[i-1]``.
max_tries : int
The max number of re-connection attempts.
Returns
-------
A deferred to the actual client class.
"""
return self.get_client(
profile, cluster_dir, furl_or_file,
'ipcontroller-mec.furl', ipython_dir,
delay, max_tries
)
def get_client(self, profile='default', cluster_dir=None,
furl_or_file=None, furl_file_name=None, ipython_dir=None,
delay=DELAY, max_tries=MAX_TRIES):
"""Get a remote reference and wrap it in a client by furl.
This method is a simple wrapper around `get_client` that passes in
the default name of the task client FURL file. Usually only
the ``profile`` option will be needed. If a FURL file can't be
found by its profile, use ``cluster_dir`` or ``furl_or_file``.
Parameters
----------
profile : str
The name of a cluster directory profile (default="default"). The
cluster directory "cluster_<profile>" will be searched for
in ``os.getcwd()``, the ipython_dir and then in the directories
listed in the :env:`IPCLUSTER_DIR_PATH` environment variable.
cluster_dir : str
The full path to a cluster directory. This is useful if profiles
are not being used.
furl_or_file : str
A furl or a filename containing a FURL. This is useful if you
simply know the location of the FURL file.
furl_file_name : str
The filename (not the full path) of the FURL. This must be
provided if ``furl_or_file`` is not.
ipython_dir : str
The location of the ipython_dir if different from the default.
This is used if the cluster directory is being found by profile.
delay : float
The initial delay between re-connection attempts. Susequent delays
get longer according to ``delay[i] = 1.5*delay[i-1]``.
max_tries : int
The max number of re-connection attempts.
Returns
-------
A deferred to the actual client class. Or a failure to a
:exc:`FURLError`.
"""
try:
furl_file = self._find_furl(
profile, cluster_dir, furl_or_file,
furl_file_name, ipython_dir
)
# If this succeeds, we know the furl file exists and has a .furl
# extension, but it could still be empty. That is checked each
# connection attempt.
except FURLError:
return defer.fail(failure.Failure())
def _wrap_remote_reference(rr):
d = rr.callRemote('get_client_name')
d.addCallback(lambda name: import_item(name))
def adapt(client_interface):
client = client_interface(rr)
client.tub = self.tub
return client
d.addCallback(adapt)
return d
d = self._try_to_connect(furl_file, delay, max_tries, attempt=0)
d.addCallback(_wrap_remote_reference)
d.addErrback(self._handle_error, furl_file)
return d
def _handle_error(self, f, furl_file):
raise ClientConnectorError('Could not connect to the controller '
'using the FURL file. This usually means that i) the controller '
'was not started or ii) a firewall was blocking the client from '
'connecting to the controller: %s' % furl_file)
@inlineCallbacks
def _try_to_connect(self, furl_or_file, delay, max_tries, attempt):
"""Try to connect to the controller with retry logic."""
if attempt < max_tries:
log.msg("Connecting [%r]" % attempt)
try:
self.furl = find_furl(furl_or_file)
# Uncomment this to see the FURL being tried.
# log.msg("FURL: %s" % self.furl)
rr = yield self.get_reference(self.furl)
log.msg("Connected: %s" % furl_or_file)
except:
if attempt==max_tries-1:
# This will propagate the exception all the way to the top
# where it can be handled.
raise
else:
yield sleep_deferred(delay)
rr = yield self._try_to_connect(
furl_or_file, 1.5*delay, max_tries, attempt+1
)
returnValue(rr)
else:
returnValue(rr)
else:
raise ClientConnectorError(
'Could not connect to controller, max_tries (%r) exceeded. '
'This usually means that i) the controller was not started, '
'or ii) a firewall was blocking the client from connecting '
'to the controller.' % max_tries
)
class ClientConnector(object):
"""A blocking version of a client connector.
This class creates a single :class:`Tub` instance and allows remote
references and client to be retrieved by their FURLs. Remote references
are cached locally and FURL files can be found using profiles and cluster
directories.
"""
def __init__(self):
self.async_cc = AsyncClientConnector()
def get_task_client(self, profile='default', cluster_dir=None,
furl_or_file=None, ipython_dir=None,
delay=DELAY, max_tries=MAX_TRIES):
"""Get the task client.
Usually only the ``profile`` option will be needed. If a FURL file
can't be found by its profile, use ``cluster_dir`` or
``furl_or_file``.
Parameters
----------
profile : str
The name of a cluster directory profile (default="default"). The
cluster directory "cluster_<profile>" will be searched for
in ``os.getcwd()``, the ipython_dir and then in the directories
listed in the :env:`IPCLUSTER_DIR_PATH` environment variable.
cluster_dir : str
The full path to a cluster directory. This is useful if profiles
are not being used.
furl_or_file : str
A furl or a filename containing a FURL. This is useful if you
simply know the location of the FURL file.
ipython_dir : str
The location of the ipython_dir if different from the default.
This is used if the cluster directory is being found by profile.
delay : float
The initial delay between re-connection attempts. Susequent delays
get longer according to ``delay[i] = 1.5*delay[i-1]``.
max_tries : int
The max number of re-connection attempts.
Returns
-------
The task client instance.
"""
client = blockingCallFromThread(
self.async_cc.get_task_client, profile, cluster_dir,
furl_or_file, ipython_dir, delay, max_tries
)
return client.adapt_to_blocking_client()
def get_multiengine_client(self, profile='default', cluster_dir=None,
furl_or_file=None, ipython_dir=None,
delay=DELAY, max_tries=MAX_TRIES):
"""Get the multiengine client.
Usually only the ``profile`` option will be needed. If a FURL file
can't be found by its profile, use ``cluster_dir`` or
``furl_or_file``.
Parameters
----------
profile : str
The name of a cluster directory profile (default="default"). The
cluster directory "cluster_<profile>" will be searched for
in ``os.getcwd()``, the ipython_dir and then in the directories
listed in the :env:`IPCLUSTER_DIR_PATH` environment variable.
cluster_dir : str
The full path to a cluster directory. This is useful if profiles
are not being used.
furl_or_file : str
A furl or a filename containing a FURL. This is useful if you
simply know the location of the FURL file.
ipython_dir : str
The location of the ipython_dir if different from the default.
This is used if the cluster directory is being found by profile.
delay : float
The initial delay between re-connection attempts. Susequent delays
get longer according to ``delay[i] = 1.5*delay[i-1]``.
max_tries : int
The max number of re-connection attempts.
Returns
-------
The multiengine client instance.
"""
client = blockingCallFromThread(
self.async_cc.get_multiengine_client, profile, cluster_dir,
furl_or_file, ipython_dir, delay, max_tries
)
return client.adapt_to_blocking_client()
def get_client(self, profile='default', cluster_dir=None,
furl_or_file=None, ipython_dir=None,
delay=DELAY, max_tries=MAX_TRIES):
client = blockingCallFromThread(
self.async_cc.get_client, profile, cluster_dir,
furl_or_file, ipython_dir,
delay, max_tries
)
return client.adapt_to_blocking_client()
class ClusterStateError(Exception):
pass
class AsyncCluster(object):
"""An class that wraps the :command:`ipcluster` script."""
def __init__(self, profile='default', cluster_dir=None, ipython_dir=None,
auto_create=False, auto_stop=True):
"""Create a class to manage an IPython cluster.
This class calls the :command:`ipcluster` command with the right
options to start an IPython cluster. Typically a cluster directory
must be created (:command:`ipcluster create`) and configured before
using this class. Configuration is done by editing the
configuration files in the top level of the cluster directory.
Parameters
----------
profile : str
The name of a cluster directory profile (default="default"). The
cluster directory "cluster_<profile>" will be searched for
in ``os.getcwd()``, the ipython_dir and then in the directories
listed in the :env:`IPCLUSTER_DIR_PATH` environment variable.
cluster_dir : str
The full path to a cluster directory. This is useful if profiles
are not being used.
ipython_dir : str
The location of the ipython_dir if different from the default.
This is used if the cluster directory is being found by profile.
auto_create : bool
Automatically create the cluster directory it is dones't exist.
This will usually only make sense if using a local cluster
(default=False).
auto_stop : bool
Automatically stop the cluster when this instance is garbage
collected (default=True). This is useful if you want the cluster
to live beyond your current process. There is also an instance
attribute ``auto_stop`` to change this behavior.
"""
self._setup_cluster_dir(profile, cluster_dir, ipython_dir, auto_create)
self.state = 'before'
self.launcher = None
self.client_connector = None
self.auto_stop = auto_stop
def __del__(self):
if self.auto_stop and self.state=='running':
print "Auto stopping the cluster..."
self.stop()
@property
def location(self):
if hasattr(self, 'cluster_dir_obj'):
return self.cluster_dir_obj.location
else:
return ''
@property
def running(self):
if self.state=='running':
return True
else:
return False
def _setup_cluster_dir(self, profile, cluster_dir, ipython_dir, auto_create):
if ipython_dir is None:
ipython_dir = get_ipython_dir()
if cluster_dir is not None:
try:
self.cluster_dir_obj = ClusterDir.find_cluster_dir(cluster_dir)
except ClusterDirError:
pass
if profile is not None:
try:
self.cluster_dir_obj = ClusterDir.find_cluster_dir_by_profile(
ipython_dir, profile)
except ClusterDirError:
pass
if auto_create or profile=='default':
# This should call 'ipcluster create --profile default
self.cluster_dir_obj = ClusterDir.create_cluster_dir_by_profile(
ipython_dir, profile)
else:
raise ClusterDirError('Cluster dir not found.')
@make_deferred
def start(self, n=2):
"""Start the IPython cluster with n engines.
Parameters
----------
n : int
The number of engine to start.
"""
# We might want to add logic to test if the cluster has started
# by another process....
if not self.state=='running':
self.launcher = IPClusterLauncher(os.getcwd())
self.launcher.ipcluster_n = n
self.launcher.ipcluster_subcommand = 'start'
d = self.launcher.start()
d.addCallback(self._handle_start)
return d
else:
raise ClusterStateError('Cluster is already running')
@make_deferred
def stop(self):
"""Stop the IPython cluster if it is running."""
if self.state=='running':
d1 = self.launcher.observe_stop()
d1.addCallback(self._handle_stop)
d2 = self.launcher.stop()
return gatherBoth([d1, d2], consumeErrors=True)
else:
raise ClusterStateError("Cluster not running")
def get_multiengine_client(self, delay=DELAY, max_tries=MAX_TRIES):
"""Get the multiengine client for the running cluster.
If this fails, it means that the cluster has not finished starting.
Usually waiting a few seconds are re-trying will solve this.
"""
if self.client_connector is None:
self.client_connector = AsyncClientConnector()
return self.client_connector.get_multiengine_client(
cluster_dir=self.cluster_dir_obj.location,
delay=delay, max_tries=max_tries
)
def get_task_client(self, delay=DELAY, max_tries=MAX_TRIES):
"""Get the task client for the running cluster.
If this fails, it means that the cluster has not finished starting.
Usually waiting a few seconds are re-trying will solve this.
"""
if self.client_connector is None:
self.client_connector = AsyncClientConnector()
return self.client_connector.get_task_client(
cluster_dir=self.cluster_dir_obj.location,
delay=delay, max_tries=max_tries
)
def get_ipengine_logs(self):
return self.get_logs_by_name('ipengine')
def get_ipcontroller_logs(self):
return self.get_logs_by_name('ipcontroller')
def get_ipcluster_logs(self):
return self.get_logs_by_name('ipcluster')
def get_logs_by_name(self, name='ipcluster'):
log_dir = self.cluster_dir_obj.log_dir
logs = {}
for log in os.listdir(log_dir):
if log.startswith(name + '-') and log.endswith('.log'):
with open(os.path.join(log_dir, log), 'r') as f:
logs[log] = f.read()
return logs
def get_logs(self):
d = self.get_ipcluster_logs()
d.update(self.get_ipengine_logs())
d.update(self.get_ipcontroller_logs())
return d
def _handle_start(self, r):
self.state = 'running'
def _handle_stop(self, r):
self.state = 'after'
class Cluster(object):
def __init__(self, profile='default', cluster_dir=None, ipython_dir=None,
auto_create=False, auto_stop=True):
"""Create a class to manage an IPython cluster.
This class calls the :command:`ipcluster` command with the right
options to start an IPython cluster. Typically a cluster directory
must be created (:command:`ipcluster create`) and configured before
using this class. Configuration is done by editing the
configuration files in the top level of the cluster directory.
Parameters
----------
profile : str
The name of a cluster directory profile (default="default"). The
cluster directory "cluster_<profile>" will be searched for
in ``os.getcwd()``, the ipython_dir and then in the directories
listed in the :env:`IPCLUSTER_DIR_PATH` environment variable.
cluster_dir : str
The full path to a cluster directory. This is useful if profiles
are not being used.
ipython_dir : str
The location of the ipython_dir if different from the default.
This is used if the cluster directory is being found by profile.
auto_create : bool
Automatically create the cluster directory it is dones't exist.
This will usually only make sense if using a local cluster
(default=False).
auto_stop : bool
Automatically stop the cluster when this instance is garbage
collected (default=True). This is useful if you want the cluster
to live beyond your current process. There is also an instance
attribute ``auto_stop`` to change this behavior.
"""
self.async_cluster = AsyncCluster(
profile, cluster_dir, ipython_dir, auto_create, auto_stop
)
self.cluster_dir_obj = self.async_cluster.cluster_dir_obj
self.client_connector = None
def _set_auto_stop(self, value):
self.async_cluster.auto_stop = value
def _get_auto_stop(self):
return self.async_cluster.auto_stop
auto_stop = property(_get_auto_stop, _set_auto_stop)
@property
def location(self):
return self.async_cluster.location
@property
def running(self):
return self.async_cluster.running
def start(self, n=2):
"""Start the IPython cluster with n engines.
Parameters
----------
n : int
The number of engine to start.
"""
return blockingCallFromThread(self.async_cluster.start, n)
def stop(self):
"""Stop the IPython cluster if it is running."""
return blockingCallFromThread(self.async_cluster.stop)
def get_multiengine_client(self, delay=DELAY, max_tries=MAX_TRIES):
"""Get the multiengine client for the running cluster.
This will try to attempt to the controller multiple times. If this
fails altogether, try looking at the following:
* Make sure the controller is starting properly by looking at its
log files.
* Make sure the controller is writing its FURL file in the location
expected by the client.
* Make sure a firewall on the controller's host is not blocking the
client from connecting.
Parameters
----------
delay : float
The initial delay between re-connection attempts. Susequent delays
get longer according to ``delay[i] = 1.5*delay[i-1]``.
max_tries : int
The max number of re-connection attempts.
"""
if self.client_connector is None:
self.client_connector = ClientConnector()
return self.client_connector.get_multiengine_client(
cluster_dir=self.cluster_dir_obj.location,
delay=delay, max_tries=max_tries
)
def get_task_client(self, delay=DELAY, max_tries=MAX_TRIES):
"""Get the task client for the running cluster.
This will try to attempt to the controller multiple times. If this
fails altogether, try looking at the following:
* Make sure the controller is starting properly by looking at its
log files.
* Make sure the controller is writing its FURL file in the location
expected by the client.
* Make sure a firewall on the controller's host is not blocking the
client from connecting.
Parameters
----------
delay : float
The initial delay between re-connection attempts. Susequent delays
get longer according to ``delay[i] = 1.5*delay[i-1]``.
max_tries : int
The max number of re-connection attempts.
"""
if self.client_connector is None:
self.client_connector = ClientConnector()
return self.client_connector.get_task_client(
cluster_dir=self.cluster_dir_obj.location,
delay=delay, max_tries=max_tries
)
def __repr__(self):
s = "<Cluster(running=%r, location=%s)" % (self.running, self.location)
return s
def get_logs_by_name(self, name='ipcluter'):
"""Get a dict of logs by process name (ipcluster, ipengine, etc.)"""
return self.async_cluster.get_logs_by_name(name)
def get_ipengine_logs(self):
"""Get a dict of logs for all engines in this cluster."""
return self.async_cluster.get_ipengine_logs()
def get_ipcontroller_logs(self):
"""Get a dict of logs for the controller in this cluster."""
return self.async_cluster.get_ipcontroller_logs()
def get_ipcluster_logs(self):
"""Get a dict of the ipcluster logs for this cluster."""
return self.async_cluster.get_ipcluster_logs()
def get_logs(self):
"""Get a dict of all logs for this cluster."""
return self.async_cluster.get_logs()
def _print_logs(self, logs):
for k, v in logs.iteritems():
print "==================================="
print "Logfile: %s" % k
print "==================================="
print v
print
def print_ipengine_logs(self):
"""Print the ipengine logs for this cluster to stdout."""
self._print_logs(self.get_ipengine_logs())
def print_ipcontroller_logs(self):
"""Print the ipcontroller logs for this cluster to stdout."""
self._print_logs(self.get_ipcontroller_logs())
def print_ipcluster_logs(self):
"""Print the ipcluster logs for this cluster to stdout."""
self._print_logs(self.get_ipcluster_logs())
def print_logs(self):
"""Print all the logs for this cluster to stdout."""
self._print_logs(self.get_logs())