def test_parse_execution_date(self): execution_date_str_wo_ms = '2017-11-02 00:00:00' execution_date_str_w_ms = '2017-11-05 16:18:30.989729' bad_execution_date_str = '2017-11-06TXX:00:00Z' self.assertEqual(timezone.datetime(2017, 11, 2, 0, 0, 0), dates.parse_execution_date(execution_date_str_wo_ms)) self.assertEqual(timezone.datetime(2017, 11, 5, 16, 18, 30, 989729), dates.parse_execution_date(execution_date_str_w_ms)) self.assertRaises(ValueError, dates.parse_execution_date, bad_execution_date_str)
def task_instance_info(dag_id, execution_date, task_id): """ Returns a JSON with a task instance's public instance variables. The format for the exec_date is expected to be "YYYY-mm-DDTHH:MM:SS", for example: "2016-11-16T11:34:15". This will of course need to have been encoded for URL in the request. """ # Convert string datetime into actual datetime try: execution_date = parse_execution_date(execution_date) except ValueError: error_message = ( 'Given execution date, {}, could not be identified ' 'as a date. Example date format: 2015-11-16T14:34:15+00:00'.format( execution_date)) _log.info(error_message) response = jsonify({'error': error_message}) response.status_code = 400 return response try: info = get_task_instance(dag_id, task_id, execution_date) except (AirflowException, XToolException) as err: _log.info(err) response = jsonify(error="{}".format(err)) response.status_code = err.status_code return response # JSONify and return. fields = {k: str(v) for k, v in vars(info).items() if not k.startswith('_')} return jsonify(fields)
def trigger_dag(dag_id): """ Trigger a new dag run for a Dag with an execution date of now unless specified in the data. """ data = request.get_json(force=True) run_id = None if 'run_id' in data: run_id = data['run_id'] conf = None if 'conf' in data: conf = data['conf'] execution_date = None if 'execution_date' in data and data['execution_date'] is not None: execution_date = data['execution_date'] # Convert string datetime into actual datetime try: execution_date = parse_execution_date(execution_date) except ValueError: error_message = ( 'Given execution date, {}, could not be identified ' 'as a date. Example date format: 2015-11-16T14:34:15+00:00'.format( execution_date)) _log.info(error_message) response = jsonify({'error': error_message}) response.status_code = 400 return response try: dr = trigger.trigger_dag(dag_id, run_id, conf, execution_date) except (AirflowException, XToolException) as err: _log.error(err) response = jsonify(error="{}".format(err)) response.status_code = err.status_code return response if getattr(g, 'user', None): _log.info("User {} created {}".format(g.user, dr)) response = jsonify(message="Created {}".format(dr)) return response
def _read(self, ti, try_number, metadata=None): """ Endpoint for streaming log. :param ti: task instance object :param try_number: try_number of the task instance :param metadata: log metadata, can be used for steaming log reading and auto-tailing. :return a list of log documents and metadata. """ if not metadata: metadata = {'offset': 0} if 'offset' not in metadata: metadata['offset'] = 0 offset = metadata['offset'] log_id = self._render_log_id(ti, try_number) logs = self.es_read(log_id, offset) next_offset = offset if not logs else logs[-1].offset metadata['offset'] = next_offset # end_of_log_mark may contain characters like '\n' which is needed to # have the log uploaded but will not be stored in elasticsearch. metadata['end_of_log'] = False if not logs \ else logs[-1].message == self.end_of_log_mark.strip() cur_ts = dt.datetime.now() # Assume end of log after not receiving new log for 5 min, # as executor heartbeat is 1 min and there might be some # delay before Elasticsearch makes the log available. if 'last_log_timestamp' in metadata: last_log_ts = parse_execution_date(metadata['last_log_timestamp']) if cur_ts.diff(last_log_ts).in_minutes() >= 5: metadata['end_of_log'] = True if offset != next_offset or 'last_log_timestamp' not in metadata: metadata['last_log_timestamp'] = str(cur_ts) message = '\n'.join([log.message for log in logs]) return message, metadata
def wrapper(*args, **kwargs): session = settings.Session() if g.user.is_anonymous: user = '******' else: user = g.user.username log = models.Log(event=f.__name__, task_instance=None, owner=user, extra=str(list(request.args.items())), task_id=request.args.get('task_id'), dag_id=request.args.get('dag_id')) if 'execution_date' in request.args: log.execution_date = parse_execution_date( request.args.get('execution_date')) session.add(log) session.commit() return f(*args, **kwargs)
def wrapper(*args, **kwargs): # AnonymousUserMixin() has user attribute but its value is None. if current_user and hasattr(current_user, 'user') and current_user.user: user = current_user.user.username else: user = '******' log = models.Log(event=f.__name__, task_instance=None, owner=user, extra=str(list(request.args.items())), task_id=request.args.get('task_id'), dag_id=request.args.get('dag_id')) if request.args.get('execution_date'): log.execution_date = parse_execution_date( request.args.get('execution_date')) with create_session() as session: session.add(log) session.commit() return f(*args, **kwargs)