-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
139 additions
and
97 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
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 +1 @@ | ||
from .dummy import DummyScheduler | ||
from .airflow import AirflowScheduler |
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,12 +1,11 @@ | ||
# -*- coding: utf-8 -*- | ||
import configparser | ||
import json | ||
import os | ||
import os.path | ||
import jinja2 | ||
from sqlalchemy import create_engine | ||
from base64 import b64encode | ||
from random import randint, choice | ||
from random import choice | ||
from sqlalchemy import create_engine | ||
from .base import BaseScheduler, TIMING_MAP | ||
|
||
with open(os.path.abspath(os.path.join(os.path.dirname(__file__), 'paperboy.airflow.py')), 'r') as fp: | ||
|
@@ -19,10 +18,15 @@ | |
LIMIT 20; | ||
''' | ||
|
||
####################################### | ||
# FIXME merge with dummy when # | ||
# airflow has better python3 support # | ||
####################################### | ||
|
||
|
||
class DummyScheduler(BaseScheduler): | ||
class AirflowScheduler(BaseScheduler): | ||
def __init__(self, *args, **kwargs): | ||
super(DummyScheduler, self).__init__(*args, **kwargs) | ||
super(AirflowScheduler, self).__init__(*args, **kwargs) | ||
cp = configparser.ConfigParser() | ||
cp.read(self.config.scheduler.config) | ||
try: | ||
|
@@ -36,22 +40,25 @@ def __init__(self, *args, **kwargs): | |
def status(self, user, params, session, *args, **kwargs): | ||
type = params.get('type', '') | ||
if not self.sql_conn: | ||
gen = AirflowScheduler.fakequery(self.engine) | ||
if type == 'jobs': | ||
return self.statusgeneralfake()['jobs'] | ||
return gen['jobs'] | ||
elif type == 'reports': | ||
return self.statusgeneralfake()['reports'] | ||
return gen['reports'] | ||
else: | ||
return self.statusgeneralfake() | ||
return gen | ||
gen = AirflowScheduler.query(self.engine) | ||
if type == 'jobs': | ||
return self.statusgeneral()['jobs'] | ||
return gen['jobs'] | ||
elif type == 'reports': | ||
return self.statusgeneral()['reports'] | ||
return gen['reports'] | ||
else: | ||
return self.statusgeneral() | ||
return gen | ||
|
||
def statusgeneral(self): | ||
@staticmethod | ||
def query(engine): | ||
ret = {'jobs': [], 'reports': []} | ||
with self.engine.begin() as conn: | ||
with engine.begin() as conn: | ||
res = conn.execute(QUERY) | ||
for i, item in enumerate(res): | ||
ret['jobs'].append( | ||
|
@@ -86,7 +93,8 @@ def statusgeneral(self): | |
) | ||
return ret | ||
|
||
def statusgeneralfake(self): | ||
@staticmethod | ||
def fakequery(engine): | ||
ret = {'jobs': [], 'reports': []} | ||
for i in range(10): | ||
ret['jobs'].append( | ||
|
@@ -110,7 +118,8 @@ def statusgeneralfake(self): | |
) | ||
return ret | ||
|
||
def schedule(self, user, notebook, job, reports, *args, **kwargs): | ||
@staticmethod | ||
def schedule_airflow(config, user, notebook, job, reports, *args, **kwargs): | ||
owner = user.name | ||
start_date = job.meta.start_time.strftime('%m/%d/%Y %H:%M:%S') | ||
email = '[email protected]' | ||
|
@@ -125,8 +134,11 @@ def schedule(self, user, notebook, job, reports, *args, **kwargs): | |
email=email, | ||
job_json=job_json, | ||
report_json=report_json, | ||
output_config=json.dumps(self.config.output.to_json()) | ||
output_config=json.dumps(config.output.to_json()) | ||
) | ||
with open(os.path.join(self.config.scheduler.dagbag, job.id + '.py'), 'w') as fp: | ||
with open(os.path.join(config.scheduler.dagbag, job.id + '.py'), 'w') as fp: | ||
fp.write(tpl) | ||
return tpl | ||
|
||
def schedule(self, user, notebook, job, reports, *args, **kwargs): | ||
AirflowScheduler.schedule_airflow(self.config, user, notebook, job, reports, *args, **kwargs) |
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 @@ | ||
from .operators import JobOperator, JobCleanupOperator, ReportOperator, PapermillOperator, NBConvertOperator, ReportPostOperator |
49 changes: 0 additions & 49 deletions
49
paperboy/scheduler/_airflow.py → .../scheduler/airflow_operators/operators.py
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,55 +1,6 @@ | ||
import json | ||
import os | ||
import os.path | ||
import jinja2 | ||
from base64 import b64encode | ||
from airflow.models import BaseOperator | ||
from airflow.utils.decorators import apply_defaults | ||
from paperboy.utils import name_to_class | ||
from .base import BaseScheduler, TIMING_MAP | ||
|
||
with open(os.path.abspath(os.path.join(os.path.dirname(__file__), 'paperboy.airflow.py')), 'r') as fp: | ||
TEMPLATE = fp.read() | ||
|
||
####################################### | ||
# FIXME merge with dummy when # | ||
# airflow has better python3 support # | ||
####################################### | ||
|
||
|
||
class AirflowScheduler(BaseScheduler): | ||
def status(self, user, params, session, *args, **kwargs): | ||
type = params.get('type', '') | ||
if type == 'notebooks': | ||
return [] | ||
elif type == 'jobs': | ||
return [] | ||
elif type == 'reports': | ||
return [] | ||
else: | ||
return {'notebook': [], 'jobs': [], 'reports': []} | ||
|
||
def schedule(self, user, notebook, job, reports, *args, **kwargs): | ||
owner = user.name | ||
start_date = job.meta.start_time.strftime('%m/%d/%Y %H:%M:%S') | ||
email = '[email protected]' | ||
job_json = b64encode(json.dumps(job.to_json(True)).encode('utf-8')) | ||
report_json = b64encode(json.dumps([r.to_json() for r in reports]).encode('utf-8')) | ||
interval = TIMING_MAP.get(job.meta.interval) | ||
|
||
tpl = jinja2.Template(TEMPLATE).render( | ||
owner=owner, | ||
start_date=start_date, | ||
interval=interval, | ||
email=email, | ||
job_json=job_json, | ||
report_json=report_json, | ||
output_type=self.config.output_type, | ||
output_dir=self.config.output_dir, | ||
) | ||
with open(os.path.join(self.config.airflow_dagbag, job.id + '.py'), 'w') as fp: | ||
fp.write(tpl) | ||
return tpl | ||
|
||
|
||
class JobOperator(BaseOperator): | ||
|
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,16 @@ | ||
import requests | ||
from .base import BaseScheduler | ||
|
||
|
||
class RemoteScheduler(BaseScheduler): | ||
def __init__(self, *args, **kwargs): | ||
super(RemoteScheduler, self).__init__(*args, **kwargs) | ||
|
||
def status(self, user, params, session, *args, **kwargs): | ||
# FIXME async/celery | ||
return requests.get(self.config.scheduler.status_url, params=params).json() | ||
|
||
def schedule(self, user, notebook, job, reports, *args, **kwargs): | ||
# FIXME async/celery | ||
params = {'user': user.to_json(), 'notebook': notebook.to_json(), 'job': job.to_json(), 'reports': [r.to_json() for r in reports]} | ||
return requests.post(self.config.scheduler.schedule_url, params=params).json() |
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