<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>python Archives - Big Data Processing</title>
	<atom:link href="https://bigdataproc.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>https://bigdataproc.com/tag/python/</link>
	<description>Big Data Solution for GCP, AWS, Azure and on-prem</description>
	<lastBuildDate>Wed, 24 Mar 2021 17:14:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.2</generator>
	<item>
		<title>Monkey patching to decorate your library function</title>
		<link>https://bigdataproc.com/monkey-patching-to-decorate-your-library-function/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=monkey-patching-to-decorate-your-library-function</link>
		
		<dc:creator><![CDATA[Gaurang]]></dc:creator>
		<pubDate>Wed, 24 Mar 2021 17:08:11 +0000</pubDate>
				<category><![CDATA[Airflow]]></category>
		<category><![CDATA[airflow]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">http://allabouthadoop.net/?p=241</guid>

					<description><![CDATA[<p>We are using Airflow as our scheduler/orchestrator for all AWS data related jobs. We use SSHOperator to run our jobs. Our support team complained that&#8230;</p>
<div class="more-link-wrapper"><a class="more-link" href="https://bigdataproc.com/monkey-patching-to-decorate-your-library-function/">Continue reading<span class="screen-reader-text">Monkey patching to decorate your library function</span></a></div>
<p>The post <a rel="nofollow" href="https://bigdataproc.com/monkey-patching-to-decorate-your-library-function/">Monkey patching to decorate your library function</a> appeared first on <a rel="nofollow" href="https://bigdataproc.com">Big Data Processing </a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>We are using Airflow as our scheduler/orchestrator for all AWS data related jobs.  We use SSHOperator to run our jobs. Our support team complained that sometimes job fails while making a connection, and re-running them just works fine.  This was creating a lot of noise. And as there is no re-try mechanism in operator,  we can&#8217;t configure it to auto-retry.  </p>



<p>I realized that SSHOperator class calls a function from SSHHook class which actually makes the ssh connection to given details.  </p>



<h3 class="wp-block-heading"><strong>Problem </strong><br></h3>



<p>There are lots of Dags (classes) which refers to function. How to add retry mechanism without modifying each and every class </p>



<h3 class="wp-block-heading"><strong>Solution</strong></h3>



<p>Monkey patching is first thing which came to my mind. However,  when I applied the monkey patching it created a recursion, as I had to call function again for retry. It took a while to fix this issue. However, finally I was able to fix it by creating another reference of original function is same class I am patching. </p>



<p><strong>plugins/hooks/ssh_hook_with_retry.py</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># -*- coding: utf-8 -*-

# Author: Gaurang Shah
#
from airflow.contrib.hooks.ssh_hook import SSHHook
import time 

max_retry = 3
retry = 0


def get_conn_with_retry(self):
    try:
		# call the original function 
        return SSHHook.original_func(self)
    except Exception as e:
        global retry
        if retry &lt; max_retry:
            retry += 1
            print("tried %s times, failed to connect retrying again" %retry)
			time.sleep(60)
			# call the patched function which inturn calls get_conn_with_retry function 
            self.get_conn()
        else:
            raise e</pre>



<p><strong>plugins/hooks/__init__.py</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from airflow.contrib.hooks import ssh_hook

# add new reference to original function to avoid recursion 
ssh_hook.SSHHook.original_func = ssh_hook.SSHHook.get_conn
from hooks.ssh_hook_with_retry import get_conn_with_retry

# path get_conn function with our function 
ssh_hook.SSHHook.get_conn = get_conn_with_retry

# when someone calls SSHHook.get_conn then ssh_hook_with_retry.get_conn_with_retry will be called
# and to call orignal get_conn we will have to call  SSHHook.original_func </pre>
<p>The post <a rel="nofollow" href="https://bigdataproc.com/monkey-patching-to-decorate-your-library-function/">Monkey patching to decorate your library function</a> appeared first on <a rel="nofollow" href="https://bigdataproc.com">Big Data Processing </a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
