Cleanup different places using unix_time()

This commit is contained in:
Steve Pulec 2015-11-27 14:14:40 -05:00
commit 705ec314a3
15 changed files with 60 additions and 82 deletions

View file

@ -2,8 +2,8 @@ from __future__ import unicode_literals
from datetime import datetime
import uuid
from moto.core.utils import unix_time
from ..exceptions import SWFWorkflowExecutionClosedError
from ..utils import now_timestamp
from .timeout import Timeout
@ -15,7 +15,7 @@ class ActivityTask(object):
self.activity_type = activity_type
self.details = None
self.input = input
self.last_heartbeat_timestamp = now_timestamp()
self.last_heartbeat_timestamp = unix_time()
self.scheduled_event_id = scheduled_event_id
self.started_event_id = None
self.state = "SCHEDULED"
@ -60,19 +60,18 @@ class ActivityTask(object):
self.state = "FAILED"
def reset_heartbeat_clock(self):
self.last_heartbeat_timestamp = now_timestamp()
self.last_heartbeat_timestamp = unix_time()
def first_timeout(self):
if not self.open or not self.workflow_execution.open:
return None
# TODO: handle the "NONE" case
heartbeat_timeout_at = self.last_heartbeat_timestamp + \
int(self.timeouts["heartbeatTimeout"])
heartbeat_timeout_at = (self.last_heartbeat_timestamp +
int(self.timeouts["heartbeatTimeout"]))
_timeout = Timeout(self, heartbeat_timeout_at, "HEARTBEAT")
if _timeout.reached:
return _timeout
def process_timeouts(self):
_timeout = self.first_timeout()
if _timeout:

View file

@ -2,8 +2,8 @@ from __future__ import unicode_literals
from datetime import datetime
import uuid
from moto.core.utils import unix_time
from ..exceptions import SWFWorkflowExecutionClosedError
from ..utils import now_timestamp
from .timeout import Timeout
@ -49,7 +49,7 @@ class DecisionTask(object):
def start(self, started_event_id):
self.state = "STARTED"
self.started_timestamp = now_timestamp()
self.started_timestamp = unix_time()
self.started_event_id = started_event_id
def complete(self):

View file

@ -1,10 +1,8 @@
from __future__ import unicode_literals
from datetime import datetime
from time import mktime
from moto.core.utils import underscores_to_camelcase
from moto.core.utils import underscores_to_camelcase, unix_time
from ..utils import decapitalize, now_timestamp
from ..utils import decapitalize
# We keep track of which history event types we support
@ -28,6 +26,7 @@ SUPPORTED_HISTORY_EVENT_TYPES = (
"WorkflowExecutionTimedOut",
)
class HistoryEvent(object):
def __init__(self, event_id, event_type, event_timestamp=None, **kwargs):
if event_type not in SUPPORTED_HISTORY_EVENT_TYPES:
@ -39,16 +38,16 @@ class HistoryEvent(object):
if event_timestamp:
self.event_timestamp = event_timestamp
else:
self.event_timestamp = now_timestamp()
self.event_timestamp = unix_time()
# pre-populate a dict: {"camelCaseKey": value}
self.event_attributes = {}
for key, value in kwargs.items():
if value:
camel_key = underscores_to_camelcase(key)
if key == "task_list":
value = { "name": value }
value = {"name": value}
elif key == "workflow_type":
value = { "name": value.name, "version": value.version }
value = {"name": value.name, "version": value.version}
elif key == "activity_type":
value = value.to_short_dict()
self.event_attributes[camel_key] = value

View file

@ -1,4 +1,4 @@
from ..utils import now_timestamp
from moto.core.utils import unix_time
class Timeout(object):
@ -9,4 +9,4 @@ class Timeout(object):
@property
def reached(self):
return now_timestamp() >= self.timestamp
return unix_time() >= self.timestamp

View file

@ -1,9 +1,7 @@
from __future__ import unicode_literals
from datetime import datetime
from time import mktime
import uuid
from moto.core.utils import camelcase_to_underscores
from moto.core.utils import camelcase_to_underscores, unix_time
from ..constants import (
DECISIONS_FIELDS,
@ -13,7 +11,7 @@ from ..exceptions import (
SWFValidationException,
SWFDecisionValidationException,
)
from ..utils import decapitalize, now_timestamp
from ..utils import decapitalize
from .activity_task import ActivityTask
from .activity_type import ActivityType
from .decision_task import DecisionTask
@ -59,7 +57,7 @@ class WorkflowExecution(object):
self.latest_execution_context = None
self.parent = None
self.start_timestamp = None
self.tag_list = [] # TODO
self.tag_list = [] # TODO
self.timeout_type = None
self.workflow_type = workflow_type
# args processing
@ -89,7 +87,7 @@ class WorkflowExecution(object):
def _set_from_kwargs_or_workflow_type(self, kwargs, local_key, workflow_type_key=None):
if workflow_type_key is None:
workflow_type_key = "default_"+local_key
workflow_type_key = "default_" + local_key
value = kwargs.get(local_key)
if not value and hasattr(self.workflow_type, workflow_type_key):
value = getattr(self.workflow_type, workflow_type_key)
@ -131,7 +129,7 @@ class WorkflowExecution(object):
"taskList": {"name": self.task_list}
}
}
#configuration
# configuration
for key in self._configuration_keys:
attr = camelcase_to_underscores(key)
if not hasattr(self, attr):
@ -139,9 +137,9 @@ class WorkflowExecution(object):
if not getattr(self, attr):
continue
hsh["executionConfiguration"][key] = getattr(self, attr)
#counters
# counters
hsh["openCounts"] = self.open_counts
#latest things
# latest things
if self.latest_execution_context:
hsh["latestExecutionContext"] = self.latest_execution_context
if self.latest_activity_task_timestamp:
@ -225,7 +223,7 @@ class WorkflowExecution(object):
return evt
def start(self):
self.start_timestamp = now_timestamp()
self.start_timestamp = unix_time()
self._add_event(
"WorkflowExecutionStarted",
child_policy=self.child_policy,
@ -403,7 +401,7 @@ class WorkflowExecution(object):
def complete(self, event_id, result=None):
self.execution_status = "CLOSED"
self.close_status = "COMPLETED"
self.close_timestamp = now_timestamp()
self.close_timestamp = unix_time()
self._add_event(
"WorkflowExecutionCompleted",
decision_task_completed_event_id=event_id,
@ -414,7 +412,7 @@ class WorkflowExecution(object):
# TODO: implement lenght constraints on details/reason
self.execution_status = "CLOSED"
self.close_status = "FAILED"
self.close_timestamp = now_timestamp()
self.close_timestamp = unix_time()
self._add_event(
"WorkflowExecutionFailed",
decision_task_completed_event_id=event_id,
@ -470,7 +468,7 @@ class WorkflowExecution(object):
# find timeouts or default timeout, else fail
timeouts = {}
for _type in ["scheduleToStartTimeout", "scheduleToCloseTimeout", "startToCloseTimeout", "heartbeatTimeout"]:
default_key = "default_task_"+camelcase_to_underscores(_type)
default_key = "default_task_" + camelcase_to_underscores(_type)
default_value = getattr(activity_type, default_key)
timeouts[_type] = attributes.get(_type, default_value)
if not timeouts[_type]:
@ -504,7 +502,7 @@ class WorkflowExecution(object):
)
self.domain.add_to_activity_task_list(task_list, task)
self.open_counts["openActivityTasks"] += 1
self.latest_activity_task_timestamp = now_timestamp()
self.latest_activity_task_timestamp = unix_time()
def _find_activity_task(self, task_token):
for task in self.activity_tasks:

View file

@ -1,9 +1,3 @@
from datetime import datetime
from time import mktime
def decapitalize(key):
return key[0].lower() + key[1:]
def now_timestamp():
return float(mktime(datetime.utcnow().timetuple()))