Matt Keller
Matt Keller

Reputation: 347

Easiest way to store a single timestamp in appengine

I am running a Python script on Google's AppEngine. The python script is very basic. Every time the script runs i need it to update a timestamp SOMEWHERE so i can record and keep track of when the script last ran. This will allow me to do logic based on when the last time the script ran, etc. At the end of the script i'll update the timestamp to the current time.

Using Google's NBD seems to be overkill for this but it also seems to be the only way to store ANY data in AppEngine. Is there a better/easier way to do what i want?

Upvotes: 0

Views: 672

Answers (2)

Matt Keller
Matt Keller

Reputation: 347

Another way to solve this, that i found, is to use memcache. It's super easy. Though it should probably be noted that memcache could be cleared at anytime, so NDB is probably a better solution.

Set the timestamp:

memcache.set("timestamp", current_timestamp)

Then, to read the timestamp:

memcache.get("timestamp")

Upvotes: 0

Tim Hoffman
Tim Hoffman

Reputation: 12986

It really is very simple, and not overkill.. Anything else will be overkill in appengine - trying to use the low level API, GCS or storing it in some other service will all require more work and introduce levels of complexity and potential unreliability and will be slower. In addition any mechanism that doesn't store/retrieve datetime objects as datetime objects (ie text file) means you will need to parse the string as a date, creating even more work.

Define it

class TimeStamp(ndb.Model):
    timestamp = ndb.DatetimeProperty(auto_now=True)

Create it.

TimeStamp(key_name="TIMESTAMP").put()

Update it without reading.

TimeStamp(key_name="TIMESTAMP").put()

Read it, then update.

ts = TimeStamp.get_by_id("TIMESTAMP")
ts.put()

Upvotes: 4

Related Questions