Skip to content

Commit

Permalink
Merge pull request #8 from mohemohe/fix-s3-timestamp
Browse files Browse the repository at this point in the history
Lambdaで実行する場合にchainfileが更新されない問題を修正
  • Loading branch information
naaaaaaaaaaaf authored Feb 5, 2024
2 parents e2acc04 + 3a7acef commit a562a26
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ def worker():
filename = "{}@{}.json".format(account_info["username"], domain).lower()
filepath = os.path.join(base_dir, os.path.basename(filename.lower()))

now = datetime.datetime.now().timestamp()
if is_lambda:
s3.get_file(filename, filepath)
_, last_modified = s3.get_file(filename, filepath)
else:
last_modified = os.path.getmtime(filepath) if os.path.isfile(filepath) else None

if (os.path.isfile(filepath) and datetime.datetime.now().timestamp() - os.path.getmtime(filepath) < 60 * 60 * 24):
if (os.path.isfile(filepath) and last_modified is not None and now - last_modified < 60 * 60 * 24):
print("モデルは再生成されません")
else:
exportModel.generateAndExport(mastodonTool.loadMastodonAPI(domain, read_access_token, account_info['id'], params), filepath)
Expand Down
8 changes: 5 additions & 3 deletions src/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

def get_file(key, file_path):
try:
s3.Bucket(os.environ["S3_BUCKET"]).download_file(key, file_path)
bucketName = os.environ["S3_BUCKET"]
object_summery = s3.ObjectSummary(bucketName, key)
s3.Bucket(bucketName).download_file(key, file_path)
print("File downloaded: {} to {}".format(key, file_path))
return True, object_summery.last_modified.timestamp()
except ClientError:
print("File not found: {}".format(key))
return False
return True
return False, None


def put_file(key, file_path):
Expand Down

0 comments on commit a562a26

Please sign in to comment.