Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def add_success_result(self, repo_id, offset):
started = datetime.datetime.now(dateutils.local_tz())
completed = started + datetime.timedelta(days=offset)
r = RepoSyncResult.expected_result(repo_id, 'foo', 'bar', dateutils.format_iso8601_datetime(started), dateutils.format_iso8601_datetime(completed), 1, 1, 1, '', '', RepoSyncResult.RESULT_SUCCESS)
RepoSyncResult.get_collection().save(r, safe=True)
def _populate_for_date_queries(self):
'''
Populates the history with events scattered over the course of a year, suitable for
date range tests.
'''
e1 = ConsumerHistoryEvent(123, 'admin', consumer_history.TYPE_CONSUMER_CREATED, None)
e2 = ConsumerHistoryEvent(123, 'admin', consumer_history.TYPE_CONSUMER_DELETED, None)
e3 = ConsumerHistoryEvent(123, 'admin', consumer_history.TYPE_REPO_BOUND, None)
e4 = ConsumerHistoryEvent(123, 'admin', consumer_history.TYPE_REPO_UNBOUND, None)
e1.timestamp = dateutils.format_iso8601_datetime(datetime.datetime(2000, 2, 1))
e2.timestamp = dateutils.format_iso8601_datetime(datetime.datetime(2000, 4, 1))
e3.timestamp = dateutils.format_iso8601_datetime(datetime.datetime(2000, 6, 1))
e4.timestamp = dateutils.format_iso8601_datetime(datetime.datetime(2000, 10, 1))
self.consumer_history_api.collection.insert(e1)
self.consumer_history_api.collection.insert(e2)
self.consumer_history_api.collection.insert(e3)
self.consumer_history_api.collection.insert(e4)
'''
Populates the CDS history collection with entries staggered by a large date range,
suitable for being able to query within date ranges.
The events are manually created and stored in the database; using the API calls
will cause the timestamps to be set to 'now' in all cases.
'''
e1 = CDSHistoryEvent('cds1.example.com', 'admin', CDSHistoryEventType.REGISTERED)
e2 = CDSHistoryEvent('cds2.example.com', 'admin', CDSHistoryEventType.REPO_ASSOCIATED)
e3 = CDSHistoryEvent('cds3.example.com', 'admin', CDSHistoryEventType.REPO_UNASSOCIATED)
e4 = CDSHistoryEvent('cds4.example.com', 'admin', CDSHistoryEventType.UNREGISTERED)
e1.timestamp = dateutils.format_iso8601_datetime(datetime.datetime(2000, 2, 1, tzinfo=dateutils.utc_tz()))
e2.timestamp = dateutils.format_iso8601_datetime(datetime.datetime(2000, 4, 1, tzinfo=dateutils.utc_tz()))
e3.timestamp = dateutils.format_iso8601_datetime(datetime.datetime(2000, 6, 1, tzinfo=dateutils.utc_tz()))
e4.timestamp = dateutils.format_iso8601_datetime(datetime.datetime(2000, 10, 1, tzinfo=dateutils.utc_tz()))
self.cds_history_api.collection.insert(e1)
self.cds_history_api.collection.insert(e2)
self.cds_history_api.collection.insert(e3)
self.cds_history_api.collection.insert(e4)
def _now_timestamp():
"""
@return: timestamp suitable for indicating when a publish completed
@rtype: str
"""
now = datetime.datetime.now(dateutils.local_tz())
now_in_iso_format = dateutils.format_iso8601_datetime(now)
return now_in_iso_format
def rejected(self, reply):
"""
Notification (reply) indicating an RMI request has been rejected.
This information used to update the task status.
:param reply: A rejected reply object.
:type reply: gofer.rmi.async.Rejected
"""
_logger.warn(_('Task RMI (rejected): %(r)s'), {'r': reply})
call_context = dict(reply.data)
action = call_context.get('action')
task_id = call_context['task_id']
finished = reply.timestamp
if not finished:
now = datetime.now(dateutils.utc_tz())
finished = dateutils.format_iso8601_datetime(now)
TaskStatus.objects(task_id=task_id).update_one(set__finish_time=finished,
set__state=constants.CALL_ERRORED_STATE)
if action == 'bind':
ReplyHandler._bind_failed(task_id, call_context)
return
if action == 'unbind':
ReplyHandler._unbind_failed(task_id, call_context)
return
self.options = options or {}
self.principal = principal
self.resource = resource
self.schedule = schedule
self.task = task
self.total_run_count = total_run_count
self.kwargs['scheduled_call_id'] = self.id
if first_run is None:
# get the date and time from the iso_schedule value, and if it does not have a date and
# time, use the current date and time
self.first_run = dateutils.format_iso8601_datetime(
dateutils.parse_iso8601_interval(iso_schedule)[1] or
datetime.utcnow().replace(tzinfo=isodate.UTC))
elif isinstance(first_run, datetime):
self.first_run = dateutils.format_iso8601_datetime(first_run)
else:
self.first_run = first_run
if remaining_runs is None:
self.remaining_runs = dateutils.parse_iso8601_interval(iso_schedule)[2]
else:
self.remaining_runs = remaining_runs
self.next_run = self.calculate_next_run()
def __init__(self, task):
super(TaskHistory, self).__init__()
self.task_type = task.__class__.__name__
for attr in ('id', 'class_name', 'method_name', 'args', 'kwargs',
'state', 'progress', 'result', 'exception', 'traceback',
'consecutive_failures', 'job_id'):
setattr(self, attr, copy.copy(getattr(task, attr)))
# remove the kwargs that can't be stored in the database
for arg in ('synchronizer', 'progress_callback'):
self.kwargs.pop(arg, None)
for attr in ('scheduled_time', 'start_time', 'finish_time'):
setattr(self, attr, dateutils.format_iso8601_datetime(getattr(task, attr)))
self.task_string = str(task)
def _convert_repo_dates_to_strings(repo):
"""
Convert the last_unit_added & last_unit_removed fields of a repository
This modifies the repository in place
:param repo: diatabase representation of a repo
:type repo: dict
"""
# convert the native datetime object to a string with timezone specified
last_unit_added = repo.get('last_unit_added')
if last_unit_added:
new_date = dateutils.to_utc_datetime(last_unit_added,
no_tz_equals_local_tz=False)
repo['last_unit_added'] = dateutils.format_iso8601_datetime(new_date)
last_unit_removed = repo.get('last_unit_removed')
if last_unit_removed:
new_date = dateutils.to_utc_datetime(last_unit_removed,
no_tz_equals_local_tz=False)
repo['last_unit_removed'] = dateutils.format_iso8601_datetime(new_date)
def _now_timestamp():
"""
@return: timestamp suitable for indicating when a publish completed
@rtype: str
"""
now = datetime.datetime.now(dateutils.local_tz())
now_in_iso_format = dateutils.format_iso8601_datetime(now)
return now_in_iso_format
'schedule': '2011-12-13T13:45:00-07:00/PT5M',
'type': 'sync'}
}}}
"""
if schedule_type not in self.schedule_types:
return self.not_found('No schedule type: %s' % schedule_type)
repo = api.repository(repo_id, ['id', 'sync_schedule', 'sync_options'])
if repo is None:
return self.not_found('No repository %s' % repo_id)
next_sync_time = None
if repo['sync_schedule']:
scheduled_task_list = async.find_async(method_name="_sync",
repo_id=repo_id)
if scheduled_task_list:
scheduled_task = scheduled_task_list[0]
next_sync_time = format_iso8601_datetime(
scheduled_task.scheduled_time)
data = {
'id': repo_id,
'href': serialization.repo.v1_href(repo),
'type': schedule_type,
'schedule': repo['sync_schedule'],
'options': repo['sync_options'],
'next_sync_time': next_sync_time,
}
return self.ok(data)