Skip to content

Instantly share code, notes, and snippets.

View tansengming's full-sized avatar
🏠
Working from home

SengMing Tan tansengming

🏠
Working from home
View GitHub Profile
curl --silent https://raw.githubusercontent.com/cmusphinx/cmudict/master/cmudict.dict |
egrep '[a-z]+\s+[^AEIOU]*[AEIOU]+[^AEIOU]+[AEIOU]+[^AEIOU]*$' |
perl -pe 's/([a-z]+).*/\1/' |
comm --nocheck-order -12 - /usr/share/dict/words |
shuf -n 2
@tansengming
tansengming / Rakefile
Last active February 11, 2020 22:22
Janky daily twitter daily feed
require 'irb'
require 'pathname'
require 'json'
require 'rake/clean'
require 'time'
class Tweet
attr_reader :tweet_id, :created_at
def initialize(row)
commnunity.users.each do |recipient|
sms_credits = community.sms_credits
if sms_credits > 0
# this chucks the job into a queue and returns immediately!
SmsSenderJob.perform_later recipient, 'Everything is fine.'
# take off a credit
community.sms_credits = sms_credits - 1
community.save
# Job 1
sms_credits = community.sms_credits # start with 100 credits
# Switch! -------------------------------------------------------------------
# Job 2
sms_credits = community.sms_credits # still 100 credits!!
if sms_credits > 0
send_sms recipient, message
# take off a credit
community.sms_credits = sms_credits - 1
# Job 1
sms_credits = community.sms_credits # start with 100 credits
if sms_credits > 0
send_sms recipient, message
# take off a credit
community.sms_credits = sms_credits - 1
community.save # 99 credits
end
class SmsSenderJob < ApplicationJob
def perform(recipient, community, message)
sms_credits = community.sms_credits
if sms_credits > 0
send_sms recipient, message # this still takes a while.
# take off a credit
community.sms_credits = sms_credits - 1
community.save
commnunity.users.each do |recipient|
# this chucks the job into a queue and returns immediately!
# it no longer takes a while to finish.
SmsSenderJob.perform_later recipient, community, 'Everything is fine.'
end
# Here's where you define the job that gets run by the workers
class SmsSenderJob < ApplicationJob
def perform(recipient, community, message)
sms_credits = community.sms_credits

Thread Safely (Draft 3)

Introduction

I have mistakenly believed that I did not have to worry about thread safety in Regular Ruby because of the Global Interpreter Lock. Unfortunately the GIL is there to protect the Interpreter, not to save me from my dumb ass code. This post is based on a true story.

The Setup

Let’s say I needed to send an SMS to everyone in a community. Let’s say every community had to pay for sms credits and I had to check for credits before sending their SMS. I could start with this,

class SmsSenderJob < ApplicationJob
def perform(recipient, message)
send_sms(recipient, message)
end
def send_sms(recipient, message)
# ...
end
end
community.users.each do |recipient|
sms_credits = community.sms_credits
if sms_credits > 0
send_sms recipient, 'Everything is fine.'
# take off a credit
community.sms_credits = sms_credits - 1
community.save
end