Adding --local option to run command

This commit is contained in:
Maxime Beauchemin 2015-01-21 21:54:21 -08:00
Родитель 100198983e
Коммит 4a12bd5838
4 изменённых файлов: 24 добавлений и 13 удалений

Просмотреть файл

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from airflow.configuration import conf from airflow.configuration import conf
from airflow.executors import DEFAULT_EXECUTOR
from airflow import settings from airflow import settings
from airflow import utils from airflow import utils
from airflow import jobs from airflow import jobs
@ -10,7 +11,6 @@ import dateutil.parser
from datetime import datetime from datetime import datetime
import logging import logging
import os import os
import signal
import sys import sys
import argparse import argparse
@ -67,7 +67,6 @@ def run(args):
logging.basicConfig( logging.basicConfig(
filename=filename, level=logging.INFO, filename=filename, level=logging.INFO,
format=settings.LOG_FORMAT) format=settings.LOG_FORMAT)
print("Logging into: " + filename)
if not args.pickle: if not args.pickle:
dagbag = DagBag(args.subdir) dagbag = DagBag(args.subdir)
@ -85,13 +84,19 @@ def run(args):
# TODO: add run_local and fire it with the right executor from run # TODO: add run_local and fire it with the right executor from run
ti = TaskInstance(task, args.execution_date) ti = TaskInstance(task, args.execution_date)
if args.local:
run_job = jobs.LocalTaskJob( print("Logging into: " + filename)
task_instance=ti, run_job = jobs.LocalTaskJob(
mark_success=args.mark_success, task_instance=ti,
force=args.force, mark_success=args.mark_success,
ignore_dependencies=args.ignore_dependencies) force=args.force,
run_job.run() ignore_dependencies=args.ignore_dependencies)
run_job.run()
else:
executor = DEFAULT_EXECUTOR
executor.start()
executor.queue_command(ti.key, ti.command())
executor.end()
def list_dags(args): def list_dags(args):
@ -344,6 +349,10 @@ if __name__ == '__main__':
"-f", "--force", "-f", "--force",
help="Force a run regardless or previous success", help="Force a run regardless or previous success",
action="store_true") action="store_true")
parser_run.add_argument(
"-l", "--local",
help="Runs the task locally, don't use the executor",
action="store_true")
parser_run.add_argument( parser_run.add_argument(
"-i", "--ignore_dependencies", "-i", "--ignore_dependencies",
help="Ignore upstream and depends_on_past dependencies", help="Ignore upstream and depends_on_past dependencies",

Просмотреть файл

@ -23,7 +23,8 @@ class SequentialExecutor(BaseExecutor):
def heartbeat(self): def heartbeat(self):
for key, command in self.commands_to_run: for key, command in self.commands_to_run:
try: try:
sp = subprocess.Popen(command, shell=True).wait() sp = subprocess.Popen(command, shell=True)
sp.wait()
except Exception as e: except Exception as e:
self.change_state(key, State.FAILED) self.change_state(key, State.FAILED)
raise e raise e
@ -31,4 +32,4 @@ class SequentialExecutor(BaseExecutor):
self.commands_to_run = [] self.commands_to_run = []
def end(self): def end(self):
pass self.heartbeat()

Просмотреть файл

@ -394,7 +394,6 @@ class LocalTaskJob(BaseJob):
}) })
self.thr = thr self.thr = thr
thr.start() thr.start()
while thr.is_alive(): while thr.is_alive():
self.heartbeat() self.heartbeat()

Просмотреть файл

@ -256,18 +256,19 @@ class TaskInstance(Base):
mark_success=False, mark_success=False,
ignore_dependencies=False, ignore_dependencies=False,
force=False, force=False,
local=True,
pickle_id=None): pickle_id=None):
""" """
Returns a command that can be executed anywhere where airflow is Returns a command that can be executed anywhere where airflow is
installed. This command is part of the message sent to executors by installed. This command is part of the message sent to executors by
the orchestrator. the orchestrator.
""" """
iso = self.execution_date.isoformat() iso = self.execution_date.isoformat()
mark_success = "--mark_success" if mark_success else "" mark_success = "--mark_success" if mark_success else ""
pickle = "--pickle {0}".format(pickle_id) if pickle_id else "" pickle = "--pickle {0}".format(pickle_id) if pickle_id else ""
ignore_dependencies = "-i" if ignore_dependencies else "" ignore_dependencies = "-i" if ignore_dependencies else ""
force = "--force" if force else "" force = "--force" if force else ""
local = "--local" if local else ""
subdir = "" subdir = ""
if not pickle and self.task.dag and self.task.dag.full_filepath: if not pickle and self.task.dag and self.task.dag.full_filepath:
subdir = "-sd {0}".format(self.task.dag.full_filepath) subdir = "-sd {0}".format(self.task.dag.full_filepath)
@ -276,6 +277,7 @@ class TaskInstance(Base):
"{self.dag_id} {self.task_id} {iso} " "{self.dag_id} {self.task_id} {iso} "
"{mark_success} " "{mark_success} "
"{pickle} " "{pickle} "
"{local} "
"{ignore_dependencies} " "{ignore_dependencies} "
"{force} " "{force} "
"{subdir} " "{subdir} "