Skip to content

Commit d55e7f4

Browse files
committed
Memorystore migration cleanup
1 parent baa3464 commit d55e7f4

File tree

10 files changed

+37
-30
lines changed

10 files changed

+37
-30
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Module | Topic | Video | Codelab | START here | FINISH here
8383
3|Migrate to Cloud Datastore| [link](http://twitter.com/googledevs/status/1422966928910393347?utm_source=twitter&utm_medium=unpaidsoc&utm_campaign=CDR_wes_aap-serverless_mgrcloudds_201003&utm_content=-) | [link](http://g.co/codelabs/pae-migrate-datastore) | Module 2 [code](/mod2a-cloudndb) (2.x) & [code](/mod2b-cloudndb) (3.x) | Module 3 [code](/mod3a-datastore) (2.x) & [code](/mod3b-datastore) (3.x)
8484
4|Migrate to Cloud Run with Docker| [link](https://twitter.com/googledevs/status/1428041270702735362?utm_source=twitter&utm_medium=unpaidsoc&utm_campaign=CDR_wes_aap-serverless_mgrcrdckr_sms_201017&utm_content=-)| [link](http://g.co/codelabs/pae-migrate-rundocker) | Module 2 [code](/mod2a-cloudndb) (2.x) & Module 3 [code](/mod3b-datastore) (3.x) | Module 4 [code](/mod4a-rundocker) (2.x) & [code](/mod4b-rundocker) (3.x)
8585
5|Migrate to Cloud Run with Buildpacks| [link](https://twitter.com/googledevs/status/1433113274984271875?utm_source=twitter&utm_medium=unpaidsoc&utm_campaign=CDR_wes_aap-serverless_mgrcrbdpk_sms_201031&utm_content=-) | [link](http://g.co/codelabs/pae-migrate-runbldpks) | Module 2 [code](/mod2b-cloudndb) (3.x) | Module 5 [code](/mod5-runbldpks) (3.x)
86-
6|Migrate to Cloud Firestore| _N/A_ | _N/A_ | Module 3 [code](/mod3b-datastore) (3.x) | _no work required; Datastore upgrade automatic_
86+
6|Migrate to Cloud Firestore| _N/A_ | _N/A_ | Module 3 [code](/mod3b-datastore) (3.x) | _no work required; [Datastore upgrade automatic](https://cloud.google.com/datastore/docs/upgrade-to-firestore)_
8787
7|Add App Engine `taskqueue` push tasks| [link](https://twitter.com/googledevs/status/1443410302113099778?utm_source=twitter&utm_medium=unpaidsoc&utm_campaign=CDR_wes_aap-serverless_mgrgaetasks_sms_201028&utm_content=-) | [link](http://g.co/codelabs/pae-migrate-gaetasks) | Module 1 [code](/mod1-flask) (2.x) | Module 7 [code](/mod7-gaetasks) (2.x) & [code](/mod7b-gaetasks) (3.x)
8888
8|Migrate to Cloud Tasks| [link](https://twitter.com/googledevs/status/1450960021018267656?utm_source=twitter&utm_medium=unpaidsoc&utm_campaign=CDR_wes_aap-serverless_mgrcloudtasks_sms_201112&utm_content=-) | [link](http://g.co/codelabs/pae-migrate-cloudtasks) | Module 7 [code](/mod7-gaetasks) (2.x) | Module 8 [code](/mod8-cloudtasks) (2.x)
8989
9|Migrate to Python 3, Cloud Datastore & Cloud Tasks v2| _TBD_ | _TBD_ | Module 8 [code](/mod8-cloudtasks) (2.x) | _TBD_

mod13a-memorystore/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Module 12 - Add usage of App Engine `memcache` to Flask `ndb` sample app
1+
# Module 13 - Migrate from App Engine `memcache` to Cloud Memorystore
22

3-
This repo folder is the corresponding Python 2 code to the Module 12 codelab (TBD). The tutorial STARTs with the Python 2 code in the [Module 1 repo folder](/mod1-flask) and leads developers through adding usage of App Engine's `memcache`, culminating in the code in this folder.
3+
This repo folder is the corresponding Python 2 code to the Module 13 codelab (TBD). The tutorial STARTs with the Python 2 code in the [Module 12 repo folder](/mod12-memcache) and leads developers through a migration to Cloud Memorystore, culminating in the code in this (`mod13a-memorystore`) folder. Also included is a migration from App Engine `ndb` to Google Cloud NDB, mirroring the content covered in [Module 2](http://g.co/codelabs/pae-migrate-cloudndb).

mod13a-memorystore/main.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
import os
1616
import pickle
1717
from flask import Flask, render_template, request
18-
from google.appengine.ext import ndb
18+
from google.cloud import ndb
1919
import redis
2020

2121
app = Flask(__name__)
22+
ds_client = ndb.Client()
2223
HOUR = 3600
2324
REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')
2425
REDIS_PORT = os.environ.get('REDIS_PORT', '6379')
@@ -31,12 +32,14 @@ class Visit(ndb.Model):
3132

3233
def store_visit(remote_addr, user_agent):
3334
'create new Visit entity in Datastore'
34-
Visit(visitor='{}: {}'.format(remote_addr, user_agent)).put()
35+
with ds_client.context():
36+
Visit(visitor='{}: {}'.format(remote_addr, user_agent)).put()
3537

3638
def fetch_visits(limit):
3739
'get most recent visits'
38-
return (v.to_dict() for v in Visit.query().order(
39-
-Visit.timestamp).fetch(limit))
40+
with ds_client.context():
41+
return (v.to_dict() for v in Visit.query().order(
42+
-Visit.timestamp).fetch(limit))
4043

4144
@app.route('/')
4245
def root():
@@ -51,6 +54,6 @@ def root():
5154
if not visits or visits[0]['visitor'] != visitor:
5255
store_visit(ip_addr, usr_agt)
5356
visits = list(fetch_visits(10))
54-
REDIS.set('visits', pickle.dumps(visits), ex=HOUR) # set() not add()
57+
REDIS.set('visits', pickle.dumps(visits), ex=HOUR)
5558

5659
return render_template('index.html', visits=visits)

mod13a-memorystore/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
flask
22
redis
3+
google-cloud-ndb

mod13b-memorystore/README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
# Module 12 - Add usage of App Engine `memcache` to Flask `ndb` sample app
1+
# Module 13 - Migrate from App Engine `memcache` to Cloud Memorystore
22

3-
This repo folder is the corresponding Python 3 code to the Module 12 codelab (TBD). The tutorial STARTs with the Python 2 code in the [Module 1 repo folder](/mod1-flask) and leads developers through adding usage of App Engine's `memcache`, followed by a bonus migration to Python 3, culminating in the code in this folder.
3+
This repo folder is the corresponding Python 3 code to a pair of different migrations related to the Module 12 and 13 codelabs. The tutorial either...
44

5-
> **LEGACY SERVICES PUBLIC PREVIEW**: Accessing legacy services such as App Engine `ndb` and `memcache` from Python 3 (and next generation App Engine in general) is available in a public preview. See the [Sep 2021 announcement](https://twitter.com/googledevs/status/1445916786755571712) for more information.
5+
- STARTs with the _Python 3_ code in the [Module 12b repo folder](/mod12b-memcache) and leads developers through migrations from App Engine `memcache` to Cloud Memorystore and App Engine `ndb` to Cloud Datastore (skipping Cloud NDB); **or**,
6+
- STARTs with the _Python 2_ code in the [Module 13a repo folder](/mod13a-memorystore) and leads developers through migrations from Cloud NDB to Cloud Datastore, mirroring the content covered in [Module 3](http://g.co/codelabs/pae-migrate-datastore), and also a BONUS migration from Python 2 to 3.
7+
8+
Either way, both set of migrations culminate in the code in this (`mod13b-memorystore`) folder.

mod13b-memorystore/main.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,34 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from datetime import datetime
1516
import os
1617
import pickle
1718
from flask import Flask, render_template, request
18-
from google.cloud import ndb
19+
from google.cloud import datastore
1920
import redis
2021

2122
app = Flask(__name__)
22-
ds_client = ndb.Client()
23+
ds_client = datastore.Client()
2324
HOUR = 3600
2425
REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')
2526
REDIS_PORT = os.environ.get('REDIS_PORT', '6379')
2627
REDIS = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
2728

28-
class Visit(ndb.Model):
29-
'Visit entity registers visitor IP address & timestamp'
30-
visitor = ndb.StringProperty()
31-
timestamp = ndb.DateTimeProperty(auto_now_add=True)
32-
3329
def store_visit(remote_addr, user_agent):
3430
'create new Visit entity in Datastore'
35-
with ds_client.context():
36-
Visit(visitor='{}: {}'.format(remote_addr, user_agent)).put()
31+
entity = datastore.Entity(key=ds_client.key('Visit'))
32+
entity.update({
33+
'timestamp': datetime.now(),
34+
'visitor': '{}: {}'.format(remote_addr, user_agent),
35+
})
36+
ds_client.put(entity)
3737

3838
def fetch_visits(limit):
3939
'get most recent visits'
40-
with ds_client.context():
41-
return (v.to_dict() for v in Visit.query().order(
42-
-Visit.timestamp).fetch(limit))
40+
query = ds_client.query(kind='Visit')
41+
query.order = ['-timestamp']
42+
return query.fetch(limit=limit)
4343

4444
@app.route('/')
4545
def root():
@@ -54,6 +54,6 @@ def root():
5454
if not visits or visits[0]['visitor'] != visitor:
5555
store_visit(ip_addr, usr_agt)
5656
visits = list(fetch_visits(10))
57-
REDIS.set('visits', pickle.dumps(visits), ex=HOUR) # set() not add()
57+
REDIS.set('visits', pickle.dumps(visits), ex=HOUR)
5858

5959
return render_template('index.html', visits=visits)

mod13b-memorystore/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
flask
22
redis
3-
google-cloud-ndb
3+
google-cloud-datastore

mod2a-cloudndb/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Module 2 - Migrate from App Engine `ndb` to Google Cloud NDB
1+
# Module 2 - Migrate from App Engine `ndb` to Cloud NDB
22

3-
This repo folder is the corresponding Python 2 code to the [Module 2 codelab](http://g.co/codelabs/pae-migrate-cloudndb). The tutorial STARTs with the Python 2 code in the [Module 1 repo folder](/mod1-flask) and leads developers through migrating away from App Engine's `ndb` to Cloud NDB to access Datastore, culminating in the code in this folder.
3+
This repo folder is the corresponding Python 2 code to the [Module 2 codelab](http://g.co/codelabs/pae-migrate-cloudndb). The tutorial STARTs with the Python 2 code in the [Module 1 repo folder](/mod1-flask) and leads developers through migrating away from App Engine's `ndb` to Cloud NDB to access Datastore, culminating in the code in this (`mod2a-cloudndb`) folder.

mod2b-cloudndb/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Module 2 - Migrate from App Engine `ndb` to Google Cloud NDB
1+
# Module 2 - Migrate from App Engine `ndb` to Cloud NDB
22

3-
This repo folder is the corresponding Python 3 code to the [Module 2 codelab](http://g.co/codelabs/pae-migrate-cloudndb). The tutorial STARTs with the Python 2 code in the [Module 1 repo folder](/mod1-flask) and leads developers through migrating away from App Engine's `ndb` to Cloud NDB to access Datastore culminating in the code in the [mod2a-cloudndb](/mod2a-cloudndb) folder. That is followed by a BONUS migration to Python 3, thus the code in *this* (`mod2b-cloudndb`) folder.
3+
This repo folder is the corresponding Python 3 code to the [Module 2 codelab](http://g.co/codelabs/pae-migrate-cloudndb). The tutorial STARTs with the Python 2 code in the [Module 1 repo folder](/mod1-flask) and leads developers through migrating away from App Engine's `ndb` to Cloud NDB to access Datastore culminating in the code in the [mod2a-cloudndb](/mod2a-cloudndb) folder. That is followed by a BONUS migration to Python 3, culminating in the code in *this* (`mod2b-cloudndb`) folder.

mod8-cloudtasks/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Module 8 - Migrate from App Engine `taskqueue` to Google Cloud Tasks
1+
# Module 8 - Migrate from App Engine `taskqueue` to Cloud Tasks
22

33
This repo folder is the corresponding Python 2 code to the [Module 8 codelab](http://g.co/codelabs/pae-migrate-cloudtasks). The tutorial STARTs with the Python 2 code in the [Module 7 repo folder](/mod7-gaetasks) and leads developers through migrating from `taskqueue` to Cloud Tasks, culminating in the code in this folder.

0 commit comments

Comments
 (0)