-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.py
58 lines (46 loc) · 1.33 KB
/
jobs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
from pymongo import MongoClient
import pymongo
import requests
import datetime
import config
# mc = MongoClient(
# "mongodb+srv://{}:{}@{}/test?retryWrites=true".format(
# config.MONGO_USER, config.MONGO_PW, config.MONGO_HOST
# )
# )
MONGO_JOBS = mc.loser.jobs
def get_oldest_job():
job = MONGO_JOBS.find_one(
{"status": "available"}, sort=[("submit_dt", pymongo.ASCENDING)]
)
return job
def get_owned_job():
job = MONGO_JOBS.find_one(
{"status": "running", "kitty_name": config.KITTY_NAME},
sort=[("submit_dt", pymongo.ASCENDING)],
)
return job
def take_job(mongo_job):
MONGO_JOBS.update_one(
{"_id": mongo_job["_id"]},
{"$set": {"status": "running", "kitty_name": config.KITTY_NAME}},
upsert=False,
)
taken = MONGO_JOBS.find_one({"_id": mongo_job["_id"]})
return config.KITTY_NAME == taken["kitty_name"]
def finish_job(mongo_job):
# check if still saying running and kitty_name matches, then
MONGO_JOBS.update_one(
{"_id": mongo_job["_id"]},
{
"$set": {
"status": "completed",
"complete_dt": datetime.datetime.utcnow(),
"kitty_name": config.KITTY_NAME,
}
},
upsert=False,
)
def get_assigned():
requests.get()