Skip to content

Commit 89e3a7e

Browse files
authored
Add --tier option to create job and cronjob commands (#49)
1 parent 41be6f0 commit 89e3a7e

5 files changed

Lines changed: 28 additions & 12 deletions

File tree

estela_cli/create/cronjob.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@
4040
callback=set_tag_format,
4141
help="Set spider cronjob tag (may have multiple)",
4242
)
43+
@click.option(
44+
"--tier",
45+
"-r",
46+
type=click.Choice(["TINY", "XSMALL", "SMALL", "MEDIUM", "LARGE", "XLARGE", "HUGE", "XHUGE"]),
47+
help="Set spider cronjob resource tier",
48+
)
4349
@click.option(
4450
"--day",
4551
"-d",
4652
type=click.INT,
4753
callback=validate_positive,
4854
help="Set spider cronjob data expiry days",
4955
)
50-
def estela_command(sid, pid, schedule, arg, env, tag, day):
56+
def estela_command(sid, pid, schedule, arg, env, tag, tier, day):
5157
"""Create a new cronjob
5258
5359
\b
@@ -67,7 +73,7 @@ def estela_command(sid, pid, schedule, arg, env, tag, day):
6773
)
6874
try:
6975
response = estela_client.create_spider_cronjob(
70-
pid, sid, schedule, arg, env, tag, day
76+
pid, sid, schedule, arg, env, tag, tier, day
7177
)
7278
click.echo("cronjob/{} created.".format(response["name"]))
7379
except Exception as ex:

estela_cli/create/job.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@
3939
callback=set_tag_format,
4040
help="Set spider job tag (may have multiple)",
4141
)
42+
@click.option(
43+
"--tier",
44+
"-r",
45+
type=click.Choice(["TINY", "XSMALL", "SMALL", "MEDIUM", "LARGE", "XLARGE", "HUGE", "XHUGE"]),
46+
help="Set spider job resource tier",
47+
)
4248
@click.option(
4349
"--day",
4450
"-d",
4551
type=click.INT,
4652
callback=validate_positive,
4753
help="Set spider job data expiry days",
4854
)
49-
def estela_command(sid, pid, arg, env, tag, day):
55+
def estela_command(sid, pid, arg, env, tag, tier, day):
5056
"""Create a new job
5157
5258
\b
@@ -64,7 +70,7 @@ def estela_command(sid, pid, arg, env, tag, day):
6470
"No active project in the current directory. Please specify the PID."
6571
)
6672
try:
67-
response = estela_client.create_spider_job(pid, sid, arg, env, tag, day)
73+
response = estela_client.create_spider_job(pid, sid, arg, env, tag, tier, day)
6874
click.echo("job/{} created.".format(response["name"]))
6975
except Exception as ex:
7076
raise click.ClickException("Cannot create the job for given SID and PID.")

estela_cli/estela_client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from datetime import date, timedelta
21
from pathlib import Path
32

43
import requests
@@ -241,16 +240,18 @@ def get_spider_job_data(self, pid, sid, jid, datatype, last_chunk=None):
241240
self.check_status(response, 200)
242241
return response.json()
243242

244-
def create_spider_job(self, pid, sid, args=[], env_vars=[], tags=[], day=None):
243+
def create_spider_job(self, pid, sid, args=[], env_vars=[], tags=[], tier=None, day=None):
245244
endpoint = "projects/{}/spiders/{}/jobs".format(pid, sid)
246245
data = {
247246
"args": args,
248247
"env_vars": env_vars,
249248
"tags": tags,
250249
"data_status": "PENDING" if day else "PERSISTENT",
251250
}
251+
if tier:
252+
data["resource_tier"] = tier
252253
if day:
253-
data["data_expiry_days"] = f"{date.today() + timedelta(days=day)}"
254+
data["data_expiry_days"] = day
254255

255256
response = self.post(endpoint, data=data)
256257
self.check_status(response, 201)
@@ -263,7 +264,7 @@ def stop_spider_job(self, pid, sid, jid):
263264
return response.json()
264265

265266
def create_spider_cronjob(
266-
self, pid, sid, schedule="", args=[], env_vars=[], tags=[], day=None
267+
self, pid, sid, schedule="", args=[], env_vars=[], tags=[], tier=None, day=None
267268
):
268269
endpoint = "projects/{}/spiders/{}/cronjobs".format(pid, sid)
269270
data = {
@@ -272,9 +273,10 @@ def create_spider_cronjob(
272273
"cenv_vars": env_vars,
273274
"ctags": tags,
274275
"data_status": "PENDING" if day else "PERSISTENT",
276+
"data_expiry_days": day if day else 1,
275277
}
276-
if day:
277-
data["data_expiry_days"] = f"0/{day}"
278+
if tier:
279+
data["resource_tier"] = tier
278280

279281
response = self.post(endpoint, data=data)
280282
self.check_status(response, 201)

estela_cli/list/cronjob.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ def estela_command(sid, pid, tag):
6262
format_tags(cronjob["ctags"]),
6363
format_key_value_pairs(cronjob["cargs"]),
6464
format_key_value_pairs(cronjob["cenv_vars"]),
65+
cronjob.get("resource_tier", ""),
6566
]
6667
for cronjob in cronjobs
6768
]
6869

69-
headers = ["CJID", "STATUS", "SCHEDULE", "TAGS", "ARGS", "ENV VARS"]
70+
headers = ["CJID", "STATUS", "SCHEDULE", "TAGS", "ARGS", "ENV VARS", "RESOURCE TIER"]
7071
click.echo(tabulate(cronjobs, headers, numalign="left", tablefmt="plain"))

estela_cli/list/job.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ def estela_command(sid, pid, tag, page):
7171
format_tags(job["tags"]),
7272
format_key_value_pairs(job["args"]),
7373
format_key_value_pairs(job["env_vars"]),
74+
job.get("resource_tier", ""),
7475
format_time(job["created"]),
7576
]
7677
for job in jobs
7778
]
7879

79-
headers = ["JID", "STATUS", "TAGS", "ARGS", "ENV VARS", "CREATED"]
80+
headers = ["JID", "STATUS", "TAGS", "ARGS", "ENV VARS", "RESOURCE TIER", "CREATED"]
8081
click.echo(tabulate(jobs, headers, numalign="left", tablefmt="plain"))

0 commit comments

Comments
 (0)