forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:mistercrunch/Airflow
- Loading branch information
Showing
16 changed files
with
235 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
from base_executor import LocalExecutor | ||
from base_executor import SequentialExecutor | ||
from celery_executor import CeleryExecutor | ||
from local_executor import LocalExecutor | ||
from sequential_executor import SequentialExecutor | ||
|
||
# DEFAULT_EXECUTOR = CeleryExecutor() | ||
# DEFAULT_EXECUTOR = LocalExecutor() | ||
DEFAULT_EXECUTOR = SequentialExecutor() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import multiprocessing | ||
import subprocess | ||
import time | ||
|
||
from airflow.executors.base_executor import BaseExecutor | ||
from airflow import settings | ||
from airflow.utils import State | ||
|
||
from celery_worker import execute_command | ||
|
||
class CeleryExecutor(BaseExecutor): | ||
""" Submits the task to RabbitMQ, which is picked up and executed by a bunch | ||
of worker processes """ | ||
def __init__(self, parallelism=1): | ||
super(CeleryExecutor, self).__init__() | ||
self.parallelism = parallelism | ||
|
||
def start(self): | ||
self.queue = multiprocessing.JoinableQueue() | ||
self.result_queue = multiprocessing.Queue() | ||
self.workers = [ CelerySubmitter(self.queue, self.result_queue) for i in xrange(self.parallelism) ] | ||
|
||
for w in self.workers: | ||
w.start() | ||
|
||
def execute_async(self, key, command): | ||
self.queue.put((key, command)) | ||
|
||
def heartbeat(self): | ||
while not self.result_queue.empty(): | ||
results = self.result_queue.get() | ||
self.change_state(*results) | ||
|
||
def end(self): | ||
# Sending poison pill to all worker | ||
[self.queue.put(None) for w in self.workers] | ||
self.queue.join() | ||
|
||
|
||
class CelerySubmitter(multiprocessing.Process): | ||
|
||
def __init__(self, task_queue, result_queue): | ||
multiprocessing.Process.__init__(self) | ||
self.task_queue = task_queue | ||
self.result_queue = result_queue | ||
|
||
def run(self): | ||
while True: | ||
key, command = self.task_queue.get() | ||
if command == None: | ||
# Received poison pill, no more tasks to run | ||
self.task_queue.task_done() | ||
break | ||
BASE_FOLDER = settings.BASE_FOLDER | ||
command = ( | ||
"exec bash -c '" | ||
"cd $AIRFLOW_HOME;\n" + | ||
"source init.sh;\n" + | ||
command + | ||
"'" | ||
).format(**locals()) | ||
|
||
try: | ||
res = execute_command.delay(command) | ||
result = res.get() | ||
except Exception as e: | ||
self.result_queue.put((key, State.FAILED)) | ||
raise e | ||
self.result_queue.put((key, State.SUCCESS)) | ||
self.task_queue.task_done() | ||
time.sleep(1) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import subprocess | ||
import time | ||
import logging | ||
from celery import Celery | ||
|
||
from airflow import settings | ||
|
||
# to start the celery worker, run the command: | ||
# "celery -A airflow.executors.celery_worker worker --loglevel=info" | ||
|
||
# app = Celery('airflow.executors.celery_worker', backend='amqp', broker='amqp://') | ||
app = Celery(settings.CELERY_APP_NAME, backend=settings.CELERY_BROKER, broker=settings.CELERY_RESULTS_BACKEND) | ||
|
||
@app.task (name='airflow.executors.celery_worker.execute_command') | ||
def execute_command(command): | ||
logging.info("Executing command in Celery " + command) | ||
try: | ||
subprocess.Popen(command, shell=True).wait() | ||
except Exception as e: | ||
raise e | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import logging | ||
import multiprocessing | ||
import subprocess | ||
import time | ||
|
||
from airflow import settings | ||
from airflow.utils import State | ||
|
||
from airflow.executors.base_executor import BaseExecutor | ||
|
||
class LocalWorker(multiprocessing.Process): | ||
|
||
def __init__(self, task_queue, result_queue): | ||
multiprocessing.Process.__init__(self) | ||
self.task_queue = task_queue | ||
self.result_queue = result_queue | ||
|
||
def run(self): | ||
proc_name = self.name | ||
while True: | ||
key, command = self.task_queue.get() | ||
if key is None: | ||
# Received poison pill, no more tasks to run | ||
self.task_queue.task_done() | ||
break | ||
BASE_FOLDER = settings.BASE_FOLDER | ||
print command | ||
command = ( | ||
"exec bash -c '" | ||
"cd $AIRFLOW_HOME;\n" + | ||
"source init.sh;\n" + | ||
command + | ||
"'" | ||
).format(**locals()) | ||
try: | ||
sp = subprocess.Popen(command, shell=True).wait() | ||
except Exception as e: | ||
self.result_queue.put((key, State.FAILED)) | ||
raise e | ||
self.result_queue.put((key, State.SUCCESS)) | ||
self.task_queue.task_done() | ||
time.sleep(1) | ||
|
||
|
||
class LocalExecutor(BaseExecutor): | ||
|
||
def __init__(self, parallelism=8): | ||
super(LocalExecutor, self).__init__() | ||
self.parallelism = parallelism | ||
|
||
def start(self): | ||
self.queue = multiprocessing.JoinableQueue() | ||
self.result_queue = multiprocessing.Queue() | ||
self.workers = [ | ||
LocalWorker(self.queue, self.result_queue) | ||
for i in xrange(self.parallelism) | ||
] | ||
|
||
for w in self.workers: | ||
w.start() | ||
|
||
def execute_async(self, key, command): | ||
self.queue.put((key, command)) | ||
|
||
def heartbeat(self): | ||
while not self.result_queue.empty(): | ||
results = self.result_queue.get() | ||
self.change_state(*results) | ||
|
||
def end(self): | ||
# Sending poison pill to all worker | ||
[self.queue.put((None, None)) for w in self.workers] | ||
# Wait for commands to finish | ||
self.queue.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import subprocess | ||
|
||
from airflow.utils import State | ||
|
||
from airflow.executors.base_executor import BaseExecutor | ||
|
||
class SequentialExecutor(BaseExecutor): | ||
""" | ||
Will only run one task instance at a time, can be used for debugging. | ||
""" | ||
def __init__(self): | ||
super(SequentialExecutor, self).__init__() | ||
self.commands_to_run = [] | ||
|
||
def queue_command(self, key, command): | ||
self.commands_to_run.append((key, command,)) | ||
|
||
def heartbeat(self): | ||
for key, command in self.commands_to_run: | ||
try: | ||
sp = subprocess.Popen(command, shell=True).wait() | ||
except Exception as e: | ||
self.change_state(key, State.FAILED) | ||
raise e | ||
self.change_state(key, State.SUCCESS) | ||
self.commands_to_run = [] | ||
|
||
def end(self): | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from random import random | ||
from datetime import datetime | ||
import time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from airflow import settings | ||
from airflow.hooks.hive_hook import HiveHook | ||
|
||
def max_partition( | ||
table, schema="default", hive_dbid=settings.HIVE_DEFAULT_DBID): | ||
hh = HiveHook(hive_dbid=hive_dbid) | ||
return hh.max_partition(schema=schema, table=table) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.