Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scheduling a sidekiq job with a python script

I have Sidekiq running with a Rails app. I need to be able to run a job from a Python script (as I'm using Luigi to run tasks in general). I'm searching for a Python library to work with the Sidekiq API but so far no luck. Any ideas or thoughts on this?

like image 789
gaba Avatar asked Sep 09 '26 09:09

gaba


2 Answers

This is the translation to Python:

import redis
import json

from random import choice
from datetime import datetime

sidekiq_queues = redis.Redis(host=host, port=port, db=db, password=password)

queue = 'my_queue'
job = 'MyJob'
arguments = 123456789

value = {
    "class": job,
    "queue": queue,
    "args": [arguments],
    "retry": 'true',
    "jid": ''.join([choice('qwertyuiopasdfghjklzxcvbnm1234567890') for i in range(24)]),
    "created_at": datetime.now().timestamp(),
    "enqueued_at": datetime.now().timestamp()}

sidekiq_queues.sadd(f"queues", queue)
sidekiq_queues.lpush(f"queue:{queue}" , json.dumps(value))
like image 180
nandoquintana Avatar answered Sep 10 '26 22:09

nandoquintana


https://github.com/mperham/sidekiq/wiki/FAQ#how-do-i-push-a-job-to-sidekiq-without-ruby

This is the simplest Ruby, translate it to Python:

require 'securerandom'
require 'json'

redis = Redis.new(:url => 'redis://hostname:port/db')
msg = { "class" => 'MyWorker',
    "queue" => 'default',
    "args" => [1, 2, 3],
    'retry' => true,
    'jid' => SecureRandom.hex(12),
    'created_at' => Time.now.to_f,
    'enqueued_at' => Time.now.to_f }
redis.lpush("queue:default", JSON.dump(msg))
like image 25
Mike Perham Avatar answered Sep 10 '26 22:09

Mike Perham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!