Skip to content

Instantly share code, notes, and snippets.

View Loschcode's full-sized avatar
🌀
Probably coding

Laurent Schaffner Loschcode

🌀
Probably coding
View GitHub Profile
@Loschcode
Loschcode / job_scheduler.go
Created April 20, 2024 01:12
Job scheduler in Golang
package job_scheduler
import (
"context"
"database/sql/driver"
"encoding/json"
"fmt"
"log"
"time"
Engines::Core::Rpc::Client.new(
service: ::Rpc::Services::LiveCommunications::Messages
).get_message(
::Rpc::Backend::GetMessageRequest.new(
message_id: "21ef149a-09ac-11ed-861d-0242ac120002"
)
)
syntax = "proto3";
package rpc.my_package;
service MyService {
rpc GetSomething(GetSomethingRequest) returns (GetSomethingResponse) {}
}
# concurrent migration strategy
# when migrating through this script
# you can launch it on multiple processes
# and they will deal with the concurrency by
# themselves without interrupting or crashing
class MigrateConcurrently
class Error < StandardError; end
DELAY_BETWEEN_CYCLES_IN_SECONDS = 3
MAX_CYCLES = 100
class MigrateConcurrently
class Error < StandardError; end
...
def raise_if_max_cycles_reached
...
raise Error, ...
end
end
class MigrateConcurrently
DELAY_BETWEEN_CYCLES_IN_SECONDS = 3
MAX_CYCLES = 100
def initialize; end
def perform
Logger.info(
...
delay_between_cycles_in_seconds: DELAY_BETWEEN_CYCLES_IN_SECONDS,
# concurrent migration strategy
# when migrating through this script
# you can launch it on multiple processes
# and they will deal with the concurrency by
# themselves without interrupting or crashing
class MigrateConcurrently
...
end
class MigrateConcurrently
...
def perform
...
Logger.info(
...
cycle: cycles.current,
max_cycles: cycles.max,
)
class Cycles
attr_reader :current, :max
def initialize(current: 1, max: 1)
@current = current
@max = max
end
def next
@current += 1
class MigrateConcurrently
def initialize; end
def perform
# migration taken directly
# from ActiveRecord
ActiveRecord::Base.connection.migration_context.migrate
rescue ActiveRecord::ConcurrentMigrationError
# we log the details of the migrations
# through our logger