Categories
Linux Python

Deploying a Flask app on Ubuntu: uWSGI, nginx and Upstart

Let’s get straight to the point. I have a Flask app that I want to run on Ubuntu. I shall use uwsgi and nginx. Upstart will be used to monitor the process and restart it if it’s killed or dies an untimely death.

Running the uWSGI app with nginx

It’s very easy to run Flask apps with uwsgi, just cd into the directory where your main app file exists and pass the uwsgi module name. If your main app file is called “main.py” and if your Flask app instance is called “app” inside main.py, then your uWSGI module name would be “main:app” from the directory where main.py resides.

Now, we need to configure nginx:

Here, we first need to define an upstream. We define the “flaskapp” upstream for our app. Then inside the location block, we use it as the source of our uwsgi_pass directive. It would tell nginx to connect to the uwsgi server we defined and serve as a bridge between the internet and the flask uwsgi app.

Upstarting the uWSGI process

Now, what happens if our uWSGI process dies? nginx would no longer be able to connect to the app and throw a “bad gateway” error. So we need to make sure that if the process dies from an error or somehow gets killed, it is restarted automatically. There are a few tools which can do this. Supervisord is the most popular one, Circus is also very nice. However, I like to use Upstart for cases like this. Upstart comes with Ubuntu and used by the OS for many of it’s own services. It’s easy to use, already available, reliable and most importantly – it allows me to create a service too!

To create our upstart config, we cd into “/etc/init” and create a new file named “flaskapp.conf”. Then we put the following contents in there:

Here, we’re telling Upstart to launch our app on startup and respawn whenever the process dies. We put our commands in a script block. We have defined our service, now time to fire it up. If you have any uwsgi process run with the same config, first kill them. Then run this command to start the service:

Right, you can use the usual “start|stop|restart|status” service commands with it. The service name is derived from the file name we used in “/etc/init”. The log of the process would be found in “/var/log/upstart/flaskapp.log”.

From Upstart to Systemd

Ubuntu is moving away from Upstart to systemd. Most of my apps use Ubuntu 14.04 LTS so I can still use Upstart fine. However, if your version of Ubuntu doesn’t ship Upstart, you can use systemd for similar tasks. Here’s the link: systemd for upstart users

Categories
Python

Iron Worker: Processing SendGrid Inbound Parse Data

If you use SendGrid, you know that they provide this awesome Inbound Parse APIs which allows users to catch emails sent to their domains. When there’s a new email, Sendgrid would make a POST request to your webhook endpoint. On the other hand, I love Iron Worker webhooks. When you POST data to their webhooks, the data is passed to a worker and processed in queue. It helps keep things simpler since I don’t need to setup a separate web server for accepting the data; however, keep in mind that I do make sure to use data protection services like the Venyu cybersecurity services at all times.

By now, you can probably guess my intention. I would like to make Sendgrid POST incoming emails to a Iron Worker webhook and then grab the data. But there’s one catch – Iron.io stores the incoming data in a text file and expect us to parse the text file to collect our data. Since very often the data is JSON, most client libraries take the payload, decode it from JSON and provide us with native data structures. I use Python, so I usually get Python dictionary. However, in case of Sendgrid Inbound APIs, the data is not JSON encoded. It’s simple multi-part form data. So the Python client I use, it fails to process the data and I don’t get anything for the payload.

So I went ahead and modified the payload processing part from the client library. Now this is what I have for getting the payload:

Now, we have the payload. But it’s still just plain text. We need to process it to get the different POST fields. Luckily, we can do that easily with the cgi library. This is what I use:

Finally, now this is what the worker looks like:

Categories
Bangla

কোথায়, কিভাবে খুজঁবেন রিমোট জব?

প্রচলিত ফ্রী-ল্যান্স মার্কেটপ্লেইস (আপওয়ার্ক কিংবা ফ্রী-ল্যান্সার ডট কম) এর বাইরেও প্রচুর রিমোট জব পাওয়া যায় যেগুলোর কথা হয়তো আমরা জানি না । আজকে আমি আমার পরিচিত কিছু সোর্স উল্লেখ করবো ।

শুরুতেই কিছু কথা বলে রাখা জরুরী:

  • যেহেতু আমি সাধারনত সফটওয়্যার ডেভেলপমেন্ট নিয়ে কাজ করি তাই এই সাইটগুলোর বেশীরভাগই ডেভেলপার ফোকাসড ।
  • এই সাইটগুলোর কয়েকটি রিমোট ফোকাসড, কতগুলোতে অন-সাইট এবং রিমোট দুই ধরনের জবই আছে । যেসব সাইটে দুই ধরনের জবই থাকে সেগুলোতে রিমোট জব গুলো ফিল্টার করে নিন ।
  • এই লিস্টটি সময়ের সাথে সাথে আপডেইট করা হবে । আপনাদের সাজেশন থাকলে কমেন্ট করুন, যোগ করে দিবো ।
  • সফটওয়্যার ডেভেলপমেন্ট ছাড়াও অন্যান্য ক্যাটাগরির সাইটও আমি এখানে যোগ করতে আগ্রহী, আপনার জানা থাকলে কমেন্ট করুন।

জেনেরিক জব বোর্ড

এই সাইটগুলোতে পিএইচপি, পাইথন, জাভাস্ক্রীপ্ট, সিস্টেম এ্যাডমিন, মোবাইল এ্যাপ্লিকেশন ডেভেলপার – মোটামোটি সব ধরনের স্কিলসেটের জবই পাওয়া যায় ।

স্পেশালাইজড জব বোর্ড

এই লিস্টের সাইটগুলো কোন বিশেষ টেকনোলজি ফোকাসড ।

  • Django Jobs | Django Gigs | Django Jobbers | (আরেকটা) Django Jobs – জ্যাঙ্গোর জন্য বেশ কয়েকটা জব সাইটে নজর রাখতে হবে আপনাকে ।
  • Larajobs – নাম শুনেই আন্দাজ করতে পারছেন হয়তো, এখানে শুধু লারাভেল স্পেসিফিক জব পাওয়া যায় ।
  • WordPress Jobs – এখানে পাবেন ওয়ার্ডপ্রেস এর জন্য ডিজাইন ও ডেভেলপমেন্ট এর কাজ
  • Nodejobs – নোডজেএস রিলেটেড জব খুজঁতে চাইলে ঢু মারুন এখানে ।
  • Golang Jobs – গুগলের গো ল্যাঙ্গুয়েজ নিয়ে কাজ করতে চাইলে এই ওয়েব সাইটটি হতে পারে ভালো রিসোর্স ।

কমিউনিটি, ফোরাম, আইআরসি

  • চোখ রাখুন রেডিটে – Remote Python | Remote Javascript | For Hire
  • হ্যাকার নিউজের জব লিস্টিং ছাড়াও প্রতিমাসে একটা মাসিক জব থ্রেডও পাবলিশ করা হয়, যেমন: April, 2015
  • আপনি যেই স্কিলসেট নিয়ে গর্বিত, সেই সব স্কিল সংশ্লিষ্ট কমিউনিটি গুলোতে খোজঁ খবর রাখুন । ফেইসবুক এবং লিংকইন গ্রুপগুলোতে মাঝে মাঝেই ঘুরে আসুন ।
  • আইআরসি এবং স্ল্যাকেও মিলবে জবের খোজঁ ।

কিভাবে এ্যাপ্লাই করবেন?

  • প্রথমেই আপনার নিজের সিভি আপডেইট করে নিন । সুন্দর করে সিভি প্রস্তুত করুন । শত শত এ্যাপ্লিকেশনের ভিতর থেকে আপনাকে যেন আলাদা করা যায় এমন কিছু থাকতে হবে আপনার সিভিতে । গুগল ঘেটে আকর্ষনীয় সিভি কিভাবে তৈরি করা যায় সেটা নিয়ে পড়াশুনা করুন । সময় দিন, পরিশ্রম করে সিভি বানান । আপনার স্কিল সেট সহজ, সাবলীল ভাষায়, সংক্ষিপ্ত আকারে টু-দ্যা-পয়েন্ট তুলে ধরুন । গতানুগতিক সিভির ফরম্যাট থেকে বাইরে আসুন, নতুন কিছু করার চেষ্টা করুন ।
  • অন্যদের সিভি পড়ুন, তাদের সিভির সাথে তাদের ব্যক্তিত্ব মিলিয়ে দেখুন, দেখবেন নিজের সিভির উপস্থাপনার ব্যাপারে মাথায় নতুন নতুন আইডিয়া আসছে ।
  • ওয়ার্ড ডকুমেন্ট হিসেবে সিভি পাঠানো খুবই আনপ্রফেশনাল । অবশ্যই পিডিএফ পাঠানো উচিৎ! আরও ভালো হয় পিডিএফ এর সাথে যদি একটা অনলাইন ভার্সন পাঠানো যায় । যেমন আমি সব সময় এ্যাপ্লাই করার সময় ইমেইল বা কাভার লেটারে এই লিংকটি দেই: http://masnun.me
  • কাভার লেটার বা ইমেইলটা প্রফেশনাল হওয়া খুবই জরুরী । এটাই আপনার ফার্স্ট ইমপ্রেশন তৈরি করবে । কথায় বলে – “You will never get the second chance to make a first impression.” । জব পোস্টিং টা পড়ুন, ভালো করে এ্যনালাইজ করুন, চিন্তা করুন এই জবে আপনি কেন পারফেক্ট, জব রিকোয়্যারমেন্টগুলোর কোন গুলোয় আপনার স্ট্রং পয়েন্ট আছে সেগুলো আইডেন্টিফাই করে সেগুলোর উপর জোর দিন । যে জিনিসগুলো পারেন না, সেগুলো শেখার জন্য আগ্রহ প্রকাশ করুন ।
  • আপনার ব্লগ, গিটহাব, পোর্টফোলিও – এগুলোর উপর জোর দিন । এগুলো যেন সহজে নজরে পড়ে সেভাবে উপস্থাপন করুন । আর হ্যা, অবশ্যই সময় করে নিজের ব্লগে লেখালেখি করুন (ইংরেজীতে অবশ্যই), গিটহাবে নিজের কিছু ওপেন সোর্স প্রজেক্ট রাখুন, ডিজাইনাররা পোর্টফলিও রেডী করুন । আমি গিটহাব এবং আমার ব্লগ থেকে ৩-৪ টি জবের অফার পেয়েছি । সুতরাং এই জিনিসগুলো খুবই জরুরী ।
  • আপনার কাভার লেটার, ইমেইল বা সিভির পিছনে কেউই দুই মিনিটের বেশী সময় দিবে না শুরুতে । তাই প্রাইমারি স্ক্রীনিং এ টিকতে চাইলে সংক্ষেপে সুন্দর করে উপস্থাপন করুন সব কিছু ।
  • রংঢং কিংবা আকাঁবাকা ফন্ট ব্যবহার করেছেন তো মরেছেন । আপনার ইমেইল এডিটরের ডিফল্ট ফন্ট ব্যবহার করার চেষ্টা করুন । অথবা রুচিশীল কোন প্রফেশনাল ফন্ট ব্যবহার করুন । প্রয়োজন হলে বোল্ড হরফ ব্যবহার করতে পারেন তবে সেটা অবশ্যই পরিমিত মাত্রায় ।
  • নেগেটিভিটি পরিহার করুন । নেগেটিভ কোন কথা না বলে ওভার অল পজিটিভিটি মেইনটেইন করুন । যেমন ধরুন: “I am not available full time right now” এ কথা বলার চাইতে “Part time would work better for me now, I would definitely look forward to working full time in the future” বললে একটা পজিটিভ টোন কাজ করে । পজিটিভিটিটা জবের ক্ষেত্রে খুবই গুরুত্বপূর্ন ।
  • রিসেন্টলি পাবলিশ হওয়া জব পোস্টগুলোর উপর বেশী ফোকাস করুন ।
  • ইন্টারভিউতে ডাক না পেলে হতাশ হবেন না, চেষ্টা চালিয়ে যান । অধ্যাবসায়ের কোন বিকল্প নেই!