~sircmpwn/builds.sr.ht

builds.sr.ht/buildsrht/runner.py -rw-r--r-- 1.7 KiB
d2ff9914Simon Ser go generate 7 days ago
                                                                                
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
import json
from celery import Celery
from datetime import datetime
from srht.config import cfg
from srht.database import db
from srht.email import send_email
from srht.oauth import UserType
from srht.metrics import RedisQueueCollector
from prometheus_client import Counter

allow_free = cfg("builds.sr.ht", "allow-free", default="no") == "yes"

builds_broker = cfg("builds.sr.ht", "redis")
runner = Celery('builds', broker=builds_broker, config_source={
    "CELERY_TASK_SERIALIZER": "json",
    "CELERY_ACCEPT_CONTENT": ["json"],
    "CELERY_RESULT_SERIALIZER": "json",
    "CELERY_ENABLE_UTC": True,
    "CELERY_TASK_PROTOCOL": 1
})

builds_queue_metrics_collector = RedisQueueCollector(builds_broker, "buildsrht_builds", "Number of builds currently in queue")
builds_submitted = Counter("buildsrht_builds_submited", "Number of builds submitted")

def queue_build(job, manifest):
    from buildsrht.types import JobStatus
    job.status = JobStatus.queued
    db.session.commit()
    # crypto mining attempt
    # pretend to accept it and let the admins know
    sample = json.dumps(manifest.to_dict())
    if "xuirig" in sample or "miner" in sample or "selci" in sample:
        send_email(f"User {job.owner.canonical_name} attempted to submit cryptocurrency mining job #{job.id}",
                cfg("sr.ht", "owner-email"),
                "Cryptocurrency mining attempt on builds.sr.ht")
    else:
        builds_submitted.inc()
        run_build.delay(job.id, manifest.to_dict())

def requires_payment(user):
    if allow_free:
        return False
    return user.user_type not in [
        UserType.admin,
        UserType.active_paying,
        UserType.active_free,
    ]

@runner.task
def run_build(job_id, manifest):
    pass # see worker/context.go