Python pendulum.now() Examples
The following are 30
code examples of pendulum.now().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pendulum
, or try the search function
.
Example #1
Source File: invite.py From parsec-cloud with GNU Affero General Public License v3.0 | 6 votes |
def api_invite_delete(self, client_ctx, msg): msg = invite_delete_serializer.req_load(msg) try: await self.delete( organization_id=client_ctx.organization_id, greeter=client_ctx.user_id, token=msg["token"], on=pendulum_now(), reason=msg["reason"], ) except InvitationNotFoundError: return {"status": "not_found"} except InvitationAlreadyDeletedError: return {"status": "already_deleted"} return invite_delete_serializer.rep_dump({"status": "ok"})
Example #2
Source File: vlob.py From parsec-cloud with GNU Affero General Public License v3.0 | 6 votes |
def api_vlob_create(self, client_ctx, msg): msg = vlob_create_serializer.req_load(msg) now = pendulum.now() if not timestamps_in_the_ballpark(msg["timestamp"], now): return {"status": "bad_timestamp", "reason": f"Timestamp is out of date."} try: await self.create(client_ctx.organization_id, client_ctx.device_id, **msg) except VlobAlreadyExistsError as exc: return vlob_create_serializer.rep_dump({"status": "already_exists", "reason": str(exc)}) except VlobAccessError: return vlob_create_serializer.rep_dump({"status": "not_allowed"}) except VlobEncryptionRevisionError: return vlob_create_serializer.rep_dump({"status": "bad_encryption_revision"}) except VlobInMaintenanceError: return vlob_create_serializer.rep_dump({"status": "in_maintenance"}) return vlob_create_serializer.rep_dump({"status": "ok"})
Example #3
Source File: remote_loader.py From parsec-cloud with GNU Affero General Public License v3.0 | 6 votes |
def create_realm(self, realm_id: EntryID): """ Raises: FSError FSBackendOfflineError """ certif = RealmRoleCertificateContent.build_realm_root_certif( author=self.device.device_id, timestamp=pendulum_now(), realm_id=realm_id ).dump_and_sign(self.device.signing_key) rep = await self._backend_cmds("realm_create", certif) if rep["status"] == "already_exists": # It's possible a previous attempt to create this realm # succeeded but we didn't receive the confirmation, hence # we play idempotent here. return elif rep["status"] != "ok": raise FSError(f"Cannot create realm {realm_id}: `{rep['status']}`")
Example #4
Source File: replication.py From bloop with MIT License | 6 votes |
def stream_replicate(): """Monitor changes in approximately real-time and replicate them""" stream = primary.stream(SomeDataBlob, "trim_horizon") next_heartbeat = pendulum.now() while True: now = pendulum.now() if now >= next_heartbeat: stream.heartbeat() next_heartbeat = now.add(minutes=10) record = next(stream) if record is None: continue if record["new"] is not None: replica.save(record["new"]) else: replica.delete(record["old"])
Example #5
Source File: manifest.py From parsec-cloud with GNU Affero General Public License v3.0 | 6 votes |
def to_remote(self, author: DeviceID, timestamp: Pendulum = None) -> RemoteFileManifest: # Checks self.assert_integrity() assert self.is_reshaped() # Blocks blocks = tuple(chunks[0].get_block_access() for chunks in self.blocks) return RemoteFileManifest( author=author, timestamp=timestamp or pendulum_now(), id=self.id, parent=self.parent, version=self.base_version + 1, created=self.created, updated=self.updated, size=self.size, blocksize=self.blocksize, blocks=blocks, )
Example #6
Source File: invite.py From parsec-cloud with GNU Affero General Public License v3.0 | 6 votes |
def new_for_device( self, organization_id: OrganizationID, greeter_user_id: UserID, created_on: Optional[Pendulum] = None, ) -> DeviceInvitation: """ Raise: Nothing """ created_on = created_on or pendulum_now() async with self.dbh.pool.acquire() as conn, conn.transaction(): token = await _do_new_user_invitation( conn, organization_id=organization_id, greeter_user_id=greeter_user_id, claimer_email=None, created_on=created_on, ) return DeviceInvitation( greeter_user_id=greeter_user_id, greeter_human_handle=None, token=token, created_on=created_on, )
Example #7
Source File: test_es_task_handler.py From airflow with Apache License 2.0 | 6 votes |
def test_read_with_match_phrase_query(self): similar_log_id = '{task_id}-{dag_id}-2016-01-01T00:00:00+00:00-1'.format( dag_id=TestElasticsearchTaskHandler.DAG_ID, task_id=TestElasticsearchTaskHandler.TASK_ID) another_test_message = 'another message' another_body = {'message': another_test_message, 'log_id': similar_log_id, 'offset': 1} self.es.index(index=self.index_name, doc_type=self.doc_type, body=another_body, id=1) ts = pendulum.now() logs, metadatas = self.es_task_handler.read(self.ti, 1, {'offset': 0, 'last_log_timestamp': str(ts), 'end_of_log': False}) self.assertEqual(1, len(logs)) self.assertEqual(len(logs), len(metadatas)) self.assertEqual(self.test_message, logs[0]) self.assertNotEqual(another_test_message, logs[0]) self.assertFalse(metadatas[0]['end_of_log']) self.assertEqual('1', metadatas[0]['offset']) self.assertTrue(timezone.parse(metadatas[0]['last_log_timestamp']) > ts)
Example #8
Source File: test_es_task_handler.py From airflow with Apache License 2.0 | 6 votes |
def test_read_nonexistent_log(self): ts = pendulum.now() # In ElasticMock, search is going to return all documents with matching index # and doc_type regardless of match filters, so we delete the log entry instead # of making a new TaskInstance to query. self.es.delete(index=self.index_name, doc_type=self.doc_type, id=1) logs, metadatas = self.es_task_handler.read(self.ti, 1, {'offset': 0, 'last_log_timestamp': str(ts), 'end_of_log': False}) self.assertEqual(1, len(logs)) self.assertEqual(len(logs), len(metadatas)) self.assertEqual([''], logs) self.assertFalse(metadatas[0]['end_of_log']) self.assertEqual('0', metadatas[0]['offset']) # last_log_timestamp won't change if no log lines read. self.assertTrue(timezone.parse(metadatas[0]['last_log_timestamp']) == ts)
Example #9
Source File: test_es_task_handler.py From airflow with Apache License 2.0 | 6 votes |
def test_read_with_empty_metadata(self): ts = pendulum.now() logs, metadatas = self.es_task_handler.read(self.ti, 1, {}) self.assertEqual(1, len(logs)) self.assertEqual(len(logs), len(metadatas)) self.assertEqual(self.test_message, logs[0]) self.assertFalse(metadatas[0]['end_of_log']) # offset should be initialized to 0 if not provided. self.assertEqual('1', metadatas[0]['offset']) # last_log_timestamp will be initialized using log reading time # if not last_log_timestamp is provided. self.assertTrue(timezone.parse(metadatas[0]['last_log_timestamp']) > ts) # case where offset is missing but metadata not empty. self.es.delete(index=self.index_name, doc_type=self.doc_type, id=1) logs, metadatas = self.es_task_handler.read(self.ti, 1, {'end_of_log': False}) self.assertEqual(1, len(logs)) self.assertEqual(len(logs), len(metadatas)) self.assertEqual([''], logs) self.assertFalse(metadatas[0]['end_of_log']) # offset should be initialized to 0 if not provided. self.assertEqual('0', metadatas[0]['offset']) # last_log_timestamp will be initialized using log reading time # if not last_log_timestamp is provided. self.assertTrue(timezone.parse(metadatas[0]['last_log_timestamp']) > ts)
Example #10
Source File: test_dag.py From airflow with Apache License 2.0 | 6 votes |
def test_sync_to_db_default_view(self, mock_now): dag = DAG( 'dag', start_date=DEFAULT_DATE, default_view="graph", ) with dag: DummyOperator(task_id='task', owner='owner1') SubDagOperator( task_id='subtask', owner='owner2', subdag=DAG( 'dag.subtask', start_date=DEFAULT_DATE, ) ) now = datetime.datetime.utcnow().replace(tzinfo=pendulum.timezone('UTC')) mock_now.return_value = now session = settings.Session() dag.sync_to_db(session=session) orm_dag = session.query(DagModel).filter(DagModel.dag_id == 'dag').one() self.assertIsNotNone(orm_dag.default_view) self.assertEqual(orm_dag.default_view, "graph") session.close()
Example #11
Source File: check.py From heartbeats with MIT License | 6 votes |
def process_at_service(self, service): """ 当 当前时间 > at时,看[at, at + grace]之间是否有上报的数据 """ latest_ping = self.get_last_ping(service) if not latest_ping: return at = pendulum.parse(service.value, tz=settings.TIME_ZONE).in_timezone('UTC') last_created = pendulum.instance(latest_ping.created) now = pendulum.now(tz='UTC') if now < at.add(minutes=int(service.grace)): return if last_created < at: self.notify(service, now)
Example #12
Source File: check.py From heartbeats with MIT License | 6 votes |
def notify(self, service, now): msg = '{service.name}({service.value},{service.grace}) not send heartbeat at {now}'.format( service=service, now=now.in_timezone(settings.TIME_ZONE).to_datetime_string() ) logger.warning(msg) if not service.notify_to.strip(): logger.warning('service %s notify_to is empty', service.name) return notify_async(service.notify_to.strip().split(";"), service.name, service.tp, service.value, service.grace, msg )
Example #13
Source File: latest_only_operator.py From airflow with Apache License 2.0 | 6 votes |
def choose_branch(self, context: Dict) -> Union[str, Iterable[str]]: # If the DAG Run is externally triggered, then return without # skipping downstream tasks if context['dag_run'] and context['dag_run'].external_trigger: self.log.info( "Externally triggered DAG_Run: allowing execution to proceed.") return list(context['task'].get_direct_relative_ids(upstream=False)) now = pendulum.now('UTC') left_window = context['dag'].following_schedule( context['execution_date']) right_window = context['dag'].following_schedule(left_window) self.log.info( 'Checking latest only with left_window: %s right_window: %s now: %s', left_window, right_window, now ) if not left_window < now <= right_window: self.log.info('Not latest execution, skipping downstream.') # we return an empty list, thus the parent BaseBranchOperator # won't exclude any downstream tasks from skipping. return [] else: self.log.info('Latest, allowing execution to proceed.') return list(context['task'].get_direct_relative_ids(upstream=False))
Example #14
Source File: test_taskinstance.py From airflow with Apache License 2.0 | 6 votes |
def test_execute_callback(self): called = False def on_execute_callable(context): nonlocal called called = True self.assertEqual( context['dag_run'].dag_id, 'test_dagrun_execute_callback' ) dag = DAG('test_execute_callback', start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=10)) task = DummyOperator(task_id='op', email='test@test.test', on_execute_callback=on_execute_callable, dag=dag) ti = TI(task=task, execution_date=datetime.datetime.now()) ti.state = State.RUNNING session = settings.Session() session.merge(ti) session.commit() ti._run_raw_task() assert called ti.refresh_from_db() assert ti.state == State.SUCCESS
Example #15
Source File: test_taskinstance.py From airflow with Apache License 2.0 | 6 votes |
def test_email_alert(self, mock_send_email): dag = models.DAG(dag_id='test_failure_email') task = BashOperator( task_id='test_email_alert', dag=dag, bash_command='exit 1', start_date=DEFAULT_DATE, email='to') ti = TI(task=task, execution_date=datetime.datetime.now()) try: ti.run() except AirflowException: pass (email, title, body), _ = mock_send_email.call_args self.assertEqual(email, 'to') self.assertIn('test_email_alert', title) self.assertIn('test_email_alert', body) self.assertIn('Try 1', body)
Example #16
Source File: test_taskinstance.py From airflow with Apache License 2.0 | 6 votes |
def test_success_callback_no_race_condition(self): callback_wrapper = CallbackWrapper() dag = DAG('test_success_callback_no_race_condition', start_date=DEFAULT_DATE, end_date=DEFAULT_DATE + datetime.timedelta(days=10)) task = DummyOperator(task_id='op', email='test@test.test', on_success_callback=callback_wrapper.success_handler, dag=dag) ti = TI(task=task, execution_date=datetime.datetime.now()) ti.state = State.RUNNING session = settings.Session() session.merge(ti) session.commit() callback_wrapper.wrap_task_instance(ti) ti._run_raw_task() self.assertTrue(callback_wrapper.callback_ran) self.assertEqual(callback_wrapper.task_state_in_callback, State.RUNNING) ti.refresh_from_db() self.assertEqual(ti.state, State.SUCCESS)
Example #17
Source File: handler.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def _apply_migration(conn, migration: MigrationItem): async with conn.transaction(): await conn.execute(migration.sql) if migration.idx >= CREATE_MIGRATION_TABLE_ID: # The migration table is created in the second migration sql = "INSERT INTO migration (_id, name, applied) VALUES ($1, $2, $3)" await conn.execute(sql, migration.idx, migration.name, pendulum_now())
Example #18
Source File: user.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def revoke_user( self, organization_id: OrganizationID, user_id: UserID, revoked_user_certificate: bytes, revoked_user_certifier: DeviceID, revoked_on: pendulum.Pendulum = None, ) -> None: org = self._organizations[organization_id] try: user = org.users[user_id] except KeyError: raise UserNotFoundError(user_id) if user.revoked_on: raise UserAlreadyRevokedError() org.users[user_id] = user.evolve( revoked_on=revoked_on or pendulum.now(), revoked_user_certificate=revoked_user_certificate, revoked_user_certifier=revoked_user_certifier, ) if user.human_handle: del org.human_handle_to_user_id[user.human_handle] await self._send_event( BackendEvent.USER_REVOKED, organization_id=organization_id, user_id=user_id )
Example #19
Source File: user.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def find( self, organization_id: OrganizationID, query: str = None, page: int = 0, per_page: int = 100, omit_revoked: bool = False, ): org = self._organizations[organization_id] users = org.users if query: try: UserID(query) except ValueError: # Contains invalid caracters, no need to go further return ([], 0) results = [ user_id for user_id in users.keys() if user_id.lower().startswith(query.lower()) ] else: results = users.keys() if omit_revoked: now = pendulum.now() def _user_is_revoked(user_id): revoked_on = org.users[user_id].revoked_on return revoked_on is not None and revoked_on <= now results = [user_id for user_id in results if not _user_is_revoked(user_id)] # PostgreSQL does case insensitive sort sorted_results = sorted(results, key=lambda s: s.lower()) return sorted_results[(page - 1) * per_page : page * per_page], len(results)
Example #20
Source File: user.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def is_valid(self) -> bool: return (pendulum.now() - self.created_on).total_seconds() < INVITATION_VALIDITY
Example #21
Source File: __init__.py From tap-facebook with GNU Affero General Public License v3.0 | 5 votes |
def ad_creative_success(response, stream=None): '''A success callback for the FB Batch endpoint used when syncing AdCreatives. Needs the stream to resolve schema refs and transform the successful response object.''' refs = load_shared_schema_refs() schema = singer.resolve_schema_references(stream.catalog_entry.schema.to_dict(), refs) rec = response.json() record = Transformer(pre_hook=transform_date_hook).transform(rec, schema) singer.write_record(stream.name, record, stream.stream_alias, utils.now())
Example #22
Source File: handshake.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def _apiv1_process_anonymous_answer( backend, transport: Transport, handshake: ServerHandshake ) -> Tuple[Optional[BaseClientContext], bytes, Optional[Dict]]: organization_id = handshake.answer_data["organization_id"] expected_rvk = handshake.answer_data["rvk"] def _make_error_infos(reason): return { "reason": reason, "handshake_type": APIV1_HandshakeType.ANONYMOUS, "organization_id": organization_id, } try: organization = await backend.organization.get(organization_id) except OrganizationNotFoundError: result_req = handshake.build_bad_identity_result_req() return None, result_req, _make_error_infos("Bad organization") if organization.expiration_date is not None and organization.expiration_date <= pendulum_now(): result_req = handshake.build_organization_expired_result_req() return None, result_req, _make_error_infos("Expired organization") if expected_rvk and organization.root_verify_key != expected_rvk: result_req = handshake.build_rvk_mismatch_result_req() return None, result_req, _make_error_infos("Bad root verify key") context = APIV1_AnonymousClientContext(transport, handshake, organization_id=organization_id) result_req = handshake.build_result_req() return context, result_req, None
Example #23
Source File: check.py From heartbeats with MIT License | 5 votes |
def process_every_service(self, service): """ 当前时间距离上一次上报时间 > value + grace就告警 """ latest_ping = self.get_last_ping(service) if not latest_ping: return now = pendulum.now(tz='UTC') period = now - pendulum.instance(latest_ping.created) if period.total_minutes() > (int(service.value) + int(service.grace)): self.notify(service, now)
Example #24
Source File: remote_loader.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def _get_user_realm_role_at(self, user_id: UserID, timestamp: Pendulum): if ( not self._realm_role_certificates_cache or self._realm_role_certificates_cache_timestamp <= timestamp ): cache_timestamp = pendulum_now() self._realm_role_certificates_cache, _ = await self._load_realm_role_certificates() # Set the cache timestamp in two times to avoid invalid value in case of exception self._realm_role_certificates_cache_timestamp = cache_timestamp for certif in reversed(self._realm_role_certificates_cache): if certif.user_id == user_id and certif.timestamp <= timestamp: return certif.role else: return None
Example #25
Source File: sync_transactions.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def get_minimal_remote_manifest(self, entry_id: EntryID) -> Optional[RemoteManifest]: manifest = await self.local_storage.get_manifest(entry_id) if not manifest.is_placeholder: return None return manifest.base.evolve(author=self.local_author, timestamp=pendulum_now(), version=1) # Atomic transactions
Example #26
Source File: main.py From performance_tracker with GNU General Public License v3.0 | 5 votes |
def main(command, datetime=None): ctx = Context() if datetime is None: datetime = pendulum.now(ctx.config["TIMEZONE"]) else: try: datetime = pendulum.from_format(datetime, 'YYYY-MM-DD', tz=ctx.config["TIMEZONE"]) ctx.logger(f"Running in date override mode with date {datetime.format('YYYY-MM-DD')}.") ctx.logger("This is an experimental feature and may generate incorrect data.") except: ctx.logger("Failed to parse custom datetime argument") return 1 try: outcome = ACTIONS[command](ctx, datetime) except Exception as exc: ctx.logger(exc) traceback.print_tb(exc.__traceback__) ctx.logger(f"{command} failed") return 1 if outcome is 0: ctx.logger(f"{command} completed successfully") return 0 else: ctx.logger(f"{command} failed") return 1
Example #27
Source File: manifest.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def evolve_and_mark_updated(self, **data) -> "LocalManifest": if "updated" not in data: data["updated"] = pendulum_now() data.setdefault("need_sync", True) return self.evolve(**data) # Remote methods
Example #28
Source File: test_taskinstance.py From airflow with Apache License 2.0 | 5 votes |
def _test_previous_dates_setup(schedule_interval: Union[str, datetime.timedelta, None], catchup: bool, scenario: List[str]) -> list: dag_id = 'test_previous_dates' dag = models.DAG(dag_id=dag_id, schedule_interval=schedule_interval, catchup=catchup) task = DummyOperator(task_id='task', dag=dag, start_date=DEFAULT_DATE) def get_test_ti(session, execution_date: pendulum.DateTime, state: str) -> TI: dag.create_dagrun( run_type=DagRunType.SCHEDULED, state=state, execution_date=execution_date, start_date=pendulum.now('UTC'), session=session ) ti = TI(task=task, execution_date=execution_date) ti.set_state(state=State.SUCCESS, session=session) return ti with create_session() as session: # type: Session date = cast(pendulum.DateTime, pendulum.parse('2019-01-01T00:00:00+00:00')) ret = [] for idx, state in enumerate(scenario): new_date = date.add(days=idx) ti = get_test_ti(session, new_date, state) ret.append(ti) return ret
Example #29
Source File: test_taskinstance.py From airflow with Apache License 2.0 | 5 votes |
def test_execute_queries_count_store_serialized(self): with create_session() as session: dag = DAG('test_queries', start_date=DEFAULT_DATE) task = DummyOperator(task_id='op', dag=dag) ti = TI(task=task, execution_date=datetime.datetime.now()) ti.state = State.RUNNING session.merge(ti) with assert_queries_count(10), patch( "airflow.models.taskinstance.STORE_SERIALIZED_DAGS", True ): ti._run_raw_task()
Example #30
Source File: __init__.py From tap-facebook with GNU Affero General Public License v3.0 | 5 votes |
def job_params(self): start_date = get_start(self, self.bookmark_key) buffer_days = 28 if CONFIG.get('insights_buffer_days'): buffer_days = int(CONFIG.get('insights_buffer_days')) buffered_start_date = start_date.subtract(days=buffer_days) end_date = pendulum.now() if CONFIG.get('end_date'): end_date = pendulum.parse(CONFIG.get('end_date')) # Some automatic fields (primary-keys) cannot be used as 'fields' query params. while buffered_start_date <= end_date: yield { 'level': self.level, 'action_breakdowns': list(self.action_breakdowns), 'breakdowns': list(self.breakdowns), 'limit': self.limit, 'fields': list(self.fields().difference(self.invalid_insights_fields)), 'time_increment': self.time_increment, 'action_attribution_windows': list(self.action_attribution_windows), 'time_ranges': [{'since': buffered_start_date.to_date_string(), 'until': buffered_start_date.to_date_string()}] } buffered_start_date = buffered_start_date.add(days=1)