scheduling a sidekiq job with a python script
Asked Answered
G

2

5

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?

Gluteus answered 19/9, 2017 at 17:14 Comment(0)
D
4

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))
Dropwort answered 19/9, 2017 at 17:29 Comment(4)
Thanks for the answer! I tried this but I still can not see it in the sidekiq interface. I have been looking for different ways to send it to a queue that sidekiq can see but no luck so far.Gluteus
See the note at the end of that linked FAQ.Dropwort
After playing with redis for a while I was able to make it work. It helped to install a visual tool to look at redis.Gluteus
Running redis-cli monitor against your localhost can be very educational.Dropwort
H
6

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))
Hysterectomy answered 3/4, 2019 at 8:37 Comment(0)
D
4

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))
Dropwort answered 19/9, 2017 at 17:29 Comment(4)
Thanks for the answer! I tried this but I still can not see it in the sidekiq interface. I have been looking for different ways to send it to a queue that sidekiq can see but no luck so far.Gluteus
See the note at the end of that linked FAQ.Dropwort
After playing with redis for a while I was able to make it work. It helped to install a visual tool to look at redis.Gluteus
Running redis-cli monitor against your localhost can be very educational.Dropwort

© 2022 - 2024 — McMap. All rights reserved.